【问题标题】:Android local notification doesn't arrive every timeAndroid本地通知并非每次都到达
【发布时间】:2017-07-05 22:05:06
【问题描述】:

我希望我的用户设置一个时间来接收来自我的应用的每日提醒。在我的 ReminderActivity 中,我创建了 PendingIntent 和警报管理器,然后在我的警报接收器类中,我在 onReceive() 中创建了通知。我在创建待处理意图时尝试了 FLAG_CANCEL_CURRENT 和 FLAG_UPDATE_CURRENT 标志,但是当我测试应用程序并更改提醒时间时,有时通知根本没有到达,或者它仅在应用程序在后台运行时到达,并且屏幕开启。如果有任何想法或想法,我将不胜感激。

提醒活动代码:

private void setNotification() {

    Calendar calendar = Calendar.getInstance();
    Calendar now = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, chosenHour);
    calendar.set(Calendar.MINUTE, chosenMinute);
    calendar.set(Calendar.SECOND, 0);

    //if user sets the alarm after their preferred time has already passed that day
    if(now.after(calendar)) {
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }

    Intent intent = new Intent(this, AlarmReceiver.class);

    pendingIntent = PendingIntent.getBroadcast(ReminderActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    alarmManager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

}

报警接收代码:

   @Override
public void onReceive(Context context, Intent intent) {

    Bitmap largeLogo = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.ptwired_logo);

    //create local notification
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    //notificationIntent.putExtra("FromPTWired", true); //to track if user opens the app from the daily digest notification

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ptwired_logo)
            .setLargeIcon(largeLogo)
            .setContentTitle(context.getResources().getString(R.string.app_name))
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setContentText(REMINDER_TEXT)
            .setAutoCancel(true)
            .setOngoing(false)
            .build();



     notificationManager.notify(1, notification);

    }
}

【问题讨论】:

    标签: android android-notifications local android-alarms reminders


    【解决方案1】:

    可能是Doze模式的问题,看看Android限制:

    打瞌睡限制

    以下限制适用于您在打瞌睡时的应用:

    标准 AlarmManager 警报(包括 setExact() 和 setWindow())被推迟到下一个维护窗口。

    如果您需要设置在打瞌睡时触发的警报,请使用 setAndAllowWhileIdle() 或 setExactAndAllowWhileIdle()。

    使用 setAlarmClock() 设置的警报继续正常触发 - 系统在这些警报触发前不久退出打盹。

    【讨论】:

    • 谢谢@motis10 但是setExactAndAllowWhileIdle() 不要求“间隔”值,我希望闹钟每天在同一时间重复。
    • 对。但您可以在每个 setExactAndAllowWhileIdle() 设置警报。例如,意图可以调用服务。并且该服务可以做功能+设置第二天的新闹钟。
    • 知道了。问题是我没有使用服务。只有一个广播接收器。
    • 太棒了。所以在你的 onReceive() 中重新安排它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2021-12-02
    • 2012-07-13
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多