我正在尝试在我的应用程序上实现一个 InboxStyle 通知,让人们看到他们所有的最后通知,但我找不到更新最后一个通知的方法,即使我使用它,它也会被新的通知覆盖相同的 NOTIF_ID。
我的实际代码:
private void sendNotification(String notif, String[] args) {
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, main_activity.class), 0);
if (notif.equals("NOTIF_0")){
notif = String.format(getString(R.string.NOTIF_0), args[0]);
} else if (notif.equals("NOTIF_1")) {
notif = String.format(getString(R.string.NOTIF_1), args[0]);
} else if (notif.equals("NOTIF_2")) {
notif = String.format(getString(R.string.NOTIF_2), args[0]);
} else if (notif.equals("NOTIF_2_EX")) {
notif = String.format(getString(R.string.NOTIF_2_EX), args[0], args[1], args[2]);
} else if (notif.equals("NOTIF_3")) {
notif = String.format(getString(R.string.NOTIF_3), args[0]);
} else if (notif.equals("NOTIF_3_EX")) {
notif = String.format(getString(R.string.NOTIF_3_EX), args[0], args[1], args[2]);
} else if (notif.equals("NOTIF_3_EX_DIS")) {
notif = String.format(getString(R.string.NOTIF_3_EX_DIS), args[0], args[1], args[2]);
} else if (notif.equals("NOTIF_4")) {
notif = String.format(getString(R.string.NOTIF_4), args[0]);
} else if (notif.equals("NOTIF_5")) {
notif = String.format(getString(R.string.NOTIF_5), args[0]);
} else if (notif.equals("NOTIF_5_EX")) {
notif = String.format(getString(R.string.NOTIF_5_EX), args[0], args[1], args[2]);
} else if (notif.equals("NOTIF_6_EX")) {
notif = String.format(getString(R.string.NOTIF_6_EX), args[0], args[1]);
} else if (notif.equals("NOTIF_31")) {
notif = String.format(getString(R.string.NOTIF_31), args[0]);
}
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(notif);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle(getString(R.string.app_name))
.setContentText(notif);
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(contentIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
mNotificationManager.notify(1, notification);
}
附加问题:有没有办法只显示最近 5 条通知(新通知和上一条通知后的 4 条通知)? 我也在寻找一种显示 BigView 而不是 InboxStyle 的方法,但只有当只有一个通知处于 Activity 状态时,否则通知应显示为列表,是否可以查看是否存在具有相同 ID 的通知并且根据该检查加载一种或另一种样式?在 Internet 上找不到有关它的任何信息。
最佳答案
现在设法以一种丑陋的方式去做。我将最后的通知保存在 SQLite 数据库中,随时检索它们并构建更新的通知。 我在启动应用程序时清理数据库,以便下次用户只会收到新通知。
我不知道这是否是最佳解决方案,但我找不到任何 API 来访问旧通知以从中检索信息,所以我实现了这个解决方案。
关于android - 使用新行更新 InboxStyle 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276650/