Java Android OneSignal通知无法打开“;“已关闭”;应用

Java Android OneSignal通知无法打开“;“已关闭”;应用,java,android,onesignal,Java,Android,Onesignal,我正在使用OneSignal接收通知,如果我的应用程序在后台,我单击通知,应用程序将正常打开,因为我想去请求的活动,没有任何问题! 但如果应用程序没有运行,我的意思是完全关闭,点击通知后,什么也没有发生!应用程序未打开!通知将在不打开应用程序的情况下消失!有什么建议吗 下面是我用来处理OneSignal通知的代码 private class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {

我正在使用OneSignal接收通知,如果我的应用程序在后台,我单击通知,应用程序将正常打开,因为我想去请求的活动,没有任何问题! 但如果应用程序没有运行,我的意思是完全关闭,点击通知后,什么也没有发生!应用程序未打开!通知将在不打开应用程序的情况下消失!有什么建议吗

下面是我用来处理OneSignal通知的代码

 private class NotificationOpenedHandler 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 launchUrl = result.notification.payload.launchURL; // update docs launchUrl
        String body = result.notification.payload.body; // update docs launchUrl

        String customKey;
        String openURL = null;
        Object activityToLaunch = MainActivity.class;

        if (data != null) {
            customKey = data.optString("customkey", null);
            openURL = data.optString("openURL", null);

            if (customKey != null)
                Log.i("OneSignalExample", "customkey set with value: " + customKey);

            if (openURL != null)
                Log.i("OneSignalExample", "openURL to webview with URL value: " + openURL);
        }

        if (actionType == OSNotificationAction.ActionType.ActionTaken) {
            Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);

            if (result.action.actionID.equals("id1")) {
                Log.i("OneSignalExample", "button id called: " + result.action.actionID);
                activityToLaunch = Notifications.class;
            } else
                Log.i("OneSignalExample", "button id called: " + result.action.actionID);
        }
        // The following can be used to open an Activity of your choice.

        Intent toActivity = new Intent(MainActivity.this, Notifications.class);
        toActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);

        toActivity.putExtra("openURL", launchUrl);

        toActivity.putExtra("body", body);

        Log.i("OneSignalExample", "openURL = " + launchUrl);

        //my Code
        String message;
        SharedPreferences sharedPreferences = getSharedPreferences(NOTES, Context
                .MODE_PRIVATE);
        String jsonLink = sharedPreferences.getString(NOTES_LINKS, null);
        String jsonTitle = sharedPreferences.getString(NOTES_TITLE, null);

        if (jsonLink != null && jsonTitle != null) {

            Gson gson = new Gson();
            ArrayList<String> linkList = gson.fromJson(jsonLink, new TypeToken<ArrayList<String>>() {
            }.getType());

            ArrayList<String> titleList = gson.fromJson(jsonTitle, new TypeToken<ArrayList<String>>() {
            }.getType());

            if (linkList.contains(launchUrl)) {
                message = "Notification Exist!";
                Log.i("Notifications","Notification Exist!");

            } else {
                linkList.add(launchUrl);
                titleList.add(body.trim());
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(NOTES_LINKS, new Gson().toJson(linkList));
                editor.putString(NOTES_TITLE, new Gson().toJson(titleList));
                editor.apply();

                Log.i("Notifications","Notification Stored!");

            }
        } else {

            ArrayList<String> linkList = new ArrayList<>();
            ArrayList<String> titleList = new ArrayList<>();
            linkList.add(launchUrl);
            titleList.add(body.trim());
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(NOTES_LINKS, new Gson().toJson(linkList));
            editor.putString(NOTES_TITLE, new Gson().toJson(titleList));
            editor.apply();

            Log.i("Notifications","Stored");

        }
        // startActivity(intent);
        startActivity(toActivity);
    }
}
私有类NotificationOpenedHandler实现一个信号。NotificationOpenedHandler{
//当通过点击通知打开通知时,将触发该命令。
@凌驾
公共作废通知已打开(OSNotificationOpenResult){
OSNotificationAction.ActionType ActionType=result.action.type;
JSONObject data=result.notification.payload.additionalData;
字符串launchUrl=result.notification.payload.launchUrl;//更新文档launchUrl
字符串body=result.notification.payload.body;//更新文档启动URL
字符串自定义键;
字符串openURL=null;
对象activityToLaunch=MainActivity.class;
如果(数据!=null){
customKey=data.optString(“customKey”,null);
openURL=data.optString(“openURL”,null);
if(customKey!=null)
Log.i(“OneSignalExample”,“设置值为“+customkey”的customkey);
if(openURL!=null)
Log.i(“OneSignalExample”,“openURL到webview,URL值:”+openURL);
}
if(actionType==OSNotificationAction.actionType.ActionTaked){
Log.i(“OneSignalExample”,“id为“+result.action.actionID”的按钮被按下);
if(result.action.actionID.equals(“id1”)){
Log.i(“OneSignalExample”,“调用的按钮id:”+result.action.actionID);
activityToLaunch=Notifications.class;
}否则
Log.i(“OneSignalExample”,“调用的按钮id:”+result.action.actionID);
}
//以下内容可用于打开您选择的活动。
意图到活动=新意图(MainActivity.this、Notifications.class);
toActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
putExtra(“openURL”,launchUrl);
toActivity.putExtra(“身体”,身体);
Log.i(“OneSignalExample”、“openURL=“+launchUrl”);
//我的代码
字符串消息;
SharedReferences SharedReferences=GetSharedReferences(注释、上下文
·模式(私人);;
String jsonLink=SharedReferences.getString(NOTES\u LINKS,null);
String jsonTitle=SharedReferences.getString(NOTES\u TITLE,null);
if(jsonLink!=null&&jsonTitle!=null){
Gson Gson=新的Gson();
ArrayList linkList=gson.fromJson(jsonLink,新的TypeToken(){
}.getType());
ArrayList titleList=gson.fromJson(jsonTitle,新的TypeToken(){
}.getType());
if(linkList.contains(launchUrl)){
message=“通知存在!”;
Log.i(“通知”,“通知存在!”);
}否则{
linkList.add(启动URL);
titleList.add(body.trim());
SharedReferences.Editor=SharedReferences.edit();
putString(NOTES_LINKS,new Gson().toJson(linkList));
putString(NOTES_TITLE,new Gson().toJson(titleList));
editor.apply();
Log.i(“通知”,“已存储通知!”);
}
}否则{
ArrayList linkList=新建ArrayList();
ArrayList titleList=新的ArrayList();
linkList.add(启动URL);
titleList.add(body.trim());
SharedReferences.Editor=SharedReferences.edit();
putString(NOTES_LINKS,new Gson().toJson(linkList));
putString(NOTES_TITLE,new Gson().toJson(titleList));
editor.apply();
Log.i(“通知”、“存储”);
}
//星触觉(意向);
起始触觉(toActivity);
}
}

我正在挖掘onesignal通知的另一个问题,发现了这个问题, 我想回答如下:

您在清单中为NotificationOpened.DEFAULT设置了什么值

“启用”将打开您的应用程序

<application>   <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="Enable" /></application>

我以前也遇到过同样的问题,通过创建一个应用程序类并在其中初始化oneSignal,我能够解决这个问题

    public class Application extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // OneSignal Initialization
        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .setNotificationOpenedHandler(new NotificationHandler(this))
                .init();
    }
}
你还需要在你的手拳中加入这些

<application 
   android:name="Application"
   ...>
    <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>