How to send parameters from a notification-click to an activity? How to send parameters from a notification-click to an activity? android android

How to send parameters from a notification-click to an activity?


Take a look at this guide (creating a notification) and to samples ApiDemos "StatusBarNotifications" and "NotificationDisplay".

For managing if the activity is already running you have two ways:

  1. Add FLAG_ACTIVITY_SINGLE_TOP flag to the Intent when launching the activity, and then in the activity class implement onNewIntent(Intent intent) event handler, that way you can access the new intent that was called for the activity (which is not the same as just calling getIntent(), this will always return the first Intent that launched your activity.

  2. Same as number one, but instead of adding a flag to the Intent you must add "singleTop" in your activity AndroidManifest.xml.

If you use intent extras, remeber to call PendingIntent.getActivity() with the flag PendingIntent.FLAG_UPDATE_CURRENT, otherwise the same extras will be reused for every notification.


I had the similar problem my application displays message notifications.When there are multiple notifications and clicking each notification it displays that notification detail in a view message activity. I solved the problem of same extra parameters is being received in view message intent.

Here is the code which fixed this.Code for creating the notification Intent.

 Intent notificationIntent = new Intent(getApplicationContext(), viewmessage.class);    notificationIntent.putExtra("NotificationMessage", notificationMessage);    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(),notificationIndex,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);    notification.flags |= Notification.FLAG_AUTO_CANCEL;    notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingNotificationIntent);

Code for view Message Activity.

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    onNewIntent(getIntent());}@Overridepublic void onNewIntent(Intent intent){    Bundle extras = intent.getExtras();    if(extras != null){        if(extras.containsKey("NotificationMessage"))        {            setContentView(R.layout.viewmain);            // extract the extra-data in the Notification            String msg = extras.getString("NotificationMessage");            txtView = (TextView) findViewById(R.id.txtMessage);            txtView.setText(msg);        }    }}


Maybe a bit late, but:instead of this:

public void onNewIntent(Intent intent){    Bundle extras = intent.getExtras();    Log.i( "dbg","onNewIntent");    if(extras != null){        Log.i( "dbg", "Extra6 bool: "+ extras.containsKey("net.dbg.android.fjol"));        Log.i( "dbg", "Extra6 val : "+ extras.getString("net.dbg.android.fjol"));    }    mTabsController.setActiveTab(TabsController.TAB_DOWNLOADS);}

Use this:

Bundle extras = getIntent().getExtras();if(extras !=null) {    String value = extras.getString("keyName");}