Notification(Android)消息推送機制


Notification有哪些功能作用呢

  1)顯示接收的短信,消息(QQ,微信,新浪,愛奇藝等)

  2)顯示客戶端的推送消息

  3)顯示正在進行的事務(正在播放的音樂,下載進度條)

通知狀態欄主要涉及到兩個類,Notification、NotificationManager

Notification是通知消息類,它里面對應的通知欄的各種屬性

 

Notification使用步驟

  <1>.得到通知管理者,因為NotificationManager是一個系統Service,所以需要通過getService得到

NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

  <2>.創建Notification

NotificationCompat.Builder builder=new NotificationCompat.Builder(this);

  <3>.為Notification設置各種屬性

  <4>.通過通知管理者NotificationManager發送通知

notificationManager.notify(0x101,notification);

  <5>.刪除通知

notificationManager.cancel(NOTIFICATION_ID);

 

完整的代碼
1.創建對應的service

 1 class TaskManageNotificationService extends Service {
 2                 // 獲取消息線程
 3                 private MessageThread messageThread = null;
 4 
 5                 // 點擊查看
 6                 private Intent messageIntent = null;
 7                 private PendingIntent messagePendingIntent = null;
 8 
 9                 // 通知欄消息
10                 private int messageNotificationID = 1000;
11                // private Notification messageNotification = null;
12                 private NotificationManager messageNotificationManager = null;
13                 //實例化通知
14                 private NotificationCompat.Builder builder = null;
15 
16                 public TaskManageNotificationService() {
17                 
18                 }
19 
20                 @Override
21                 public IBinder onBind(Intent intent) {
22                     return null;
23                 }
24 
25                 @Override
26                 public int onStartCommand(Intent intent, int flags, int startId) {
27 
28                     //實例化通知管理器
29                     messageNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
30                    
31 
32                     messageIntent = new Intent(this, MainActivity.class);
33                     messagePendingIntent = PendingIntent.getActivity(this, 0,
34                             messageIntent, 0);
35 
36                     // 開啟線程
37                     messageThread = new MessageThread();
38                     messageThread.isRunning = true;
39                     messageThread.start();
40 
41                     return super.onStartCommand(intent, flags, startId);
42                 }
43 
44 
45                 /**
46                  * 從服務器端獲取消息
47                  *
48                  */
49                 class MessageThread extends Thread {
50                     // 設置是否循環推送
51                     public boolean isRunning = true;
52 
53                     public void run() {
54                          while (isRunning) {
55                             try {
56                                 // 間隔時間
57                                 Thread.sleep(1000);
58                                 // 獲取服務器消息
59                                 String serverMessage = getServerMessage();
60                                 if (serverMessage != null && !"".equals(serverMessage)) {
61                                      //實例化通知
62                                     builder = new NotificationCompat.Builder(this);
63                                     builder.setContentTitle("新任務");//設置通知標題
64                                     builder.setContentText("不要放孔明燈,容易起火");//設置通知內容
65                                     builder.setDefaults(NotificationCompat.DEFAULT_ALL);//設置通知的方式,震動、LED燈、音樂等
66                                     builder.setAutoCancel(true);//點擊通知后,狀態欄自動刪除通知
67                                     builder.setSmallIcon(R.drawable.court_easyicon_9);//設置小圖標
68                                     // 更新通知欄 設置通知內容
69                                     builder.setContentText(serverMessage);
70                                     //設置點擊通知后將要啟動的程序組件對應的PendingIntent
71                                     builder.setContentIntent(messagePendingIntent);
72                                     messageNotificationManager.notify(messageNotificationID,
73                                             builder.build());
74                                     // 每次通知完,通知ID遞增一下,避免消息覆蓋掉
75                                     messageNotificationID++;
76                                 }
77                             } catch (InterruptedException e) {
78                                 e.printStackTrace();
79                             }
80                          }
81                     }
82                 }
83 
84                 @Override
85                 public void onDestroy() {
86                     // System.exit(0);
87                     messageThread.isRunning = false;
88                     super.onDestroy();
89                 }
90 
91                 /**
92                  * 模擬發送消息
93                  *
94                  * @return 返回服務器要推送的消息,否則如果為空的話,不推送
95                  */
96                 public String getServerMessage() {
97                     return "NEWS!";
98                 }
99         }

2.在mainfeast.xml文件中配置Service

<service  android:name=".services.TaskManageNotificationService">
    <intent-filter>
        <action android:name="task_manage_notification"/>
    </intent-filter>
</service>    

3.使用創建的service

// 啟動service
Intent intent = new Intent();
intent.setAction("task_manage_notification");
startService(intent);

// 停止service
Intent intent = new Intent();
intent.setAction("task_manage_notification");
stopService(intent);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM