Android Notification - How to bring the activity t

2019-08-21 06:48发布

问题:

In my app, in an activity(AndroidNotification.java) i am doing the following:

1) i am showing a notification when user press home button.

2) when user click the notification i show the app with the activity - AndroidNotification.java

3) i close the notification on notification bar when my app comes to front(that is in onResume method)

The below code working but the problem is the notification call the activity(AndroidNotification.java) again

my question is i do not want to relaunch the activity , the notification should bring the

activity to front from background, it should not call relaunch.

result of the current behavior is activity is called again so that whole functionality is worked again. that is the problem .

my code:

private NotificationManager mNotificationManager;
Notification notifyDetails;
......
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notifyDetails = new Notification(R.drawable.ic_launcher,
            "Notification, Click Me!", System.currentTimeMillis());

    Context context = AndroidNotification.this;
    CharSequence contentTitle = "Notification Test";
    CharSequence contentText = "Get back to Application on clicking me.";

    Intent notifyIntent = new Intent(context, AndroidNotification.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT );

    PendingIntent intent = PendingIntent.getActivity(
            AndroidNotification.this, 0, notifyIntent,0);

    // Set properties to notify object - its title, text
    notifyDetails.setLatestEventInfo(context, contentTitle,contentText, intent);

    // Notify user in status bar
    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);

is anything need to add for the particular activity in AndroidManifest.xml

please help me.

回答1:

i have solved by adding the following in on create method

if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
    // Activity was brought to front and not created, 
    // Thus finishing this will get us to the last viewed activity 
    finish(); 
    return; 
}