分组 Android 条通知

Grouping Android Notifications

以下代码在创建通知及其语音和操作方面对我来说工作正常。

但看起来 .setGroup(GROUP_KEY_Fonix) 不起作用,因为通知没有分组在一起,我是不是漏掉了什么!

public int notificationID = 0;
final static String GROUP_KEY_Fonix = "fonix_notification";

private void Notify(String notificationTitle, String notificationMessage){
    notificationID++;

    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

// intent triggered, you can add other intent for other actions
    Intent intent = new Intent(MainActivity.this, NotificationView.class);
    PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

// Build the notification, setting the group appropriately
    Notification notif = new NotificationCompat.Builder(this)
            .setGroup(GROUP_KEY_Fonix)
            .setContentTitle("New note: " + notificationTitle)
            .setContentText(notificationMessage)
            .setSmallIcon(R.drawable.ic_stat_new_message)
            .setContentIntent(pIntent)
            .setSound(soundUri)
            .addAction(R.drawable.ic_stat_new_message, "View", pIntent)
            .addAction(0, "Remind", pIntent)
            .build();

// Issue the notification
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);

    // hide the notification after its selected
    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(notificationID, notif);
}

此外,您应该创建群组摘要通知。为此,只需使用此代码:

Notification notificationSummary = new NotificationCompat.Builder(this)
        //Set content
        .setGroup(GROUP_KEY_Fonix)
        .setGroupSummary(true)
        .build();

notificationManager.notify(notificationSummaryId, notificationSummary);

了解更多here.