Android: Calling IntentService from NotificationManager PendingIntent activity

I have a push notification app. When a push notification appears, the BroadcastReceiver calls the GCMIntentService to set up the notification

mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, HomeActivity.class);
        Intent intent = new Intent(this, AddToCalendarIntentService.class);
        intent.putExtra(JSON_KEY_CLASS_ID, classid);
        intent.putExtra(JSON_KEY_CLASS_NAME, classname);
        intent.putExtra(Config.JSON_KEY_DATE, tgl);
        intent.putExtra(Config.JSON_KEY_TIME_START, timestart);
        intent.putExtra(Config.JSON_KEY_TIME_END, timeend);
        intent.putExtra(Config.JSON_KEY_VENUE, venue);
        Log.d(LOG_TAG, classid + ", " + classname + ", " + venue);
        PendingIntent contentIntent = PendingIntent.getService(this, 0,
                intent, 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_notif)
                        .setContentTitle("Re: " + classname)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(pesan))
                        .setContentText(pesan)
                        .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                        .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
                        .setDefaults(NotificationCompat.DEFAULT_SOUND)
                        .addAction(R.drawable.ic_action_add_person, "Add to Calendar", contentIntent)
                        .setAutoCancel(true)
                        .setTicker("Reschedule Class");

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

      

When the user clicks on "Add to Calendar", he calls the PendingIntent to launch the AddToCalendarIntentService with all parameters.

public class AddToCalendarIntentService extends IntentService {

public AddToCalendarIntentService() {
    super("AddToCalendarIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    final String JSON_KEY_CLASS_NAME = "classname";
    final String JSON_KEY_CLASS_ID = "classid";
    Bundle extra = intent.getExtras();
    String title = extra.getString(JSON_KEY_CLASS_NAME);
    String location = extra.getString(Config.JSON_KEY_VENUE);
    String tgl = extra.getString(Config.JSON_KEY_DATE);
    String[] tglSplit = tgl.split("-");
    int year = Integer.parseInt(tglSplit[0]);
    int month = Integer.parseInt(tglSplit[1]);
    int date = Integer.parseInt(tglSplit[2]);

    String timestart = extra.getString(Config.JSON_KEY_TIME_START);
    String timeend = extra.getString(Config.JSON_KEY_TIME_END);
    String[] start = timestart.split(":");
    String[] end = timeend.split(":");
    int hourStart = Integer.parseInt(start[0]);
    int minStart = Integer.parseInt(start[1]);
    int hourEnd = Integer.parseInt(end[0]);
    int minEnd = Integer.parseInt(end[1]);
    Log.d("INTENT SERVICE", location);
    TaskHelper.addToCalendar(this, "Reschedule: " + title, location, year, month-1, date, hourStart, minStart, hourEnd, minEnd);
    NotificationManager mNotification = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotification.cancel(1);
}
}

      

The code works, but if I get another notification again and add it to the calendar, the parameter in AddToCalendarIntentService will keep the old parameter, ignoring the new one. For example, Indonesia comes first, with both GCMIntentService and AddToCalendarIntentService .

In the second notification, the location is in Singapore, it displays correctly in the GCMIntentService , but when entering AddToCalendarIntentService, it shows Indonesia (it should be Singapore).

Please, help.

Thank.

+3


source to share


2 answers


This is because the system thinks of the same PendingIntent, if you want to have a pair of pending intents you need to differentiate between them.

The easiest way is to provide different requestCodes (2nd parameter of PendingIntent.getService).



You can read more about this here:

http://developer.android.com/reference/android/app/PendingIntent.html

+3


source


As TomerZ pointed out, the system thinks about the same PendingIntent. Add the flag: PendingIntent.FLAG_UPDATE_CURRENT to your pending intent to tell the system to update additional pending intent features.

PendingIntent contentIntent = PendingIntent.getService(this, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

      



FLAG_UPDATE_CURRENT documentation for reference

+1


source







All Articles