Notification getIntent().getExtras() gets the same bundle
The problem im having is I call sendAffimationNotice() twice passing a unique aff_id
each time (as confirmed by printing "putting " + aff_id
).
This prints 'putting 1'
and on the second call 'putting 2'
.
The phone now has 2 notifications. Though when both are clicked they开发者_如何学C both have the same ID toasted in the onCreate method on the intents activity.
Both times it prints "ID is: 1" even though both notifications are unique.
public class PossAffNotification{
private static int notif_ID = 1;
private static NotificationManager notificationManager;
private static Notification note;
private static PendingIntent contentIntent;
public static void sendAffimationNotice(Context ctx, String title,String contentText,int aff_id){
notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
note = new Notification(android.R.drawable.btn_star_big_on, contentText, System.currentTimeMillis() );
note.defaults |= Notification.DEFAULT_SOUND;
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.FLAG_AUTO_CANCEL;
note.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(ctx, com.mindfsck.PossAff.MainActivity.class);
notificationIntent.putExtra("aff_id",aff_id);
System.out.println("putting " + aff_id);
notificationIntent.setAction("com.mindfsck.PossAff.intent.action.aff");
notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
note.setLatestEventInfo(ctx, title, contentText, contentIntent);
notificationManager.notify(notif_ID,note);
notif_ID += 1;
}
};
public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent serviceIntent = new Intent(this,PAService.class);
this.startService(serviceIntent);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
Toast.makeText(getApplicationContext(), "ID is: " + extras.getInt("aff_id"),
Toast.LENGTH_SHORT).show();
}
}
}
Your Intent
is the same on all non-extra parameters (e.g., action). Hence, you get the same PendingIntent
back from your getActivity()
call, as there is only one PendingIntent
per distinct Intent
. You need to change something -- beyond extras -- in your second Intent
, such as a different action string.
The problem for this issue came from this line:
contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
The last parameter shouldn't be 0 it should be PendingIntent.FLAG_CANCEL_CURRENT
contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
otherwise in notificationIntent you will receive same data all the time
This article of mine may be helpful here: "A pitfal in PendingIntent"
The issue basically comes from the fact, that the system does not assume that only because we pass a new Intent object to the pending intent, we want this new intent object to be delivered and just keeps the old (initial) pending intent alive.
To get the desired semantics, we need to pass a flag to tell the system:
PendingIntent pintent =
PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
精彩评论