Android / OneSignal-了解点击的通知是否已分组

时间:2018-07-18 15:23:31

标签: android push-notification notifications onesignal

所以这个问题很不言而喻。我想知道单击的通知是分组的还是单个通知。以此为基础,将启动一个活动。

例如,如果将通知分组,则我想启动“所有消息”活动,否则,将简单地启动“聊天活动”。

这是我当前的代码:

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提供)来了解通知是否已分组。谢谢。

1 个答案:

答案 0 :(得分:0)

没关系。找到了出路。即使单独打开一个组中的通知,它也可以完美地区分单个通知和分组通知。

@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();
        }
}