2015年1月22日木曜日

【Android】ノーティフィケーションにアイコンは必須

 Androidで、以前作ったノーティフィケーション通知クラスをコピペで使いまわそうと思ったら、以前は問題なく動いていたものが動かなくてはまってしまったので、メモ。


結論:アイコンは必須 。セットしないと動かないしエラーも出ない。
備考:ノーティフィケーションビルダーにノーティフィケーションインスタンスを作成させる。

------------------------------------------------------
パッケージ名

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;


/**
 * ノーティフィケーション通知を行うクラス
 */
public class ClassNotificationSender{
    //Singletonデザインパターン : このクラスの唯一のインスタンス
    private static ClassNotificationSender notification_sender = new ClassNotificationSender();

    //ノーティフィケーションのID
    private int notification_id = 0;

    //クラス名
    private static String class_name = "ClassNotificationSender";


    ///////////////////////////////////////////////////////////
    //コンストラクタ
    ///////////////////////////////////////////////////////////
    /**
     * 同じインスタンスはひとつしか作れないようにする
     * Singletonデザインパターン
     */
    private ClassNotificationSender(){
        System.out.println("ClassNotificationSender インスタンスを生成しました。(Singletonデザインパターン)");
    }//function


    ///////////////////////////////////////////////////////////
    /**
     * インスタンス取得
     */
    public static ClassNotificationSender getInstance(){
        return notification_sender;
    }//function


    ///////////////////////////////////////////////////////////
    //メソッド
    ///////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////
    /**
     * プッシュ通知ノーティフィケーション表示。
     *
     * メインアプリ非稼働時にも動作する。
     * インスタンス作成時にコンテキストが得られないようなので
     * このメソッド引数にコンテキストを必要とすることにした。
     *
     * @param  String  通知すべきテキスト
     * @param  Context コンテキストインスタンス
     * @return void
     */
    public void sendNotification(
        String  content_text,
        Context context
    ){
        //nullチェック
        if(
            (content_text == null)||
            (context == null)
        ){
            return;
        }//if

        ////////////////////////////////////
        //ノーティフィケーションインスタンス作成
        //ノーティフィケーションインスタンスを作る際は Notification.Builder を使うのが推奨 APIレベル11以降

        //ノーティフィケーションビルダーインスタンスを作成
        Notification.Builder notification_builder = new Notification.Builder(context);

        //ノーティフィケーションの各種内容をセット
        notification_builder.setTicker("通知あり");                //メインの画面の上部の狭い領域に表示される文字列
        notification_builder.setContentTitle("通知タイトル");      //通知一覧に表示されるタイトル
        notification_builder.setContentText(content_text);         //通知一覧に表示される本文
        notification_builder.setNumber(this.notification_id);      //通し番号
        notification_builder.setSmallIcon(R.drawable.ic_launcher); //注意! アイコンは必須で、無いと表示されないしエラーも出ない

        //ビルダーにノーティフィケーションインスタンスを作成させる
        Notification notification = notification_builder.build();

        //通知表示実行
        //ノーティフィケーションマネジャーインスタンス
        NotificationManager notification_manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notification_manager.notify(
            context.getPackageName(),    //ノーティフィケーション識別タグ
            this.notification_id,        //ノーティフィケーションに割り振ったID
            notification                 //ノーティフィケーションインスタンス
        );

        System.out.println(class_name + "[通過]ノーティフィケーション発行 : " + notification.toString());

        //ノーティフィケーションのIDをカウントアップ
        this.notification_id++;

    }//function

}//class


0 件のコメント:

コメントを投稿