Androidでのお知らせ-Notification



NotificationはAndroidでよく使われる通知方式で、メールを読んでいない場合や電話に出ていない場合、画面のステータスバーにヒントアイコンが表示され、ステータスバーを下げて通知を読み取ることができます.微信を使用する場合(微信はバックグラウンドで動作)、新しいメッセージがある場合は音声メッセージが発行され、ステータスバーにも対応する微信メッセージが表示されます.
AndroidでのNotification通知の実装手順:
1.NotificationManagerオブジェクトNotificationManagerを取得するための3つの共通の方法:1 cancel(int id)以前に表示した通知をキャンセルする.短い通知であれば、非表示にしようとします.永続的な通知であれば、ステータスバーから移動します.②cancelAll()以前に表示されたすべての通知をキャンセルします.③notify(int id,Notification notification)は、通知をステータスバーに永続的に送信する.
2.NotificationオブジェクトNotificationを初期化する属性:audioStreamType音声が鳴ると、使用するオーディオストリームのタイプcontentIntent通知エントリがクリックされると、この設定されたIntent contentViewがステータスバーに表示されると、同時にこの設定されたビューはdefaultsがどの値をデフォルトに設定するかを指定するdeleteIntentユーザーが「Clear All Notifications」ボタン領域をクリックしてすべての通知を削除すると、この設定されたIntentがiconステータスバーを実行するためのピクチャiconLevelステータスバーがいくつかある場合、ここでledARGB LEDランプの色ledOffMS LEDオフ時のフラッシュ時間(ミリ秒で計算)ledOnMS LED開始時のフラッシュ時間(ミリ秒で計算)numberこの通知はイベントの番号sound通知を表す音声tickerText通知がステータスバーに表示されたときに表示される情報vibrate振動モードwhen通知のタイムスタンプを設定する
注意:Notificationをステータスバーに常駐させる場合は、NotificationのflagsプロパティをFLAG_に設定できます.ONGOING_EVENT
1
n.flags = Notification.FLAG_ONGOING_EVENT 

3.通知の表示パラメータを設定するにはPendingIntentを使用して通知Intentをラッピングし、NotificationのsetLatestEventInfoを使用して通知のタイトル、通知内容などの情報を設定します.
4.通知を送信するには、NotificationManagerのnotify(int id,Notification)メソッドを使用して通知を送信します.
次のコードプレゼンテーションでは、Notificationの使用を通知します.
Notification実装の手順:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/** 
 * MainActivity 
 * @author zuolongsnail 
 */  
 public class MainActivity extends Activity {  
 private Button notifyBtn;  
 private Button cancelBtn;  
 private NotificationManager nm;  
 private Notification n;  
 //    ID  
 public static final int ID = 1;  
 @Override  
 public void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);  
 setContentView(R.layout.main);  
 notifyBtn = (Button) findViewById(R.id.notify);  
 cancelBtn = (Button) findViewById(R.id.cancel);  
 notifyBtn.setOnClickListener(new MyOnClickListener());  
 cancelBtn.setOnClickListener(new MyOnClickListener());  
 // 1.  NotificationManager    
 nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
 // 2.   Notification    
 n = new Notification();  
 //      icon  
 n.icon = R.drawable.notify;  
 //                   
 n.tickerText = "    ";  
 //          
 n.when = System.currentTimeMillis();  
 }  
 class MyOnClickListener implements OnClickListener{  
 @Override  
 public void onClick(View v) {  
 switch (v.getId()) {  
 case R.id.notify:  
 // 3.           
 Intent intent = new Intent(MainActivity.this, NotificationView.class);  
 PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);  
 n.setLatestEventInfo(MainActivity.this, "    ", "    ", pi);  
 // 4.      
 nm.notify(ID, n);  
 break;  
 case R.id.cancel:  
 //       
 nm.cancel(ID);  
 break;  
 }  
 }  
 }  
 }  

通知を開いてジャンプしたActivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/** 
 *         Activity 
 * @author zuolongsnail 
 */  
 public class NotificationView  extends Activity{  
 
 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);  
 setContentView(R.layout.view);  
 //       
 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
 nm.cancel(MainActivity.ID);  
 }  
 }  

スクリーンショット:
補足:
Notification提示方式:①ステータスバー(Status Bar)に表示される通知テキスト提示:notification.tickerText = “Hello Notification”;
②提示音を出す.
1
2
3
notification.defaults |= Notification.DEFAULT_SOUND; 
notification.sound = Uri.parse("<a>file:///sdcard/notification/ringer.mp3</a>"); 
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

③携帯電話の振動、例えば:
1
2
3
4
5
notification.defaults |= Notification.DEFAULT_VIBRATE;
 
// 100      ,   200   ,   100    ,    300   
long[] vibrate = {100,200,100,300};
notification.vibrate = vibrate;

注意:権限defaults |= Notification.DEFAULT_LIGHTS; notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS;