0

I am trying to pass value of my push notification to my activity. But it always returns null.

Here is how I am trying to pass extra string:

Intent notificationIntent = new Intent(context, Register.class);
notificationIntent.putExtra("NotificationMessage", new_key.toString());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;

// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);      

Here is my activity where I am trying to get this extra string.

Intent intent = getIntent();
String msg = intent.getStringExtra("NotificationMessage");
Toast.makeText(this, "Hello"+msg, Toast.LENGTH_LONG).show();

This toast always show me " Hello null ". Can anyone please help?

Thanks in advance.

1 Answers1

0

Add PendingIntent to your notification via .setContentIntent(). It will trigger intent when notification is clicked.

Notification notification = new NotificationCompat.Builder(context)
        ...
        .setContentIntent(intent)
        ...
        .build();
Boban S.
  • 1,662
  • 13
  • 16