解决Notification PendingIntent意图打开Activity数据没有更新的问题

来源:互联网 发布:网络教育和函授含金量 编辑:程序博客网 时间:2024/05/19 18:44

在使用Notification通知时,如果没有设置PendingIntent.FLAG_UPDATE_CURRENT就会出现在通知栏点击第二条通知时还是第一条的通知的数据,以下解释:

8. PendingIntent中定义了几个FLAG。

(1) android.app.PendingIntent.FLAG_UPDATE_CURRENT

如果PendingIntent已经存在,保留它并且只替换它的extra数据。

int android.app.PendingIntent.FLAG_UPDATE_CURRENT = 134217728 [0x8000000] 
Flag for use with getActivity, getBroadcast, and getService: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.


代码:

 Context context = getApplicationContext();  NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.jpush_notification_icon, "南方有道", System.currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL;  //点击后自动消失 Intent intent = new Intent(this,TestActivity.class); intent.putExtra("message", msg); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(context, "标题", msg, pendingIntent); notificationManager.notify(0, notification);



原创粉丝点击