我尝试在用户打开通知后向我的MainActivity发送附加信息。在我的sendNotification
方法中,我有以下代码
intent = new Intent(this, MainActivity.class);
intent.putExtra("fromNotification", true);
intent.putExtras(extras);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, 0);
在我的MainActivity中,我接受意图并获得额外内容:
Bundle extras = getIntent().getExtras();
if(extras != null){
System.out.println("MainActivity intent extras: " + getIntent().getExtras().toString());
if(extras.getBoolean("fromNotification"))
if(extras.getInt("not_id", 0) == 1) // Voicemail
switchContent(new VoicemailFragment());
else if (extras.getInt("not_id", 0) == 2) // Missed Call
switchContent(new RecentFragment());
else
switchContent(new MainDashboardFragment());
}
我的getInt("not_id", 0)
总是返回0.我检查了Bundle等于什么,这就是logcat显示的内容
服务
02-04 15:10:48.628: I/System.out(11835): Pending intent extras: Bundle[{message=New
Voicemail, collapse_key=do_not_collapse, not_id=1, fromNotification=true,
android.support.content.wakelockid=1, from=123412341234}]
MainActivity
02-04 15:12:29.206: I/System.out(11835): MainActivity intent extras: Bundle[mParcelledData.dataSize=56]
是否有理由我只能检索extras.getBoolean("fromNotification")
而不是extras.getInt("not_id", 0)
?
是的,我尝试过添加not_id
,就像我做的那样'来自通知`。
任何帮助都会很棒
AP
答案 0 :(得分:0)
每次调用getActivity时都使用不同的requestCode