:做一个项目,里面有类似​​微信​​聊天功能。在通知栏显示通知Notification后,点击进入聊天界面,聊天对象总是不是通知设定的对象。


1. Bundle bundle = new Bundle();  
2. bundle.putString("receiveMemberID", String.valueOf(chatObjectID) );
3. bundle.putString("receiveMemberName", chatObjectName);
4. bundle.putString("receiveMemberAvator", chatObjectAvator);
5.
6. // The PendingIntent to launch our activity if the user selects this notification
7. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context,pendingActivity).putExtras(bundle),0);


解决:在接收端,接收的数据一直不是设定传递的数据,在google官方网站查了下,发现问题出在flags上。

PendingIntent.getActivity原型如下:







1. PendingIntent android.app.PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags);


    因此查看官方api,发现最后一个参数参数是int flags,这 个值可以是FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT


简单翻译一下:
int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
int FLAG_UPDATE_CURRENT:如果该PendingIntent已经存在,则用新传入的Intent更新当前的数据。
我们需要把最后一个参数改为PendingIntent.FLAG_UPDATE_CURRENT,这样在启动的Activity里就可以用接收Intent传送数据的方法正常接收。