Android OneSignal通知无法打开“已关闭”应用程序

时间:2019-01-21 16:49:32

标签: java android onesignal

我正在使用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);
    }
}

2 个答案:

答案 0 :(得分:1)

我只是在寻找一个有关单信号通知的问题,发现了这个问题, 我想回答:

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

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

412,34523,123412

答案 1 :(得分:0)

我以前也遇到过同样的问题,并且能够通过创建应用程序类并在其中初始化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();
    }
}

您还需要将这些添加到您的Manifist中

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