通知意图(如果已暂停)在后台打开应用程序主要活动 [英] notification intent opens the apps main activity in background if its paused

查看:93
本文介绍了通知意图(如果已暂停)在后台打开应用程序主要活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个通知服务,该服务将为用户创建一个通知,如果用户单击通知,它将打开一个活动来显示消息,我用来创建通知的方法是:

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

MessageNotificationActivity使用"android:style/Theme.Dialog"使其看起来像一个对话框,现在的事情是,当我单击通知时,一切进展顺利,并且该活动像对话框一样打开,但对话框中没有任何内容.但如果应用程序已暂停并且在我单击通知时处于后台,则它将应用程序的主活动置于最前面,然后在其顶部打开MessageNotiicationActivity.如何使通知不将主要活动放在首位,而仅将意图活动放在指定活动上?

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?

推荐答案

发生这种情况的原因是,以对话框为主题的活动与应用中的其他活动具有相同的任务相似性" ,因此,当您单击通知时,Android会寻找合适的任务来启动以对话框为主题的活动.如果您的应用在后台运行,Android会将其视为与对话框主题的Activity具有相同的任务相似性" 的任务,并将该任务置于前台,然后将对话框主题的Activity启动到那个任务.

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.

为防止这种情况发生,您需要在以对话框为主题的Activity的清单中的<activity>声明中添加android:taskAffinity="".这告诉Android将对话框主题的Activity启动到新任务中,而不是应用程序任务中.

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.

这篇关于通知意图(如果已暂停)在后台打开应用程序主要活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆