views:

2145

answers:

3

Hi

I can find a way to send parameters to my activity from my notification.

I have a service that creates a notification. When the user clicks on the notification i want to open my main activity with some special parameters. E.g an item id, so my activity can load and present a special item detail view. More spesific, im downloading a file, and when the file is downloaded i want the notification to have an intent that when clicked it opens my activity in a special mode. I have tried to use putExtra on my intent, but cant seem to extract it, so i think im doing it wrong.

Code from my service that creates the Notification:

        // construct the Notification object.
     final Notification notif = new Notification(R.drawable.icon, tickerText, System.currentTimeMillis());


 final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
 contentView.setImageViewResource(R.id.image, R.drawable.icon);
 contentView.setTextViewText(R.id.text, tickerText);
 contentView.setProgressBar(R.id.progress,100,0, false);
 notif.contentView = contentView;  

 Intent notificationIntent = new Intent(context, Main.class);
 notificationIntent.putExtra("item_id", "1001"); // <-- HERE I PUT THE EXTRA VALUE
 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
 notif.contentIntent = contentIntent;

    nm.notify(id, notif);

Code from my Activity that tries to fetch the extra parameter from the notification:

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

    setContentView(R.layout.main);


    Bundle extras = getIntent().getExtras();
    if(extras != null){
        Log.i( "dd","Extra:" + extras.getString("item_id") );
    }

The extras is always null and I never gets anything into my log.

Btw... the onCreate is only run when my activity starts, if my activity is already started I also want to collect the extras and present my activity acording to the item_id I recieve.

Any ideas?

+2  A: 

Hello,

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 to 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.

Lucas S.
and to answer the user's question about extras being null: You have to call `PendingIntent.getActivity()` with the flag `PendingIntent.FLAG_UPDATE_CURRENT`, otherwise the same extras will be reused for every notification.
Matthias
A: 

Thanks, that seems to be something in the right direction.

I manage to pass an extra String to my activity now, but it is the same value all the times, even the extra is set diffrent every time. Like the value is never overridden.

   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);
    }

Now, the item.getUsername() returns diffrent username for all my notifications, but when i get it from the Extra it is always the same.

And the creation of the intent.

    Intent notificationIntent = new Intent(context, Main.class);
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
 notificationIntent.putExtra("net.dbg.android.fjol", item.getUsername()); // <-- HERE I PUT THE EXTRA VALUE
 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
 notif.contentIntent = contentIntent;

Any idea why this happens?

PHP_Jedi
Example: im downloading 3 files, for each download i create a notification with downloadstatus. When the user click a notification i want my activity to display more file details for that file, e.g thumbnail, filesize, resolution etc... and to do this, all my notification needs an unique extra to pass to my activity to identify the file.
PHP_Jedi
I tried using the FLAG_UPDATE_CURRENT on the pending intent, but this only resultet in all my nitifications got the value of the last notification created. Im really looking in the blind here... PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PHP_Jedi
A: 

After reading some email-lists and other forums i found that the trick seems to add som unique data to the intent.

like this:

   Intent notificationIntent = new Intent(Main.this, Main.class);
   notificationIntent.putExtra("sport_id", "sport"+id);
   notificationIntent.putExtra("game_url", "gameURL"+id);

   notificationIntent.setData((Uri.parse("foobar://"+SystemClock.elapsedRealtime())));

I dont understand why this needs to be done, It got something to do with the intent cant be identified only by its extras...

PHP_Jedi