LoginSignup
8
8

More than 5 years have passed since last update.

Titanium Androidアプリで通知バーにメッセージを表示

Last updated at Posted at 2014-03-29

TitaniumでAndroidアプリの通知バーにメッセージを表示(+クリックされた自アプリを起動)する方法を調べたのでメモ。
サーバからの通知を受け取ったりする処理は対象外。

通知の作成及び送信するコード部分

        var notification = Ti.Android.createNotification({
            //通知領域に表示されるアイコンの指定
            icon: Ti.App.Android.R.drawable.appicon,
            //通知領域に表示されるメッセージのタイトル部分
            contentTitle: 'contentTitle',
            //通知領域に表示されるメッセージ部分
            contentText: 'contentText',
            //通知領域の右側に表示される付属情報.多分数字だけしか指定できない.
            number: 5,
            //クリック時に発行されるIntentの指定
            contentIntent: pending,
            //以下の値を指定しておかないと通知時にLEDが光らないし、バイブも起きない
            //音は使ってないからDEFAULT_LIGHTS | DEFAULT_VIBRATEでもいいかも
            defaults:  Titanium.Android.DEFAULT_ALL, // DEFAULT_LIGHTS | DEFAULT_SOUND | DEFAULT_VIBRATE
            //画面を切ってる際に通知があることを知らせる為にLEDを光らせる
            flags: Ti.Android.FLAG_SHOW_LIGHTS,
            //通知メッセージが届いた際に画面上部の通知領域に表示されるメッセージ
            tickerText: "tickerText"
        });
        //通知番号は通知メッセージを識別するのに使う
        //同じ通知番号の通知を2つ以上送っても通知領域に表示されるメッセージは1つだけ
        //異なる通知番号の通知を送ると複数の通知が通知領域に表示される
        var notificationId = 1;
        Ti.Android.NotificationManager.notify(notificationId, notification);

通知メッセージクリック時に自分のアプリを起動するインテント作成

        var intent = Ti.Android.createIntent({
            flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK,
            className: 'com.example.notificationbar.NotificationbarActivity'        
        });
        var pending = Ti.Android.createPendingIntent({
            intent: intent,
            flags: Titanium.Android.FLAG_UPDATE_CURRENT 
        });

インテント生成時に指定するclassNameは自分のアプリのアクティビティ名.
以下はTitaniumで生成される場合のActivity名の命名規則。

<app_id>.<Appname>Activity
<app_id> : アプリケーションのapp ID(tiapp.xmlのidタグの値)
<Appname> : アプリケーションの名前(但し、最初の文字だけ大文字で後は全部小文字になる)

通知するためのサンプルコード全体

        var intent = Ti.Android.createIntent({
            flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK,
            className: 'com.example.notificationbar.NotificationbarActivity'        
        });
        var pending = Ti.Android.createPendingIntent({
            intent: intent,
            flags: Titanium.Android.FLAG_UPDATE_CURRENT 
        });
        var notification = Ti.Android.createNotification({
            icon: Ti.App.Android.R.drawable.appicon,
            contentTitle: 'contentTitle',
            contentText: 'contentText',
            contentIntent: pending,
            defaults:  Titanium.Android.DEFAULT_ALL, // DEFAULT_LIGHTS | DEFAULT_SOUND | DEFAULT_VIBRATE
            flags: Ti.Android.FLAG_SHOW_LIGHTS,
            tickerText: "tickerText",
            number: 5
        });
        var notificationId = 1;
        Ti.Android.NotificationManager.notify(notificationId, notification);

参考URL

Notifications
http://developer.android.com/guide/topics/ui/notifiers/notifications.html

GCM push notifications for Titanium (made easy)
http://iamyellow.net/post/40100981563/gcm-appcelerator-titanium-module

Titanium.Android.NotificationManager
http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.Android.NotificationManager

Android Notifications
https://wiki.appcelerator.org/display/guides/Android+Notifications

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8