Yashwanth Kumar
Yashwanth Kumar

Reputation: 29121

Intent extras not received

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.

Upvotes: 3

Views: 2023

Answers (1)

Cruceo
Cruceo

Reputation: 6824

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
}

Upvotes: 6

Related Questions