Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 点击FCM NotitAction后,如何在应用程序不在后台时重新打开应用程序_Android_Kotlin_Notifications_Firebase Cloud Messaging - Fatal编程技术网

Android 点击FCM NotitAction后,如何在应用程序不在后台时重新打开应用程序

Android 点击FCM NotitAction后,如何在应用程序不在后台时重新打开应用程序,android,kotlin,notifications,firebase-cloud-messaging,Android,Kotlin,Notifications,Firebase Cloud Messaging,我正在使用FCM向我的应用程序发送通知,但有一个问题。如果我的用户正在使用应用程序,只需按home按钮(而不是back键)退出应用程序并收到通知,然后按通知,我的应用程序将重新打开。如果用户按下后退按钮,他/她将退出重新打开的应用程序,他/她将被引导到按下主页按钮之前的应用程序的最后状态。(换句话说,用户应按下后退按钮两次,一次用于退出重新打开的应用程序,另一次用于在后台退出应用程序) 如果应用程序处于后台,我如何通过点击通知来加载应用程序的最后状态,如果不在后台,如何重新打开应用程序 编辑:

我正在使用FCM向我的应用程序发送通知,但有一个问题。如果我的用户正在使用应用程序,只需按home按钮(而不是back键)退出应用程序并收到通知,然后按通知,我的应用程序将重新打开。如果用户按下后退按钮,他/她将退出重新打开的应用程序,他/她将被引导到按下主页按钮之前的应用程序的最后状态。(换句话说,用户应按下后退按钮两次,一次用于退出重新打开的应用程序,另一次用于在后台退出应用程序)

如果应用程序处于后台,我如何通过点击通知来加载应用程序的最后状态,如果不在后台,如何重新打开应用程序

编辑:

我将“onMessageReceived”更改为下面的代码,但没有任何更改:


class MyFireBase() : FirebaseMessagingService(){

    override fun onNewToken(p0: String) {
        super.onNewToken(p0)
    }

    override fun onMessageReceived(p0: RemoteMessage) {
        try {

            val intent = Intent()
            intent.action = "ACTION_STRING_ACTIVITY"
            intent.putExtra("category", p0.data["category"])
            sendBroadcast(intent)


            val notificationIntent = Intent(baseContext, MainActivity::class.java)
            notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
             PendingIntent.getActivity(
                baseContext,
                1,
                notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )

            super.onMessageReceived(p0)
        } catch (e: Exception) {
            e.printStackTrace()
        }


    }

}


如果您使用Firebase云消息传递向应用程序发送通知,则意味着您可以创建自己的通知。然后,您只需为您的
意图设置适当的
标志

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    try {
        Intent notificationIntent = new Intent(context, MainActivity.class);
        //Intent.FLAG_ACTIVITY_NEW_TASK Launches a new instances of your Activity. 
        //So if there is already one in background, there will be two instances.
        //Intent.FLAG_ACTIVITY_CLEAR_TOP is what you want, it resumes the last
        //session if there is any, or launches a new instance if there is no instance in background
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, requestID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //do the rest
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

我通过改变活动的启动模式解决了这个问题,所以在AndroidManifest中,我为main活动设置了
launchMode=singleTask

共享您的
pendingent
代码。我没有这样的设置code@SajjadI我认为您无法更改此行为,因为您显然正在使用Firebase应用程序内消息传递,它会自动创建和处理通知。为了能够改变这种行为,您需要使用Firebase云消息传递,而Firebase云消息传递又需要一台服务器通过Firebase发送通知。我正在使用FCM并通过自己的服务器获取通知,现在,我该如何处理它呢?@SajjadI使用了这些代码,但没有任何更改。请检查我的编辑。同时使用
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
并告诉结果。