3

I am showing a notification from a library attached to my project, when clicked on the notification, the notification takes to an Activity (ReceivingActivity). Activity opens after clicking the notification ,but the extras attached to it are not received.

Notification triggering code - I call sendNotification when i receive a gcm message and Notification code is in the library

public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
private void sendNotification(Bundle extras) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);



    Intent redirectIntent = new Intent(this, Class.forName("com.xyz.kljdf.ReceivingActivity"));
            redirectIntent.putExtra("key", "value"); // These extras are not received in ReceivingActivity onCreate(), see the code below




    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            redirectIntent, 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(((Context)this).getResources().getIdentifier("icon", "drawable", getPackageName()))
    .setContentTitle(extras.getString(PushConstants.TITLE))
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(extras.getString(PushConstants.ALERT)))
    .setContentText(extras.getString(PushConstants.ALERT));

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

Checking the extras in the ReceivingActivity...

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_receiving);


        Log.i("extras..... @@@@@@", getIntent().getExtras().toString()); //NPE here


    }

I need help in figuring out how to pass the extras and receive them correctly.

Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
  • https://stackoverflow.com/questions/3168484/pendingintent-works-correctly-for-the-first-notification-but-incorrectly-for-the – CommonsWare Apr 21 '14 at 17:59

1 Answers1

6

Make sure the PendingIntent is actually passing the data when it's called. See this answer for an explanation: https://stackoverflow.com/a/3851414/1426565

For anyone else, if you're positive the intent already exists and is just brought to the front, the new Intent's data won't be passed to it by default. You can override onNewIntent(Intent) in order to get around that:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent); // Set the new Intent's data to be the currently passed arguments
}
Community
  • 1
  • 1
Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • The activity/intent was never open before i send the notification, so i believe this is not the right answer. – Yashwanth Kumar Apr 21 '14 at 17:51
  • My apologies, then. It's usually a common issue. Have you checked this then? http://stackoverflow.com/a/3851414/1426565 – Cruceo Apr 21 '14 at 17:56
  • Thank you, the link you posted has the correct answer, modify your answer to just the link one, so that i can accept it. – Yashwanth Kumar Apr 21 '14 at 18:05
  • Modified to show the correct answer first, but left the onNewIntent() because that's very commonly overlooked with Notifications and PendingIntents – Cruceo Apr 21 '14 at 18:10
  • but this method is not called if app is not in stack and notification activity is on top on stack. kindly help – Saad Bilal Mar 10 '15 at 15:25
  • That's how it should work. Those are 2 different use cases. Just add some code after the setIntent(intent); line to re-setup your Activity based on the new Intent that's passed. Also note, if you don't use FLAG_ACTIVITY_NEW_TASK, you may not be able to trigger the event. – Cruceo Mar 10 '15 at 15:47