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

时间:2016-05-26 09:41:52

标签: 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);
    }

1 个答案:

答案 0 :(得分:0)

您应该为第一个通知设置.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);

我希望这会有所帮助。