Android / OneSignal - 知道点击的通知是不是被分组

Posted

技术标签:

【中文标题】Android / OneSignal - 知道点击的通知是不是被分组【英文标题】: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。 【参考方案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();
        

【讨论】:

以上是关于Android / OneSignal - 知道点击的通知是不是被分组的主要内容,如果未能解决你的问题,请参考以下文章

无法解决:原生 Android 上的 com.onesignal:onesignal:3.12.3

在 android 上未收到 OneSignal 通知,但从 OneSignal 控制台发送

OneSignal 推送通知 |在聊天时禁用它们进行聊天|安卓

Android 从 GCM 迁移到 oneSignal 推送通知

android、unity、onesignal——声音文件放在哪里?

Onesignal通知显示在OneSignal仪表板上,但未在通知栏中显示(android)