Android:分组通知和摘要仍在4.4及以下单独显示

Ant*_*hyn 37 android android-notifications android-wear-notification

我想在Android Wear上实现堆叠通知为此,我为每个"项目"创建了1个摘要通知和N个单独通知.我只想在手机上显示摘要.这是我的代码:

private void showNotifications() {
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    showNotification1(notificationManager);
    showNotification2(notificationManager);
    showGroupSummaryNotification(notificationManager);
}

private void showNotification1(NotificationManager notificationManager) {
    showSingleNotification(notificationManager, "title 1", "message 1", 1);
}

private void showNotification2(NotificationManager notificationManager) {
    showSingleNotification(notificationManager, "title 2", "message 2", 2);
}

protected void showSingleNotification(NotificationManager notificationManager,
                                      String title,
                                      String message,
                                      int notificationId) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setGroupSummary(false)
            .setGroup("group");
    Notification notification = builder.build();
    notificationManager.notify(notificationId, notification);
}

private void showGroupSummaryNotification(NotificationManager notificationManager) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Dummy content title")
            .setContentText("Dummy content text")
            .setStyle(new NotificationCompat.InboxStyle()
                    .addLine("Line 1")
                    .addLine("Line 2")
                    .setSummaryText("Inbox summary text")
                    .setBigContentTitle("Big content title"))
            .setNumber(2)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setCategory(Notification.CATEGORY_EVENT)
            .setGroupSummary(true)
            .setGroup("group");
    Notification notification = builder.build();
    notificationManager.notify(123456, notification);
}
Run Code Online (Sandbox Code Playgroud)

这在Android 5.1上运行得很好,只有摘要显示在手机的通知栏中:

在此输入图像描述

但在Android 4.4上,它还会显示个别通知1和2:

在此输入图像描述

在这两种情况下,Android Wear上的通知都会根据需要显示在堆栈中.如何让Android 4.4仅在通知栏中显示摘要通知?

Ant*_*hyn 19

通过使用修复此问题

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
Run Code Online (Sandbox Code Playgroud)

代替

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Run Code Online (Sandbox Code Playgroud)

并在相应的方法签名中用NotificationManagerCompat替换NotificationManager.

  • 这适用于Android 4.4,但不适用于4.0.4. (2认同)