简体   繁体   English

推送通知打开一个新活动

[英]Push notification open a new activity

I am using OneSignal push notification service. 我正在使用OneSignal推送通知服务。 When user clicking on the push notification it is open automaticlly a new activity called "Main2Activity.java". 当用户单击推送通知时,它会自动打开一个名为“ Main2Activity.java”的新活动。 When the user clicking on the notification, the code is working and open a new acvtivity. 当用户单击通知时,代码正在工作并打开新的通知。 But if I am sending another push notification, when the user is clicking on the notification the application firstly open the "MainActivity.java". 但是,如果我要发送另一个推送通知,则当用户单击通知时,应用程序首先打开“ MainActivity.java”。 Only when the user click the previus button on the phone, it opening the "MainActicity2.java". 仅当用户单击电话上的previus按钮时,它才会打开“ MainActicity2.java”。 This is my code 这是我的代码

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OneSignal.startInit(this)
                .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
                .init();
    }

    class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
        // This fires when a notification is opened by tapping on it.
        @Override
        public void notificationOpened(OSNotificationOpenResult result) {
            OSNotificationAction.ActionType actionType = result.action.type;
            JSONObject data = result.notification.payload.additionalData;
            String customKey;

            if (data != null) {
                customKey = data.optString("customkey", null);
                if (customKey != null) {
                    Log.i("OneSignalExample", "customkey set with value: " + customKey);
                    Toast.makeText(MainActivity.this,"My Custom Key is:"+customKey,Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
                         intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);

                    startActivity(intent);


                }
            }

        }
    }
}

Thank you! 谢谢!

When you open activity from notification then you put some data which is identify user came from notification 当您从通知中打开活动时,您需要放置一些数据来标识用户来自通知

And when user press back button from mainactivity2 then check, user came from notification or any other place, if came from notification then close app. 当用户从mainactivity2中按下返回按钮然后检查时,用户来自通知或其他任何地方,如果来自通知,则关闭应用程序。

Add below code in your menifest.xml 在menifest.xml中添加以下代码

<activity ..
      android:launchMode= "singleInstance" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM