Android 从开始日期到结束日期重复本地通知,并且每天重复1-4次

Android 从开始日期到结束日期重复本地通知,并且每天重复1-4次,android,broadcastreceiver,alarmmanager,uilocalnotification,android-pendingintent,Android,Broadcastreceiver,Alarmmanager,Uilocalnotification,Android Pendingintent,我有一个应用程序,我需要用我的数据设置一些本地通知。我可以使用AlarmManager、PendingEvent和BroadcastReceiver获取硬编码时间值的本地通知 现在我的问题是,我需要设置从开始日期到结束日期的本地通知/提醒,每天可以设置1-4次(即频率可以是1-4次,共4次) 说 从6月3日开始,到6月30日,我应该每天早上9点和下午6点收到本地通知 更新的问题每次我运行应用程序时,都会显示最后一个挂起的通知,即如果我今天下午5:30有一个通知,如果我在下午6:00运行应用程序,

我有一个应用程序,我需要用我的数据设置一些本地通知。我可以使用AlarmManager、PendingEvent和BroadcastReceiver获取硬编码时间值的本地通知

现在我的问题是,我需要设置从开始日期到结束日期的本地通知/提醒,每天可以设置1-4次(即频率可以是1-4次,共4次)

从6月3日开始,到6月30日,我应该每天早上9点和下午6点收到本地通知

更新的问题每次我运行应用程序时,都会显示最后一个挂起的通知,即如果我今天下午5:30有一个通知,如果我在下午6:00运行应用程序,我的应用程序将显示下午5:30的通知。同样,如果我昨天晚上7:00左右有最后一个通知,今天当我运行应用程序时,它会显示出来。如何取消或清除该通知

*主要活动方法

scheduleNotificationsMethod() {

AlarmManager alarmManager = (AlarmManager) mCtx.getSystemService(ALARM_SERVICE);

        if(treatment.getSCHEDULE().equalsIgnoreCase("Daily")) {
            String[] times = {treatment.getTIME1(), treatment.getTIME2(), treatment.getTIME3(), treatment.getTIME4()};
            for(String time : times) {
                if(time != null && time != "") {

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(fromISO8601UTC(time));
                    int hours = calendar.get(Calendar.HOUR_OF_DAY);
                    int minutes = calendar.get(Calendar.MINUTE);
                    date1 = createDate(hours, minutes);
                    time1 = date1.getTime();

                    intent.putExtra(MEDICINE_TIME, toISO8601UTC(fromISO8601UTC(time)));
                    intent.putExtra(END_DATE, treatment.getEndDate());

                    String reqCode1 = treatment.getID()+""+treatment.getMedicineName()+""+hours+""+minutes;
                    int req1 = reqCode1.hashCode();
                    intent.putExtra(REQUEST_CODE, req1);

                    pendingIntent1 = PendingIntent.getBroadcast(mCtx, req1, intent, 0);
                    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time1, AlarmManager.INTERVAL_DAY, pendingIntent1);

                }
            }
        }
}
通知接收方类别

public class NotificationReciever extends BroadcastReceiver {

private final String CHANNEL_ID = "personal_notifications";
public static String NOTIFICATION_MESSAGE = "notificationMessage";
private Context context;

@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("Utils Inside NotificationReciever onReceive");
    String message = intent.getStringExtra(NOTIFICATION_MESSAGE);
    this.context = context;
    context.sendBroadcast(new Intent("SHOW_ALERT_CARD"));

    String action = "It's time to take your medicine!";
    String endDate = intent.getStringExtra("END_DATE");
    String msg = intent.getStringExtra("MEDICINE_TIME");
    int requestCode = intent.getIntExtra("REQUEST_CODE", 0);

    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, resultIntent, 0);
    buildNotification(context, pendingIntent, msg);

public void buildNotification(Context context, PendingIntent pendingIntent, String msg) {
    System.out.println("Utils Inside callTheMethod");

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    int color = 0xffffaa00;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
    builder.setSmallIcon(R.drawable.ic_dosage_c);
    builder.setContentIntent(pendingIntent);
    builder.setContentTitle("It's time to take your medicine!");
    builder.setAutoCancel(true);
    builder.setContentText(msg);
    builder.setSound(soundUri);
    builder.setLights(Color.RED, 1000, 1000);
    builder.setColor(color);

    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(102938, notification);
}
}

您可以设置任意数量的警报。这里的技巧是确保它们被
AlarmManager
认为是“唯一的”。要使其“独特”,可以使用以下方法之一:

  • 在调用
    pendingent.getBroadcast()
  • 在传递给
    pendingent.getBroadcast()的
    Intent
    中使用不同(唯一)的操作或数据
无法将警报设置为每天重复一定天数。但是,您可以将警报设置为每天重复,当警报触发时,检查您的
广播接收器
是否上次触发了警报,如果是,则取消警报,以便以后不再触发。为此,您需要记住它“过期”的日期,您可以通过将该信息存储在数据库或
SharedReferences
中,或将该信息作为“额外”添加到传递给
广播接收器的
意图中


注意:Android有许多关于安排警报的规则,以优化电池寿命。如果你的闹钟不需要在一个精确的时间触发,安卓会将时间安排修改到一个更为优化的时间。有多种方法可以使警报在准确时间触发,但无法可靠地设置重复的准确时间警报。如果您需要准确的时间,请安排一次警报,并在警报触发时安排下一次警报。了解
AlarmManager
的工作方式,以及如何在发生什么情况时最好地安排您的报警?你的问题是什么?我可以通过使用calendar.set(calendar.HOUR\u of_DAY,15)获得一次本地通知,即下午3:45(从下面的代码);日历。设置(日历。分钟,45);。我如何设置类似的本地通知,从开始日期到结束日期,频率为每天1-4次。@DavidWasser你能看一次我更新的代码,让我知道我哪里做错了。以及如何为一周中的特定日期设置提醒。请删除
pendingent.FLAG_ONE_SHOT
,这通常会导致问题,不需要。另外,请告诉我发生了什么,什么不起作用。我无法从代码中猜出它在做什么或不做什么。嘿,大卫。谢谢你的回答。我当时正忙着做其他的工作,没有仔细研究这件事。我会尝试你告诉我的方式,如果我被困在任何地方,我会回复你嘿,大卫,你能看看我更新的代码,让我知道我哪里做错了吗。我已经按照你的建议设置了不同的操作,但不起作用。
public class NotificationReciever extends BroadcastReceiver {

private final String CHANNEL_ID = "personal_notifications";
public static String NOTIFICATION_MESSAGE = "notificationMessage";
private Context context;

@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("Utils Inside NotificationReciever onReceive");
    String message = intent.getStringExtra(NOTIFICATION_MESSAGE);
    this.context = context;
    context.sendBroadcast(new Intent("SHOW_ALERT_CARD"));

    String action = "It's time to take your medicine!";
    String endDate = intent.getStringExtra("END_DATE");
    String msg = intent.getStringExtra("MEDICINE_TIME");
    int requestCode = intent.getIntExtra("REQUEST_CODE", 0);

    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, resultIntent, 0);
    buildNotification(context, pendingIntent, msg);

public void buildNotification(Context context, PendingIntent pendingIntent, String msg) {
    System.out.println("Utils Inside callTheMethod");

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    int color = 0xffffaa00;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
    builder.setSmallIcon(R.drawable.ic_dosage_c);
    builder.setContentIntent(pendingIntent);
    builder.setContentTitle("It's time to take your medicine!");
    builder.setAutoCancel(true);
    builder.setContentText(msg);
    builder.setSound(soundUri);
    builder.setLights(Color.RED, 1000, 1000);
    builder.setColor(color);

    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(102938, notification);
}
}