来自FirstActivity我正在创建三个单独的Notifications并在单击它们时启动SecondActivity,它位于同一个应用程序中。问题是SecondActivity中字符串的值始终是我发布的最后一个通知。我需要启动SecondActivity并显示调用来自通知1,当我点击ID为1的通知时,类似地对于id = 2和id = 3。但我失去那些在我的意图对象中加入额外内容的东西。如果您以前遇到过这种情况,请帮助我。我已经尝试了PendingIntent标志FLAG_CANCEL_CURRENT但是徒劳无功。提前致谢。 : - )
NotificationManager nm;
Notification n;
PendingIntent pi1, pi2, pi3;
Button btnNotify1, btnNotify2, btnNotify3;
Intent intent;
final static String TICKER_TEXT = "My_Notification";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
btnNotify1 = (Button) findViewById(R.id.btnNotify1);
btnNotify2 = (Button) findViewById(R.id.btnNotify2);
btnNotify3 = (Button) findViewById(R.id.btnNotify3);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
n = new Notification(R.drawable.ic_launcher, TICKER_TEXT,
System.currentTimeMillis());
n.flags = n.flags | Notification.FLAG_AUTO_CANCEL;
intent = new Intent(this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
btnNotify1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("call_is_from", "Notification 1");
pi1 = PendingIntent.getActivity(getApplicationContext(), 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(getApplicationContext(), "Notification 1",
"One", pi1);
nm.notify(1, n);
}
});
btnNotify2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("call_is_from", "Notification 2");
pi2 = PendingIntent.getActivity(getApplicationContext(), 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(getApplicationContext(), "Notification 2",
"Two", pi2);
nm.notify(2, n);
}
});
btnNotify3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("call_is_from", "Notification 3");
pi3 = PendingIntent.getActivity(getApplicationContext(), 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(getApplicationContext(), "Notification 3",
"Three", pi3);
nm.notify(3, n);
}
});
}
答案 0 :(得分:2)
您需要确保每个PendingIntent
都是唯一的。由于您使用相同的PendingIntent
和相同的Intent
获得requestCode
,因此您将始终获得相同的PendingIntent
(即使附加内容不同)。
要确保您的每个PendingIntent
都是唯一的,请为每个requestCode
使用不同的pi1 = PendingIntent.getActivity(getApplicationContext(), 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
pi2 = PendingIntent.getActivity(getApplicationContext(), 2,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
pi3 = PendingIntent.getActivity(getApplicationContext(), 3,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
,如下所示:
{{1}}