您现在的位置是:首页 > 文章详情

【Android】监听Notification被清除

日期:2017-12-03点击:561

     private  final BroadcastReceiver mBroadcastReceiver =  new BroadcastReceiver() {

        @Override
         public  void onReceive(Context context, Intent intent) {
             if (intent ==  null || context ==  null) {
                 return;
            }

            mNotificationManager.cancel(NOTIFICATION_ID_LIVE);

            String type = intent.getStringExtra(PUSH_TYPE);
             if (PUSH_TYPE_LINK.equals(type)) {
                 // mNumLinkes = 0;
            }  else  if (PUSH_TYPE_LIVE.equals(type)) {
                 // mNumLives = 0;
            }
             // 这里可以重新计数
        }
    };


     private  void sendLiveNotification() {
        Intent intent =  new Intent(NOTIFICATION_CLICK_ACTION);

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

        String title = "Push测试";
        mBuilder.setContentTitle(title);
        mBuilder.setTicker(title);
        mBuilder.setContentText("https://233.tv/over140");
        mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        mBuilder.setSmallIcon(R.drawable.ic_action_cast);
        mBuilder.setDefaults(Notification.DEFAULT_ALL);
        mBuilder.setWhen(System.currentTimeMillis());
        mBuilder.setContentIntent(PendingIntent.getBroadcast( this, NOTIFICATION_ID_LIVE, intent, 0));
        mBuilder.setDeleteIntent(PendingIntent.getBroadcast( this, NOTIFICATION_ID_LIVE,  new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_TYPE, PUSH_TYPE_LIVE), 0));

        mNotificationManager.notify(NOTIFICATION_ID_LIVE, mBuilder.build());
    }

  代码说明 

1、最重要的是setDeleteIntent,这个在API Level 11(Android 3.0) 新增的

2、注意不要设置setAutoCancel为true,否则监听器接收不到

3、这里统一了点击通知和消除通知的操作

4、注意广播在推送前要注册好

2015-03-28

实际使用中发现还是有一点问题,比如Service被Kill掉了,通知栏点击就会没有反应了,这块还需要再考虑一下,比如把Broadcast在AndroidMainfest中监听,,,

本文转自博客园农民伯伯的博客,原文链接:【Android】监听Notification被清除,如需转载请自行联系原博主。



原文链接:https://yq.aliyun.com/articles/327408
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章