添加总览通知后,Android组通知不会发出警报声(Android 6.0)

9

我正在尝试让本地通知像Hangouts一样工作。每次收到新的文本消息时,我希望会出现一个提醒通知。但是当有两个或更多未读通知时,我希望在Android通知栏中显示摘要通知。像这里描述的那样,似乎通过组合堆叠通知并添加组摘要可以实现这一目标。以下代码在Android 5.0和5.1上对我有效,但在Android 6.0上,当该组的摘要通知存在时,本地通知不会闹钟/以弹出视图显示。因此,只显示最初的通知。

public class MainActivity extends AppCompatActivity {

private Button _button = null;
final static String GROUP_KEY_EMAILS = "group_key_emails";
private int messageNum = 1;

private void CreateNotification() {
    // Build the notification, setting the group appropriately
    Notification headsUpNotification = new NotificationCompat.Builder(this)
            .setContentTitle("Title")
            .setContentText("New Message" + messageNum)
            .setSmallIcon(R.drawable.pngreceivedtextmessage)
            .setGroup(GROUP_KEY_EMAILS)
            .setPriority(Notification.PRIORITY_HIGH)
            .setDefaults(Notification.DEFAULT_ALL)
            .build();

    // Issue the notification
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);
    notificationManager.notify(messageNum, headsUpNotification);

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
            R.drawable.pngreceivedmessageicon);

    if(messageNum > 1) {
        // Create a summary notification since we have more than 1
        Notification summaryNotification = new NotificationCompat.Builder(this)
                .setContentTitle("Summary")
                .setNumber(messageNum)
                .setSmallIcon(R.drawable.pngreceivedtextmessage)
                .setLargeIcon(largeIcon)
                .setGroup(GROUP_KEY_EMAILS)
                .setGroupSummary(true)
                .build();

        notificationManager.notify(0, summaryNotification);
    }

    messageNum++;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    _button = (Button) findViewById(R.id.button);
    _button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CreateNotification();
        }
    });
}

我正在针对SDK 23进行开发,尝试了许多不同的组合但都没有起作用。有人知道如何生成摘要通知并仍然使弹出式通知(heads up notifications)工作吗?


你是否为 Android M 请求了运行时权限? - Jagjit Singh
我没有想到我在做任何需要权限的事情。我需要哪个权限? - Alex
2个回答

3
在Android 6.0中,通知/摘要通知的显示方式稍有改变。如果您反复发送大量通知,则在连续通知之间持续时间非常短的情况下,通知系统不会显示悬浮窗口。
为了确认这一点,在该if语句内的第二个通知构建器中添加以下代码(此代码缺失,无法将其显示为高优先级并带有警报):
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)

然后每隔几秒钟测试发出通知(让前一个悬浮通知消失,然后等待几秒钟)。如果您正确执行此操作,则应在每个通知的悬浮显示中看到警报。

如果您开始重复发送通知并且非常快,悬浮通知将不会显示。请等待1-2分钟,然后再次发出通知,然后它将显示在悬浮通知中。


1
谢谢,我已经更改了代码以每次创建新的通知文本,但是悬浮通知仍然无法正常工作。如果我将优先级和默认值添加到摘要通知中,则摘要通知将显示为悬浮通知。这不是我想要的,我只想更新摘要但将常规的“新消息”通知显示为悬浮通知。 - Alex
首先创建摘要。 - conca

1
我猜测您在调用notificationManager.notify(0, summaryNotification);时重用了硬编码的0作为notificationId,因此系统可能会忽略进一步的更新。尝试使用递增的id代替:
在您的MainActivity类中,
  1. 添加一个字段private static int lastNotificationId = 0;
  2. 使用动态id:notificationManager.notify(MainActivity.lastNotificationId++, summaryNotification);
我没有在您的代码中尝试过,但我记得我过去也遇到了类似的问题,它是通过这种方式解决的。

1
谢谢,但每次为摘要通知创建新的ID并没有解决问题。它最终会在通知栏中创建一个新的独立通知,这迫使我手动取消以前的通知。此外,我已经为悬浮通知生成了新的通知ID。 - Alex
然后,您可以通过代码取消通知,而不是手动由用户取消,使用notificationManager.cancel(lastNotificationId)在发布最新通知之前。它是否放置在您想要的通知栏中的正确位置?现在是否正确触发了Heads-up?(除了旧的还在那里..) - Andrei Mărcuţ
不,提醒通知仍然没有正确触发。 - Alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接