Live2D

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);
posted @ 2020-09-03 10:51  AcmeZhang  阅读(853)  评论(0编辑  收藏  举报