简体   繁体   中英

Android: how to launch activity from notification when task is already running?

I've inherited this (Xamarin) Android application. It consists of several activities and it also has a BroadcastReceiver that responds to GCM messages. Whenever a GCM message is received, the BroadcastReceiver puts a notification in the notification area (eg "There are promotions available at some store or another" ).

The notification has a PendingIntent attached to it, so that whenever a user clicks it, a certain activity A of the app is launched.

Apparently, the PendingIntent to start an activity requires an Intent.FLAG_ACTIVITY_NEW_TASK flag, which says:

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.

This is indeed the behaviour I'm seeing.

  1. When the app is not active (I never launched it or I pressed the Back button enough to end the task) and I click the notification, Activity A pops up. (good!)

  2. When the app is in the foreground in Activity B and I click the notification, nothing happens (bad! I asked for activity A).

  3. When I have the app open in Activity B, I press the Home button and I click the notification, the app returns to the foreground in the state it was in before I pressed Home. (bad! I asked for activity A).

Question : how can I make it so that activity A is opened even when a task is already running?

This is how I generate my notifications:

public void CreateNotification(Context context, string title, string desc)
{
    var notificationManager = GetSystemService(NotificationService) as NotificationManager;

    var notification = new Notification(Resource.Drawable.logosmallblue, title);
    notification.Flags = NotificationFlags.AutoCancel;
    notification.Sound = RingtoneManager.GetDefaultUri (RingtoneType.Notification);
    notification.Vibrate = new long[]{500,1500,500};

    var perPlaceUniqueId = desc.GetHashCode();

    // workaround for Kitkat bug (https://code.google.com/p/android/issues/detail?id=61850)
    GetNotificationPendingIntent(context, perPlaceUniqueId).Cancel();
    notification.SetLatestEventInfo(context, title, desc, GetNotificationPendingIntent(context, perPlaceUniqueId));

    notificationManager.Notify(perPlaceUniqueId, notification);
}

private PendingIntent GetNotificationPendingIntent(Context context, int requestCode)
{
    var uiIntent = new Intent(context, typeof(MainView));
    uiIntent.PutExtra(MainView.RECOMMENDED_TAG, true);

    return PendingIntent.GetActivity(context, requestCode, uiIntent, PendingIntentFlags.UpdateCurrent);
}

Thanks!

In your PendingIntent you did not use the flag you are asking about. This code:

private PendingIntent GetNotificationPendingIntent(Context context, int requestCode)
{
    var uiIntent = new Intent(context, typeof(MainView));
    uiIntent.PutExtra(MainView.RECOMMENDED_TAG, true);

    return PendingIntent.GetActivity(context, requestCode, uiIntent, PendingIntentFlags.UpdateCurrent);
}

Also, you probably need to add Intent.FLAG_ACTIVITY_REORDER_TO_FRONT . You could also add Intent.FLAG_ACTIVITY_CLEAR_TOP . Something like this:

private PendingIntent GetNotificationPendingIntent(Context context, int requestCode)
{
    var uiIntent = new Intent(context, typeof(MainView));
    uiIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    uiIntent.PutExtra(MainView.RECOMMENDED_TAG, true);

    return PendingIntent.GetActivity(context, requestCode, uiIntent, PendingIntentFlags.UpdateCurrent);
}

Which flags you use depends on how you want the "back" button to work after the user launches the notification.

The documentation says:

When the user leaves a task by pressing the Home button, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack.

from this:

http://developer.android.com/guide/components/tasks-and-back-stack.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM