Intent extras not received

丶灬走出姿态 提交于 2020-01-04 00:29:06

问题


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.


回答1:


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
}


来源:https://stackoverflow.com/questions/23202635/intent-extras-not-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!