首页 > 代码库 > [备忘]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
Intent resultIntent  = null;
        if (!TextUtils.isEmpty(tid)){
            resultIntent = new Intent("com.shijiebang.notify");
            resultIntent.putExtra("_notify_url", tid);
        }else{
            resultIntent = new Intent("com.shijiebang.notify2");
        }
 
        NotificationManager manager = (NotificationManager) context.getSystemService("notification");
        // 1.创建一个NotificationCompat.Builder预对象
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        // 2.设置主要信息
        // builder.setContentTitle(title_); // 设置内容题目
        builder.setContentText(content); // 设置内容文本信息
        builder.setSmallIcon(context.getApplicationInfo().icon); // 设置小图标
        builder.setContentInfo("contentinfo"); // 设置内容info
        builder.setTicker("ticker"); // 对应的Notification对象create时,以消息在标题栏提示
        builder.setWhen(System.currentTimeMillis()); // 设置时间
        builder.setAutoCancel(true); // 默认点击对应的notification对象后,该对象消失
        // 3.设置意图对象
        PendingIntent pendingIntent = PendingIntent.getActivity(context, // The
 
                0, // request code
                resultIntent, // Intent of the activity to be launched.
                PendingIntent.FLAG_UPDATE_CURRENT); // 待补充...
        /*
         * 隐含执行 MainActivity.this.startActivityForResult(intent, requestCode);
         */
        builder.setContentIntent(pendingIntent);
        // 4.得到一个notification对象(根据builder预设置信息)
        Notification notification = builder.build();
 
        // 5.唤醒notification对象
        /*
         * 将该notification发送到状态条上,如果id相同且没有消失,则直接更新该notification对象信息
         * 否则创建一个Notification实例对象
         */
        manager.notify(0, // int id 应用唯一值
                notification); // Notification notification 不得设置为null