android - android 推送通知分组不起作用

标签 android

我想堆叠即将到来的推送通知,但它们不分组。我会有很多通知,它们应该组合在一起。这是我的代码(我按照@ishmaelMakitla 的建议更新了代码):

    Intent intent = new Intent(this, MainActivity.class);
    intent.replaceExtras(extras);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    if (GROUP_ID != null) {

        Log.i("Game", "Game GROUP ID " + "---- " + GROUP_ID);

        int notId = GROUP_ID.equals(GROUP_ADDS) ? NOTIFICATION_ADD_ID : NOTIFICATION_GAME_ID;

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        if (isFirstTime == true) {

            Notification firstNotification = new NotificationCompat.Builder(this)
                    .setContentTitle(messageTitle)
                    .setContentText(messageValue)
                    .setSmallIcon(R.drawable.take_4_free_logo_01)
                    .setGroup(GROUP_ID)
                    .setAutoCancel(true)
                    .setCategory(NotificationCompat.CATEGORY_SOCIAL)
                    .setGroupSummary(true)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(messageValue))
                    .setContentIntent(contentIntent)
                    .build();

            notificationManager.notify(notId, firstNotification);

            Log.i("Game", "Notification ID First time " + "---- " + notId);

            isFirstTime = false;

        } else {

            Notification followUpNotification = new NotificationCompat.Builder(this)
                    .setContentTitle(messageTitle)
                    .setContentText(messageValue)
                    .setCategory(NotificationCompat.CATEGORY_SOCIAL)
                    .setContentIntent(contentIntent)
                    .setSmallIcon(R.drawable.take_4_free_logo_01)
                    .setGroup(GROUP_ID)
                    .build();

            notificationManager.notify(notId, followUpNotification);

            Log.i("Game", "Notification ID Following " + "---- " + notId);
        }

        // notify activity
        Intent intentNewPush = new Intent(Consts.NEW_PUSH_EVENT);
        intentNewPush.replaceExtras(extras);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intentNewPush);
    }

最佳答案

您应该为第一个通知设置 .setGroupSummary(true) ONLY,这将成为一组通知的组摘要(在您的情况下属于组 GROUP_ID)。否则,每个带有 .setGroupSummary(true) 的通知都被视为单独的新通知。这是您可以快速运行的代码(扩展您的代码),如果它按预期工作,请告诉我。

Notification followUpNotification= new NotificationCompat.Builder(mContext)
         .setContentTitle("Follow Up ")
         .setContentText("Follow Up Message")
         .setSmallIcon(R.drawable.free_logo_01)
         .setGroup(GROUP_ID)
         .build();

notificationManager.notify((notId+1), followUpNotification);

希望对您有所帮助。

关于android - android 推送通知分组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37457073/

相关文章:

java - 如何让你的 Android 应用程序只请求一次 super 用户访问权限?

android - 使用 RxAndroid MVP Retrofit 修改回调

android - AsyncTask 完成执行后 ExpandableListView 未更新

android - 更改 Android XML 中的形状颜色

Android fragment 隐藏在操作栏下

android - 在 android Activity 类中动态更改可绘制的开始颜色和结束颜色

android - ImageSaver 中传递给 DngSaver 的 CaptureResult 为 null camera2 api

android - 禁用 ListView 复选框

android - 如何在android Fragment中显示日期选择器

android - 如何在 Android 应用程序中用手指画画……并将其保存到网络