Android 同时发出多个报警通知
每当需要服药时,我的应用程序都需要显示警报通知。如果同时服用两片药片,则应显示两份通知。现在,如果药丸服用时间不同,我的应用程序可以显示单独的通知。问题是,如果它们同时出现,它只会显示最后一个 我尝试了StackOverflow的一些解决方案。但对我来说什么都不行Android 同时发出多个报警通知,android,notifications,alarmmanager,reminders,Android,Notifications,Alarmmanager,Reminders,每当需要服药时,我的应用程序都需要显示警报通知。如果同时服用两片药片,则应显示两份通知。现在,如果药丸服用时间不同,我的应用程序可以显示单独的通知。问题是,如果它们同时出现,它只会显示最后一个 我尝试了StackOverflow的一些解决方案。但对我来说什么都不行 .................... Intent intent = new Intent(AddReminder.this, LocalNotification.class); Alarm
....................
Intent intent = new Intent(AddReminder.this, LocalNotification.class);
AlarmManager alm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int id = dbHelper.getAlarmCount() + 1;
dbHelper.addToLocalNotification(id, dateStr, getResources().getString(R.string.reminder), descp,
param,"");
intent.putExtra("title",getResources().getString(R.string.reminder));
intent.putExtra("desc",descp);
PendingIntent pi = PendingIntent.getBroadcast(
getApplicationContext(), id, intent, 0);
setAlarmValue(alm,dateStr,pi);
.....................................................
private void setAlarmValue(AlarmManager alm, String date, PendingIntent pi){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date dateVal = sdf.parse(date);
String repeat = s1.get(weiderholen.getSelectedItemPosition()).toLowerCase();
if (repeat.equals("daily")) {
alm.setRepeating(AlarmManager.RTC_WAKEUP, dateVal.getTime(), AlarmManager.INTERVAL_DAY, pi);
} else if (repeat.equals("weekly")) {
alm.setRepeating(AlarmManager.RTC_WAKEUP, dateVal.getTime(), AlarmManager.INTERVAL_DAY * 7, pi);
} else if (repeat.equals("monthly")) {
alm.setRepeating(AlarmManager.RTC_WAKEUP, dateVal.getTime(), AlarmManager.INTERVAL_DAY * 30, pi);
} else if (repeat.equals("yearly")) {
alm.setRepeating(AlarmManager.RTC_WAKEUP, dateVal.getTime(), AlarmManager.INTERVAL_DAY * 365, pi);
} else {
alm.set(AlarmManager.RTC_WAKEUP, dateVal.getTime(), pi);
}
}catch (Exception e){
e.printStackTrace();
}
}
还有我的广播接收器
public class LocalNotification extends BroadcastReceiver {
int m;
public void onReceive(Context context, Intent intent) {
m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
NotificationManager mNotificationManager;
Notification notification = getNotification(context,intent.getStringExtra("title"),intent.getStringExtra("desc"));
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/* Create or update. */
NotificationChannel channel = new NotificationChannel("my_channel_01",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(m, notification);
}
@TargetApi(Build.VERSION_CODES.O)
public Notification getNotification(Context context, String title, String message){
long when = System.currentTimeMillis();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
// Intent viewIntent = new Intent(context, DashBoardActivity.class);
Intent viewIntent = new Intent(context, SplashActivity.class);
viewIntent.putExtra("TRIGGERED_FROM", "NOTIFICATION_TASK");
PendingIntent pendingIntent = PendingIntent.getActivity(context, m, viewIntent, PendingIntent.FLAG_ONE_SHOT);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,"my_channel_01")
.setSmallIcon(R.mipmap.applogo)
.setContentTitle(title)
.setContentText(message).setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent);
Notification notification = mBuilder.build();
notification.flags=Notification.FLAG_AUTO_CANCEL;
return notification;
}
}
问题在于
mNotificationManager.notify(m,notification)代码>
m
与同一时间的通知相同,因此创建一个静态通知id,该id将在onReceive方法中更新
上述接收方法
`private static int NOTIFICATION_ID = 1; `
在onReceive增量中,输入此id
通知\u ID++
然后传递这个ID
mNotificationManager.notify(通知ID,通知)代码>
希望它能起作用!:) 如果同时服用两片药片,则应出现两个通知。。。为什么不只显示一个通知和两条消息?@B001ᛦ 因为这很难,因为提醒是可重复的。有些疾病可能是每天发生的,有些可能是每周发生的ym=(int)((new Date().getTime()/1000L)%Integer.MAX_值);每个通知的“m”值都不同。但不起作用。这是正确的答案。谢谢