tags:

views:

836

answers:

2

I have created an activity which sends a number of notifications to status bar. Each notification contains an intent with a bundle. Here is the code:

 String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.icon;     
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, "Test Notification", when);


    Context context = getApplicationContext();      

    Bundle bundle = new Bundle();
    bundle.putString("action", "view");
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.putExtras(bundle);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);     
    mNotificationManager.notify(1, notification);


When user clicks this notifications, I read the bundle string "action" and performs that action. Here is the code:



 Bundle bundle = this.getIntent().getExtras();

    if(bundle != null)
    {
        String action = bundle.getString("action");
            performAction(action)
    }

Everything works as expected. But, when I minimize the app using "arrow" button on device and then press and hold home button and clicks on my app icon the application starts and performs the same last action which have been performed by clicking the last notification. I figured out that when we click the app icon the application starts with last intent triggered by the notification. Can anyone help in this?

A: 

First, "an activity which sends a number of notifications to status bar" is a bad design for a production application. An Activity already has the user interface in front of the user and therefore does not need Notifications. Even if you were doing this from a Service, the vast majority of Android applications should not need more than one Notification at a time.

To your specific problem, you think you are creating several PendingIntents, but you are not. By default, a distinct PendingIntent is only created when the underlying Intent is materially different from the Intent used by another outstanding PendingIntent. Yours only differs by the extras. If you are going to have several outstanding PendingIntents, you will need to have them be on different Intents, where those Intents differ by component, action, data (Uri), or categories.

CommonsWare
Now I have modified the code and now I am sending only one notification to status bar. When I click that notification the respective PendingIntent fires. But still if I minimize the app and restore it again, still it fires the last PendingIntent. I have bound the PendingIntent with the notification and it should only fire, if someone clicks that notification from status bar
If you are updating a single `Notification` and are only changing the extras on the `Intent`, you will need to use `FLAG_UPDATE_CURRENT` on the call to create the `PendingIntent`.
CommonsWare
I want to know that why the Intent fires without clicking the notification? I just minimized the app and restored it, and it fired the last pending intent.
A: 

I have found the solution by myself:

Intent intent = getIntent();
int flags = intent.getFlags();
boolean launchedFromHistory = ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0);

This way we can check where is activity started from.