Android-带有setGroup(group)的NotificationCompat.Builder堆叠通知不起作用 [英] Android - NotificationCompat.Builder stacking notifications with setGroup(group) not working

查看:433
本文介绍了Android-带有setGroup(group)的NotificationCompat.Builder堆叠通知不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用setGroup来堆积通知(如下所述: https://developer.android.com/training/wearables/notifications/stacks.html ) 基本上,我使用0作为通知ID(始终相同)和builder.setGroup("test_group_key"),但是新的通知始终会替换之前的通知. 可能是什么问题?

I want to stack notifications using setGroup (as described here: https://developer.android.com/training/wearables/notifications/stacks.html) Basically, I use 0 as notification id (always the same) and builder.setGroup("test_group_key") but a new notification always replaces the previous one. What could be the problem ?

代码:

public BasicNotifier(Context context) {
    super(context);
    notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setSound(alarmSound)
        .setAutoCancel(true);

    stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(getParentActivityClass());

}

public void showNotification(String title, String text, Intent intent, Class cls) {
    if (text.length() > 190)
        text = text.substring(0, 189) + "...";

    mBuilder.setTicker(text).setContentText(text).setContentTitle(title);

    Intent notificationIntent = intent == null ? new Intent() : new Intent(intent);
    notificationIntent.setClass(getContext(), cls);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setGroup("test_group_key");

    Notification notif = mBuilder.build();
    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    notifManager.notify(replaceOnNew ? 0 : nextId++, notif); // replaceOnNew
                                                                // is "true"

    Log.i(TAG, "Notification shown: " + nextId + " = " + title);
}

使用NotificationManagerCompat时,表明存在问题,通知根本没有显示.

It seams there is a problem when using NotificationManagerCompat, the notifications are not being displayed at all.

  NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(getContext());
  notificationManager.notify(id, notif);

推荐答案

您没有正确使用通知ID.

You don't use notification id correctly.

要设置通知以进行更新,请通过调用NotificationManager.notify(ID,notification)来发出通知ID.要在发出通知后对其进行更新,请更新或创建NotificationCompat.Builder对象,从中构建一个Notification对象,然后以与您以前使用的ID相同的方式发出Notification."

"To set up a notification so it can be updated, issue it with a notification ID by calling NotificationManager.notify(ID, notification). To update this notification once you've issued it, update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously."

来自Android开发者

因此,如果您要在组中堆叠通知,则需要为每个新通知指定一个新ID.

So in your case, if you want to stack notification in your group, you need to specify a new id for each new notification.

这篇关于Android-带有setGroup(group)的NotificationCompat.Builder堆叠通知不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆