首页 > 代码库 > 【Android】监听Notification被清除
【Android】监听Notification被清除
前言
一般非常驻的Notification是可以被用户清除的,如果能监听被清除的事件就可以做一些事情,比如推送重新计数的问题。
声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com
正文
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());
}
@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、注意广播在推送前要注册好
结束
好久不写博客就不找借口啦
【Android】监听Notification被清除
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。