【问题标题】:Android / OneSignal - Know if the clicked notification was groupedAndroid / OneSignal - 知道点击的通知是否被分组
【发布时间】:2018-07-18 15:23:30
【问题描述】:

所以这个问题是不言自明的。我想知道单击的通知是分组的还是单个通知。在此基础上,将启动一个 Activity。

例如,如果通知被分组,我想启动“所有消息”活动,如果没有,它会简单地启动聊天活动。

这是我当前代码的样子:

public class NotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {

    private Application application;
    String message;

    public NotificationOpenedHandler(Application application) {
        this.application = application;
    }

    @Override
    public void notificationOpened(OSNotificationOpenResult result) {

        // Get custom data from notification
        JSONObject data = result.notification.payload.additionalData;


        message = data.optString("message");
        startApp(message);
    }

    private void startApp(String text) {

        Intent intent;

        SharedPreferences sharedPreferences = application.getSharedPreferences("appdata", Context.MODE_PRIVATE);
        if (sharedPreferences.getInt("pending_notifications", 1) > 1) {
            intent = new Intent(application, DemoActivity.class);
        } else {
            intent = new Intent(application, ReadLetterActivity.class);
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("pending_notifications", 0);
        editor.apply();
        intent.putExtra("message", text);
        startActivity(intent);
    }

}

如您所见,我尝试使用 SharedPreferences,但没有达到标准。

结束,我想知道是否有办法(本机或 OneSignal 提供)知道通知是否已分组。谢谢。

【问题讨论】:

  • 据我所知,通知何时被分组...如果我点击它,通知将打开以查看组中的通知。我知道 onesignal 有一个 android_group 选项,我很确定他们提供了一种方法来知道有多少通知给用户......你可以使用这个 int。

标签: android push-notification notifications onesignal


【解决方案1】:

没关系。找到了出路。这可以完美区分单个通知和分组通知,即使来自组的通知是单独打开的。

@Override
    public void notificationOpened(OSNotificationOpenResult result) {

        if (result.notification.groupedNotifications == null) {

            // if the clicked notification was single/clicked separately from a group
            Toast.makeText(getApplicationContext(), "Single notification", Toast.LENGTH_LONG).show();
        } else {

            // if the clicked notification was grouped
            Toast.makeText(getApplicationContext(), "Grouped notification quantity: " + String.valueOf(result.notification.groupedNotifications.size()), Toast.LENGTH_LONG).show();
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    相关资源
    最近更新 更多