【问题标题】:Android Repeating Alarm not working for the next dayAndroid重复警报第二天不起作用
【发布时间】:2018-01-04 10:54:49
【问题描述】:

我需要创建每天触发本地推送通知并且仅在特定时间有效的警报。

例如。 第 1 节:上午 7 点 - 上午 8 点, 第 2 节:晚上 7 点 - 晚上 8 点

从上面,我需要创建一个警报,每天早上 7 点发送本地推送通知,通知应该在早上 8 点自动消失。同样,另一个警报将在晚上 7 点触发本地推送通知,并在晚上 8 点删除通知。

这是我用来创建警报并在结束时间到达时关闭通知的代码。我将以下方法循环 2 次,以在一天内创建 2 次重复警报。如果当前时间超过结束时间,那么我会将通知推送到第二天。

private void setCheckInAlarm(JSONObject checkInDetails) {
    try {
        Calendar checkInTimeCalendar = Calendar.getInstance();
        String checkInTimeUnparsed = (String) checkInDetails.get("checkInTimeIN");
        Integer checkInHour = Integer.parseInt(checkInTimeUnparsed.split(":")[0]);
        Integer checkInMinutes = Integer.parseInt(checkInTimeUnparsed.split(":")[1]);
        checkInTimeCalendar.set(Calendar.HOUR_OF_DAY, checkInHour);
        checkInTimeCalendar.set(Calendar.MINUTE, checkInMinutes);
        checkInTimeCalendar.set(Calendar.SECOND, 00);
        Calendar checkOutTimeCalendar=Calendar.getInstance();
        String checkOutTimeUnparsed=(String)checkInDetails.get("checkInTimeOUT");
        Integer checkOutHour=Integer.parseInt(checkOutTimeUnparsed.split(":")[0]);
        Integer checkOutMinutes=Integer.parseInt(checkOutTimeUnparsed.split(":")[1]);
        checkOutTimeCalendar.set(Calendar.HOUR_OF_DAY, checkOutHour);
        checkOutTimeCalendar.set(Calendar.MINUTE, checkOutMinutes);
        checkOutTimeCalendar.set(Calendar.SECOND, 0);
        Date currentTime=Calendar.getInstance().getTime();
        Date checkOutTime=checkOutTimeCalendar.getTime();
        if ( currentTime.after(checkOutTime) ) {
            checkInTimeCalendar.add(Calendar.DATE,1);
        }
            Intent intent1 = new Intent(HomeScreen.this, AlarmReceiver.class);
            intent1.putExtra("checkInId", (Integer) checkInDetails.get("checkInID"));
            intent1.putExtra("notificationTitle", (String) checkInDetails.get("checkInNotificationMessage"));
            intent1.putExtra("notificationContent", (String) checkInDetails.get("message"));
            intent1.putExtra("checkInTime", (String) checkInDetails.get("checkInTimeIN"));
            intent1.putExtra("checkOutTime", (String) checkInDetails.get("checkInTimeOUT"));
            intent1.putExtra("checkInNotify", true);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(HomeScreen.this, (Integer) checkInDetails.get("checkInID"), intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager am = (AlarmManager) HomeScreen.this.getSystemService(HomeScreen.this.ALARM_SERVICE);
            am.setRepeating(AlarmManager.RTC_WAKEUP, checkInTimeCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
          //  setCheckInAlarmDismiss(checkInDetails);

    }
    catch (Exception e){
        e.printStackTrace();
        Log.d(activityName, "setCheckInAlarm: "+e);
    }
}

当触发警报时,我正在创建另一个警报以在时间结束时自动关闭通知。

推送通知。

public class AlarmReceiver extends BroadcastReceiver
{
    public static final String activityName="AlarmReceiver";
  @Override
  public void onReceive(Context context, Intent intent) {
      Bundle bundle = intent.getExtras();
      boolean checkInNotificationDismiss=bundle.getBoolean("checkInNotify");
      if(checkInNotificationDismiss) {
          NotificationParameters notificationParameters = new NotificationParameters();
          notificationParameters.setCheckInId(bundle.getInt("checkInId"));
          notificationParameters.setMessageTitle(bundle.getString("notificationTitle"));
          notificationParameters.setMessageContent(bundle.getString("notificationContent"));
          notificationParameters.setCheckInTime(bundle.getString("checkInTime"));
          notificationParameters.setCheckOutTime(bundle.getString("checkOutTime"));

          new CheckInNotification().createNotification(notificationParameters,context,HomeScreen.class);
      }else {
          new CheckInNotification().clearAllNotications(bundle.getInt("checkInId"),context,HomeScreen.class);
      }
  }



}

下面是处理通知和警报创建以消除通知的类。

public class CheckInNotification extends Activity {
public static final String activityName="checkNotification";
    public void createNotification(NotificationParameters notificationParameters,Context context,Class<?> cls){
        try {
            HomeScreenActions homeScreenActions = new HomeScreenActions();
            homeScreenActions.setCheckin(false);
            homeScreenActions.setRenderCheckin(true);
            homeScreenActions.setMenu(true);
            homeScreenActions.setRenderMenu(true);
            homeScreenActions.setWeather(true);
            homeScreenActions.setRenderWeather(true);
            homeScreenActions.setCheckInId(notificationParameters.getCheckInId());
            homeScreenActions.setMessageTitle(notificationParameters.getMessageTitle());
            homeScreenActions.setMessageContent(notificationParameters.getMessageContent());
          /* try {
                ConstraintLayout isHomeScreenVisible = findViewById(R.id.homeScreenLayout);
            }
            catch (NullPointerException nullPointer){
                Intent i = new Intent(CheckInNotification.this, HomeScreen.class);
                i.putExtra("actions", homeScreenActions);
                startActivity(i);
            }*/

                pushNotification(notificationParameters, homeScreenActions, context, cls);

        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public void clearAllNotications(Integer notificationId,Context context,Class<?> cls){
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(notificationId);
    }
    private void pushNotification(NotificationParameters notificationParameters,HomeScreenActions homeScreenActions,Context context,Class<?> cls){
        try{

            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Intent notificationIntent = new Intent(context, cls);
            Log.d(activityName, "pushNotification main: "+homeScreenActions.getCheckInId());
            Log.d(activityName, "pushNotification: "+homeScreenActions.getMessageTitle());
            Log.d(activityName, "pushNotification: "+homeScreenActions.getMessageContent());
            notificationIntent.putExtra("actions",homeScreenActions);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(cls);
            stackBuilder.addNextIntent(notificationIntent);

            PendingIntent pendingIntent = stackBuilder.getPendingIntent(
                    homeScreenActions.getCheckInId(),PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            Notification notification = builder.setContentTitle(notificationParameters.getMessageTitle())
                    .setContentText(notificationParameters.getMessageContent())
                    .setSound(alarmSound).setSmallIcon(R.mipmap.ic_launcher)
                    .setOngoing(true)
                    .setContentIntent(pendingIntent).build();

            NotificationManager notificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(homeScreenActions.getCheckInId(), notification);
            setCheckInAlarmDismiss(notificationParameters,context,cls);
    }
    catch (Exception e){
        e.printStackTrace();
        }
    }
    private void setCheckInAlarmDismiss(NotificationParameters notificationParameters,Context context,Class<?> cls) {
        try {
            Calendar calendar = Calendar.getInstance();
            String checkOut = notificationParameters.getCheckOutTime();
            Integer checkOutHour = Integer.parseInt(checkOut.split(":")[0]);
            Integer checkOutMinutes = Integer.parseInt(checkOut.split(":")[1]);
            calendar.set(Calendar.HOUR_OF_DAY, checkOutHour);
            calendar.set(Calendar.MINUTE, checkOutMinutes);
            calendar.set(Calendar.SECOND, 0);
            Intent intent1 = new Intent(context, AlarmReceiver.class);
            intent1.putExtra("checkInId", notificationParameters.getCheckInId());
            intent1.putExtra("notificationMessage", notificationParameters.getMessageTitle());
            intent1.putExtra("message", (String) notificationParameters.getMessageContent());
            intent1.putExtra("checkInNotify", false);
            intent1.putExtra("checkInTime", notificationParameters.getCheckInTime());
            intent1.putExtra("checkOutTime", notificationParameters.getCheckOutTime());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationParameters.getCheckInId(), intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
            am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        }
        catch (Exception e){
            e.printStackTrace();
            Log.d(activityName, "setCheckInAlarmDismiss: "+e);
        }
    }



}

所以,问题是当初始化应用程序时第一次创建警报和通知,session1 和 session2 警报都会发生,但是第二天,它就不起作用了。

【问题讨论】:

    标签: android notifications alarmmanager


    【解决方案1】:

    首先,我不仅会将小时、分钟和秒存储到日历中,还会存储当天的日期(可能还有月份),以防您每天都这样做。

    calendar.set(Calendar.DAY_OF_MONTH, insert_current_day);
    calendar.set(Calendar.MONTH, insert_current_month);
    ...
    

    一旦会话 1 和 2 的警报被触发(并且通知被解除),您就需要在日历中添加一天。

    calendar.add(Calendar.DATE, 1);

    在设置 AlarmService 之前。它将 startTime 设置为第二天,AlarmService 将在第二天和之后的每一天触发。


    希望这是您想要的,并且会帮助您。

    【讨论】:

    • 那么您是否建议我在通知被解除后创建另一个警报?因为我认为 setRepeating 会自动为指定的日历时间创建一个闹钟时间表。如果我理解错了,您可以使用我输入的上述代码输入代码 sn-p 吗?目前,如果用户根本没有参加,我正在创建一个警报以关闭当前通知。
    • 这是我的场景。如果会话 1 时间在上午 8 点至上午 9 点之间,并且用户当前是在上午 8 点 30 分首次访问该应用程序,我将创建一个警报以立即推送通知,因为结束时间一直到上午 9 点,之后,从次日上午 8 点定期推送通知,并于上午 9 点取消通知
    • 我对牛轧糖使用 setExactAllowWhileIdle 对 kitkat 以下版本使用 setexact 解决了这个问题,我最终在第一个警报触发时安排了下一个警报。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多