Ad

Send Data To Activity When Clicked On Notification?

i am opening a activity when notification is clicked . notification is activated from a service class .i want to send data to new activity opened from notification i am using intent1.putExtra("lable",lable); but in new activity it is giving me nullpointer exeption.

 intent1 = new Intent(this.getApplicationContext(), simplestop.class);
            intent1.putExtra("lable",lable);

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0); 
Notification mNotify  = new Notification.Builder(this)
            .setContentTitle("title" + "!")
            .setContentText("Click me!")
            .setSmallIcon(R.drawable.ic_launcher_background)
                            .setContentIntent(pIntent)
            .setAutoCancel(true)
            .build();
Ad

Answer

I use this code for send data to activity.

Intent intent = new Intent(this, MainActivity.class)
                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("from", "notification");
                    pIntent = PendingIntent.getActivity(this, 3, intent,
                            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

May be you have problem in setting flag=0 or try to change request code=0 to any other integer.

Ad
source: stackoverflow.com
Ad