Android - 对收到的推送通知进行分组,如 Gmail

Posted

技术标签:

【中文标题】Android - 对收到的推送通知进行分组,如 Gmail【英文标题】:Android - Grouping Received Push Notifications like Gmail 【发布时间】:2020-05-24 02:24:58 【问题描述】:

我想将收到的多个推送通知分组到我的应用程序(例如 Gmail)。 下图将描述要求。

我浏览过很多教程,包括开发者网站https://developer.android.com/training/notify-user/group

但没有运气。所有收到的通知都显示为单独的通知。

下面是我的代码 sn-p。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
    super.onMessageReceived(remoteMessage);
    // put the new message onto the event bus so that interested activities can subscribe to it
    // TODO[tim] we should probably look at the content of the message and send different events
    // onto the bus for different message types.

    int notificationId = (int) SystemClock.currentThreadTimeMillis();

    createNotificationChannel();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, ANDROID_CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher) // notification icon
            .setContentTitle(getTitle()) // title
            .setContentText("Vyn processed and ready to play") // body message
            .setContentIntent(getVynPlayIntent(getIntentExtraValues(), notificationId))
            .setAutoCancel(true) // clear notification when clicked
            .setGroup(GROUP_KEY_VYNS)
            .setGroupSummary(true)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    if (builder != null) 
        getManager().notify(notificationId, builder.build());
    


private static final String ANDROID_CHANNEL_ID = "notifications.ANDROID_CHANNEL_ID";
private static final String GROUP_KEY_VYNS = "notifications.GROUP_KEY_VYNS";
private void createNotificationChannel() 
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
        CharSequence name = "channel_name";
        String description = "channel_description";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(ANDROID_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        getManager().createNotificationChannel(channel);
    


private NotificationManagerCompat mManagerCompat;
public NotificationManagerCompat getManager() 
    if (mManagerCompat == null) 
        mManagerCompat = NotificationManagerCompat.from(mContext);
    
    return mManagerCompat;

请帮我找出这段代码中遗漏的地方,如果你能确定的话。

【问题讨论】:

【参考方案1】:

从外观上看,每条通知你都打电话给.setGroupSummary(true)

只有实际的摘要通知应该设置它。孩子们应该忽略它或将其设置为false

组摘要是通知未展开时显示的内容(您问题中的第一个屏幕截图)。所有“儿童”通知都应该是 .setGroupSummary(false) 或者根本没有设置(省略它)

你需要确保你有一个整体的小组摘要,并用自己的 ID 打电话给notify

来自this developer.android article

//use constant ID for notification used as group summary
int SUMMARY_ID = 0;
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";

Notification newMessageNotification1 =
    new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notify_email_status)
        .setContentTitle(emailObject1.getSummary())
        .setContentText("You will not believe...")
        .setGroup(GROUP_KEY_WORK_EMAIL) //**Note here that this is a "child" and does not call setGroupSummary(true)**
        .build();

Notification newMessageNotification2 =
    new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notify_email_status)
        .setContentTitle(emailObject2.getSummary())
        .setContentText("Please join us to celebrate the...")
        .setGroup(GROUP_KEY_WORK_EMAIL) //**Note here that this is a "child" and does not call setGroupSummary(true)**
        .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) //**Note here that this is the "parent" or the actual Group Summary and DOES call setGroupSummary(true)**
        .build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(emailNotificationId1, newMessageNotification1);
notificationManager.notify(emailNotificationId2, newMessageNotification2);
notificationManager.notify(SUMMARY_ID, summaryNotification);

【讨论】:

以上是关于Android - 对收到的推送通知进行分组,如 Gmail的主要内容,如果未能解决你的问题,请参考以下文章

如何处理收到推送通知后恢复应用程序的情况

在android中未收到推送通知

在 Android 中未收到解析推送通知

分组推送通知再次调用 MainActivity 的 OnCreate()。如何处理?

某些 android 设备未收到 GCM 推送通知

处理远程推送通知格式