Reputation: 982
From FirstActivity I'm creating three seperate Notifications and on clicking them I start SecondActivity which is in the same application. The issue is the value of the string in the SecondActivity is always the last Notification which i posted. I need to start SecondActivity and display call is from Notification 1 when i click on Notification with id =1 and similarly for id = 2 and id =3. But I'm losing those put extras in my intent object. Please help me with this if you have come across such a situation before. I've tried PendingIntent flag FLAG_CANCEL_CURRENT but in vain. Thanks in advance. :-)
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);
}
});
}
Upvotes: 1
Views: 198
Reputation: 95626
You need to make sure that each of your PendingIntent
s is unique. Since you are getting a PendingIntent
using the same Intent
and the same requestCode
, you will always get the same PendingIntent
(even though the extras are different).
To make sure that each of your PendingIntent
s is unique, use a different requestCode
for each, like this:
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);
Upvotes: 2