在Android Studio中显示多次每日重复通知
在我的应用中,我想显示尽可能多的日常通知(这是一种医疗提醒)。用户还设置了新通知的时间。但是我的应用程序目前仅显示一份最新通知。您能帮我解决这个问题吗?
我用于创建新通知的代码(sethour
等。以前由用户设置):
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, setHour);
calendar.set(Calendar.MINUTE, setMinute);
calendar.set(Calendar.SECOND, 0);
Intent intent1 = new Intent(ReminderActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ReminderActivity.this, MID,intent1, 0);
AlarmManager am = (AlarmManager) ReminderActivity.this.getSystemService(ReminderActivity.this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
我的alarm> alarmreceiver
:
public class AlarmReceiver extends BroadcastReceiver {
public static int MID = 0;
private static final String CHANNEL_ID = "this.is.my.channelId";
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(MID, 0);
Notification.Builder builder = new Notification.Builder(context);
Notification notification = builder.setContentTitle("Notification")
.setContentText("New Notification")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setContentIntent(pendingIntent).build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(CHANNEL_ID);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//below creating notification channel, because of androids latest update, O is Oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"NotificationDemo",
NotificationManager.IMPORTANCE_DEFAULT
);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(MID, notification);
MID++;
}
}
In my app I want to show as many daily notifications as user wants (it's sort of a medical reminder). User also sets time of the new notification. But my app currently shows only one, latest notification. Could you help me solve this problem?
My code for creating new notification (setHour
etc. are previously set by the user):
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, setHour);
calendar.set(Calendar.MINUTE, setMinute);
calendar.set(Calendar.SECOND, 0);
Intent intent1 = new Intent(ReminderActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ReminderActivity.this, MID,intent1, 0);
AlarmManager am = (AlarmManager) ReminderActivity.this.getSystemService(ReminderActivity.this.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
And my AlarmReceiver
:
public class AlarmReceiver extends BroadcastReceiver {
public static int MID = 0;
private static final String CHANNEL_ID = "this.is.my.channelId";
@Override
public void onReceive(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(MID, 0);
Notification.Builder builder = new Notification.Builder(context);
Notification notification = builder.setContentTitle("Notification")
.setContentText("New Notification")
.setTicker("New Message Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setContentIntent(pendingIntent).build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(CHANNEL_ID);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//below creating notification channel, because of androids latest update, O is Oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"NotificationDemo",
NotificationManager.IMPORTANCE_DEFAULT
);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(MID, notification);
MID++;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您创建
pendingIntent
时,请检查您不会传递相同的请求代码
MID
。在这种情况下,Android“重用”以前创建的未决意图,这就是为什么您只会看到最新的通知而不是几个通知。避免问题的最简单方法似乎是传递不同的请求代码(或其他参数,例如不同的
setAction(...)
- 参见下面的链接)。有关更多信息,请浏览一下Android如何比较即将出现的意图: >
When you are creating a
PendingIntent
hereplease check that you aren't passing the same request code
MID
. In this case Android "reuses" the pending intent which were created previously, that's why you would see only the latest notification instead of several notifications.It seems the easiest way to avoid the issue would be passing different request codes (or another params, for example different
setAction(...)
- see the link below) for every type of notifications.For more info please have a look at how Android compares pending intents: https://stackoverflow.com/a/20205696/2219237