notification intent opens the apps main activity i

2019-09-04 07:06发布

问题:

I have a notification service running that will create a notification for the user and if the user clicks the notification it opens an activity to display the message, the method I am using to create the notification is :

public void createNotificationMsg(String name,String doing, boolean persistent){

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
            0, new Intent(getApplicationContext(), MessageNotificationActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                            | Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra("message", doing),
            PendingIntent.FLAG_CANCEL_CURRENT);


    NotificationCompat.Builder notificationBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.drawable.my_logo_96)
                    .setContentTitle(name)
                    .setContentText(doing)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setContentIntent(pendingIntent)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setColor(Color.parseColor("#1aadce"));

    if (persistent){
        notificationBuilder.setOngoing(true);
    }
    else {
        notificationBuilder.setOngoing(false);
    }

    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(getApplicationContext());

    Notification notification = notificationBuilder.build();

    notificationManager.notify(0, notification);
}

the MessageNotificationActivity is using the "android:style/Theme.Dialog" to make it look like a dialog now the thing is that when I click on the notification everything goes well and the activity is opened like a dialog with nothing in the background but if the app is paused and is in background when I click the notification it brings the apps Main activity to the front and then opens the MessageNotiicationActivity on top of it. How can I make the notification not bring the main activity to front and only intent to the specefied activity?

回答1:

The reason this happens is that your dialog-themed Activity has the same "task affinity" as the other activities in your app, so when you click on the notification, Android looks for an appropriate task in which to launch the dialog-themed Activity. If your app is in the background, Android will see it as a task with the same "task affinity" as the dialog-themed Activity and bring that task to the foreground and launch the dialog-themed Activity into that task.

To prevent this from happening, you need to add android:taskAffinity="" to the <activity> declaration in the manifest for your dialog-themed Activity. This tells Android to launch the dialog-themed Activity into a new task, and not into your app's task.



回答2:

Try adding the flag Intent.FLAG_ACTIVITY_CLEAR_TASK to your intent. This will clear any other activities in the task.