[英]Android Create multiple notifications in recycler view
I'm creating an app which tracks when a food item expires and in the adapter class I have an if statement and when the item expiry date is less than 24 hours it displays a notification but when I have more than one item which is about to expire in my recycler view, it only shows the one notification I want it to show each item that is about to expire in separate notifications.我正在创建一个应用程序,用于跟踪食品何时过期,在适配器 class 中我有一个 if 语句,当项目到期日期小于 24 小时时,它会显示通知,但是当我有多个项目即将到期时在我的回收站视图中过期,它只显示一个通知,我希望它在单独的通知中显示每个即将过期的项目。
if (timeLeft > 86400000) {
viewHolder.item_expire_date.setBackgroundColor(Color.parseColor("#3deb34"));
viewHolder.item_expire_date.setText(days + " Days \n" + hours + " Hours\n" + minutes + " Minutes\n" + seconds + " Seconds");
} else if (timeLeft > 0 && timeLeft <= 86400000) {
int reqCode = 1;
showNotification(context, items.getNAME()+ " Urgent expiry date", items.getNAME()+" is about to expire in less than 24 hours", reqCode);
viewHolder.item_expire_date.setBackgroundColor(Color.parseColor("#f55742"));
viewHolder.item_expire_date.setText(days + " Days \n" + hours + " Hours\n" + minutes + " Minutes\n" + seconds + " Seconds");
} else {
viewHolder.item_expire_date.setBackgroundColor(Color.parseColor("#000000"));
viewHolder.item_expire_date.setText("\"Re-enter Date\"");
}
Creating Notification创建通知
public void showNotification(Context context, String title, String message, int reqCode) {
String CHANNEL_ID = "channel_name";//
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Channel Name";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(reqCode, notificationBuilder.build());
}
The reqCode
that you're using for each notification needs to be unique.您用于每个通知的reqCode
必须是唯一的。 A simple solution would be using your timeLeft
, assuming that is unique for each food item.一个简单的解决方案是使用你的timeLeft
,假设每个食物都是独一无二的。
This can be verified by looking at the comment above the .notify
method, specifically:这可以通过查看.notify
方法上方的注释来验证,特别是:
If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.如果您的应用程序已经发布了具有相同 id 的通知并且尚未取消,它将被更新的信息替换。
Source code:源代码:
/**
* Post a notification to be shown in the status bar. If a notification with
* the same id has already been posted by your application and has not yet been canceled, it
* will be replaced by the updated information.
*
* @param id An identifier for this notification unique within your
* application.
* @param notification A {@link Notification} object describing what to show the user. Must not
* be null.
*/
public void notify(int id, Notification notification)
{
notify(null, id, notification);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.