And*_*i F 8 android android-notifications
我想使用setGroup堆栈通知(如下所述:https://developer.android.com/training/wearables/notifications/stacks.html)基本上,我使用0作为通知ID(始终相同),builder.setGroup("test_group_key")但是新通知总是取代前一个.可能是什么问题呢 ?
码:
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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
它使用NotificationManagerCompat时出现问题,根本不显示通知.
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getContext());
notificationManager.notify(id, notif);
Run Code Online (Sandbox Code Playgroud)
您没有正确使用通知 ID。
“要设置通知以便更新,请通过调用NotificationManager.notify(ID, notification) 来发出通知 ID。要在发出通知后更新此通知,请更新或创建一个NotificationCompat.Builder 对象,构建从中获取一个通知对象,并使用您之前使用的相同 ID 发出通知。”
因此,在您的情况下,如果您想在组中堆叠通知,则需要为每个新通知指定一个新 ID。
| 归档时间: |
|
| 查看次数: |
6759 次 |
| 最近记录: |