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

在使用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);



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值