Java 打开重复活动的通知

Java 打开重复活动的通知,java,android,xml,android-notifications,onesignal,Java,Android,Xml,Android Notifications,Onesignal,我已经实现了OneSignal向我的Android应用程序发送推送通知。我主要使用他们的RESTAPI实现了所有东西,在我的应用程序中没有做太多的代码工作。我已经遵循了(并复制了代码,因为不需要做太多更改) 问题是,当我在前台使用我的应用程序(我应用程序的主活动)并发送通知(应该启动updateactivity)时,该活动将在当前活动的顶部打开。现在,即使当我从我的应用程序菜单点击退出时,它也会关闭顶部的活动,但是底部的活动仍然存在。因此,基本上,用户必须退出应用程序两次 我已在清单中将所有活动

我已经实现了OneSignal向我的Android应用程序发送推送通知。我主要使用他们的RESTAPI实现了所有东西,在我的应用程序中没有做太多的代码工作。我已经遵循了(并复制了代码,因为不需要做太多更改)

问题是,当我在前台使用我的应用程序(我应用程序的主活动)并发送通知(应该启动updateactivity)时,该活动将在当前活动的顶部打开。现在,即使当我从我的应用程序菜单点击退出时,它也会关闭顶部的活动,但是底部的活动仍然存在。因此,基本上,用户必须退出应用程序两次

我已在清单中将所有活动声明为

以下是OneSignal需要并使用的ApplicationClass.java:

package com.application;

import android.app.Application;
import com.onesignal.OneSignal;

@SuppressWarnings("unused")

public class ApplicationClass extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();

        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();
    }

}
下面是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.application"
    android:installLocation="auto">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name=".ApplicationClass"
        android:allowBackup="true"
        android:fullBackupContent="@xml/backup_descriptor"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="AllowBackup">

        <activity
            android:name=".SplashActivity"
            android:theme="@style/Splash"
            android:launchMode="singleTask">

            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="domain.tld"
                    android:scheme="http"
                    android:pathPattern="/*"/>
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="domain.tld"
                    android:scheme="https"
                    android:pathPattern="/*"/>
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="www.domain.tld"
                    android:scheme="http"
                    android:pathPattern="/*"/>
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="www.domain.tld"
                    android:scheme="https"
                    android:pathPattern="/*"/>
            </intent-filter>
        </activity>

        <activity
            android:name=".SplashActivity2"
            android:theme="@style/Splash"
            android:launchMode="singleTask">

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="domain.tld"
                    android:scheme="http"
                    android:pathPrefix="/folder"/>
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="domain.tld"
                    android:scheme="https"
                    android:pathPrefix="/folder"/>
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="www.domain.tld"
                    android:scheme="https"
                    android:pathPrefix="/folder"/>
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="www.domain.tld"
                    android:scheme="http"
                    android:pathPrefix="/folder"/>
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTask"/>

        <activity
            android:name=".UpdateActivity"
            android:launchMode="singleTask"/>

        <activity
            android:name=".ErrorActivity"
            android:launchMode="singleTask"/>
    </application>

</manifest>


那么,该如何做才能使由于通知而打开的新活动类似于合并到当前活动中,以便在用户尝试退出时,他/她可以一次性退出应用程序?

您可以尝试重新编写退出按钮功能,以检查应用程序的任何其他实例是否正在运行,然后一次性退出应用程序。添加clearTop等功能可修复此问题。我还没有在这里更新它。使用clearTop不会优雅地保存,这样你将有更多的控制权,也可以保存数据。