项目里面需要监测用户从状态栏移除(滑动/点击X关掉)notification的事件,但是发现在Android 4.4.x版本手机上我们收不到移除事件触发的intent。
设置deleteIntent的代码如下:
Notification pushNotification = new Notification(
R.drawable.icon_notification, content,
System.currentTimeMillis());
Intent dIntent = new Intent(ACTION_IGNORE);
PendingIntent delIntent = PendingIntent.getBroadcast(mContext, 1,
dIntent, PendingIntent.FLAG_UPDATE_CURRENT);
pushNotification.deleteIntent = delIntent;
pushNotification.setLatestEventInfo(mContext, title, content,
contentIntent);
R.drawable.icon_notification, content,
System.currentTimeMillis());
Intent dIntent = new Intent(ACTION_IGNORE);
PendingIntent delIntent = PendingIntent.getBroadcast(mContext, 1,
dIntent, PendingIntent.FLAG_UPDATE_CURRENT);
pushNotification.deleteIntent = delIntent;
pushNotification.setLatestEventInfo(mContext, title, content,
contentIntent);
百思不得其解,最终在万能的stackoverflow找到了答案,把最后两句的位置换一下,改为:
pushNotification.setLatestEventInfo(mContext, title, content,
contentIntent);
pushNotification.deleteIntent = delIntent;
contentIntent);
pushNotification.deleteIntent = delIntent;
问题就出在setLatestEventInfo这个API中,看看几个版本的Notification.java的源码:
============== Android 4.1.1 ===================
public void setLatestEventInfo(Context context,
public void setLatestEventInfo(Context context,

在Android 4.4.x系统中,通过Notification的deleteIntent无法捕获用户移除通知的事件。问题源于setLatestEventInfo方法,在此版本中未正确保存deleteIntent。解决方案是调整代码顺序,确保在调用setLatestEventInfo之后设置deleteIntent。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



