Question

I have a simple application (custom calendar). Every time a user creates event a new AlarmManager with custom broadcast receiver is created to call a notification 5 minuts before the event start. When that time come and user click on the notification a simple activity just shows the events info.

What is the problem: When i run app and create the event for the first time, everything works fine. When user click notification the correct one is displayed. But, when I create the second event, the problem occurs. When the notification for second notification is displayed, it looks fine (the correct information is shown in notification bar), but when i click it the first created event is shown. The same with third, fourth ...

how alarm manager is created, this is located in the activity that create event...

if (hasAlarm) {
                Bundle bundle = new Bundle();
                bundle.putString("name", eventName);
                bundle.putLong("time", dateStart);
                bundle.putString("desc", eventDescription);
                bundle.putString("location", eventLocation);
                bundle.putLong("end", dateEnd);
                bundle.putString("username", username);
                bundle.putString("password", password);
                bundle.putString("mailServer", mailServer);
                Intent alarmIntent = new Intent(this, AlarmReceiver.class);
                alarmIntent.putExtras(bundle);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
                        0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC_WAKEUP, dateStart
                        - (1000 * 60 * 5), pendingIntent);
}

and custom broadcast receiver ...

public class AlarmReceiver extends BroadcastReceiver {

    private static int id = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String desc = bundle.getString("desc");
        long time = bundle.getLong("time");
        String name = bundle.getString("name");

        for (String key : bundle.keySet()) {
            Log.d("Bundle ALARM REVEIVER", key + " = \"" + bundle.get(key)
                    + "\"");
        }

        long[] vibrate = { 0, 100, 200, 300 };
        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.stat_notify_chat)
                .setContentTitle("Upcoming event: " + name)
                .setContentText(desc).setWhen(time).setVibrate(vibrate)
                .setSound(alarmSound);

        Intent viewEvent = new Intent(context, ViewEvent.class);
        viewEvent.putExtras(intent.getExtras());
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                viewEvent, 0);
        builder.setContentIntent(contentIntent);

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(id++, builder.build());
    }
}
Was it helpful?

Solution

I found where the problem was ...as we can see in the documentation, we need to add PendingIntent.FLAG_UPDATE_CURRENT for broadcast receiver as well, so the full implementation looks like

        Intent viewEvent = new Intent(context, ViewEvent.class);
        viewEvent.putExtras(intent.getExtras());
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                viewEvent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top