如何将Android通知(如whatsapp)分组?因为setGroup对我不起作用

时间:2018-10-22 12:20:15

标签: android notifications

我目前正在Oreo和棒棒糖设备上进行测试。到目前为止,我所做的事情:

final static String GROUP_KEY_NOTIFY = "group_key_notify";
int notificationId0 = 100;
int notificationId1 = 101;
int notificationId2 = 102;
int notificationId3 = 103;

NotificationCompat.Builder builderSummary =
        new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("A Bundle Example")
                .setContentText("You have 3 new messages")
                .setGroup(GROUP_KEY_NOTIFY)
                .setGroupSummary(true);

NotificationCompat.Builder builder1 =
        new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("New Message")
                .setContentText("You have a new message from Kassidy")
                .setGroup(GROUP_KEY_NOTIFY);

NotificationCompat.Builder builder2 =
        new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("New Message")
                .setContentText("You have a new message from Caitlyn")
                .setGroup(GROUP_KEY_NOTIFY);

NotificationCompat.Builder builder3 =
        new NotificationCompat.Builder(this)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("New Message")
                .setContentText("You have a new message from Jason")
                .setGroup(GROUP_KEY_NOTIFY);

NotificationManager notifyMgr =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notifyMgr.notify(notificationId1, builder1.build());
notifyMgr.notify(notificationId2, builder2.build());
notifyMgr.notify(notificationId3, builder3.build());
notifyMgr.notify(notificationId0, builderSummary.build());

我要注意的是,如果有4条或更多条通知发生,则将它们捆绑在一起,但是当少于4条通知时,它们将不捆绑在N上方的android设备中。我阅读了说明文件,并按照它们说的那样做使用setGroup方法并为summaryNotification设置单独的通知对象。但是没有任何东西对我有用。

2 个答案:

答案 0 :(得分:0)

您可以使用this link for reference for creating bundled notification.

示例:

String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";

Notification newMessageNotification = new 
NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
    .setSmallIcon(R.drawable.new_mail)
    .setContentTitle(emailObject.getSenderName())
    .setContentText(emailObject.getSubject())
    .setLargeIcon(emailObject.getSenderAvatar())
    .setGroup(GROUP_KEY_WORK_EMAIL)
    .build();

Notification summaryNotification =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
    .setContentTitle(emailObject.getSummary())
    //set content text to support devices running API level < 24
    .setContentText("Two new messages")
    .setSmallIcon(R.drawable.ic_notify_summary_status)
    //build summary info into InboxStyle template
    .setStyle(new NotificationCompat.InboxStyle()
            .addLine("Alex Faarborg  Check this out")
            .addLine("Jeff Chang    Launch Party")
            .setBigContentTitle("2 new messages")
            .setSummaryText("janedoe@example.com"))
    //specify which group this notification belongs to
    .setGroup(GROUP_KEY_WORK_EMAIL)
    //set this notification as the summary for the group
    .setGroupSummary(true)
    .build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(emailNotificationId1, newMessageNotification);
notificationManager.notify(SUMMARY_ID, summaryNotification);

答案 1 :(得分:0)

您必须使用摘要通知生成器将通知捆绑为一组,以确保正确分组。下面是我的工作代码:

NotificationManager notificationManager;
NotificationCompat.Builder summaryNotificationBuilder;  
int bundleNotificationId = 100;
int singleNotificationId = 100;

private void notificationGroup(Map<String, String> message) {

        //  Create Notification
        String contentTitle = message.get("contentTitle");
        String contentText = message.get("contentText");
        Bitmap bm = BitmapFactory.decodeResource(getApplication().getResources(),
                R.drawable.user3);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Notification Group Key
        String groupKey = "bundle_notification_" + bundleNotificationId;

        //  Notification Group click intent
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.putExtra("notification", "Summary Notification");
        resultIntent.putExtra("notification_id", bundleNotificationId);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, bundleNotificationId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // We need to update the bundle notification every time a new notification comes up
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {            
            if (notificationManager.getNotificationChannels().size() < 2) {
                NotificationChannel groupChannel = new NotificationChannel("bundle_channel_id", "bundle_channel_name", NotificationManager.IMPORTANCE_LOW);
                notificationManager.createNotificationChannel(groupChannel);
                NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(channel);
            }
        }
        summaryNotificationBuilder = new NotificationCompat.Builder(this, "bundle_channel_id")
                .setGroup(groupKey)
                .setGroupSummary(true)
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setSmallIcon(R.drawable.app_logo)
                .setLargeIcon(bm)
                .setAutoCancel(true)
                .setContentIntent(resultPendingIntent);


        if (singleNotificationId == bundleNotificationId)
            singleNotificationId = bundleNotificationId + 1;
        else
            singleNotificationId++;

        //  Individual notification click intent
        resultIntent = new Intent(this, NotificationActivity.class);
        resultIntent.putExtra("notification", "Single Notification");
        resultIntent.putExtra("notification_id", singleNotificationId);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        resultPendingIntent = PendingIntent.getActivity(this, singleNotificationId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
                .setGroup(groupKey)
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setSmallIcon(R.drawable.app_logo)
                .setLargeIcon(bm)
                .setSound(defaultSoundUri)
                .setAutoCancel(true)
                .setGroupSummary(false)
                .setContentIntent(resultPendingIntent);

        notificationManager.notify(singleNotificationId, notification.build());
        notificationManager.notify(bundleNotificationId, summaryNotificationBuilder.build());
}

快乐编码!