1

I have a GCMIntentService where I am setting details for the intent and all when the user clicks on the notification. My code snippet relevant to this is below:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, CustomTabActivity.class);
if (noteId != null && value != null) {
    notificationIntent.putExtra("noteId", noteId);
    notificationIntent.putExtra("value", value);
}

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification updateComplete = new NotificationCompat.Builder(context)
        .setContentTitle(title)
        .setContentText(msg)
        .setTicker(title)
        .setWhen(System.currentTimeMillis())
        .setContentIntent(contentIntent)
        .setDefaults(Notification.DEFAULT_SOUND)
        .setAutoCancel(true)
        .setSmallIcon(R.drawable.icon)
        .build();

notificationManager.notify(100, updateComplete);

Whenever CustomTabActivity.class is opened, the getExtras() call is always null. Why is it that I am not able to get the values from intent?

I read the following for this and was not able to solve it:

Pass a String from one Activity to another Activity in Android

Android - how can I send a GCM push notification with instructions of which activity to load?

Community
  • 1
  • 1
KVISH
  • 12,923
  • 17
  • 86
  • 162

2 Answers2

1

I found the bulk of the problem here. Turns out that since I had singleTop for the activity, the intent from getIntent() was the old one. This code works:

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.getExtras() != null) {
        String noteId = intent.getExtras().getString("noteId");
        String value = intent.getExtras().getString("value");
    }
}

onNewIntent gets called when the user clicks the notification.

KVISH
  • 12,923
  • 17
  • 86
  • 162
-1

Try This :

private static int count = 1;

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            PendingIntent contentIntent = PendingIntent.getActivity(mContext, (int)(Math.random() * 100), new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);

 long when = System.currentTimeMillis();

Notification notification = new Notification(icon, gcmData.getString("message"), when);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notification.setLatestEventInfo(mContext, gcmData.getString("type"), intent.getExtras().getString("message"), contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(count, notification);
            count = count + 1;
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77