应用程序处于打ze模式时,警报管理器触发的Android通知未触发

编程入门 行业动态 更新时间:2024-10-27 18:21:21
本文介绍了应用程序处于打ze模式时,警报管理器触发的Android通知未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下要求。用户需要能够在我的应用中安排定期提醒,该提醒将在每天的确切时间触发推送通知。

I have the following requirements. A user needs to be able to schedule a recurring reminder in my app that will trigger a push notification at an exact time every day.

此是我希望最终不要提交的那些问题之一,因为在编写它时建议了类似的问题。但是,几个团队成员花了很多时间来查看Android Developer Docs和Stackoverflow,我们似乎离答案还很近,所以我们到了。

This is one of those questions that I hoped I would end up not submitting as similar questions were recommended while writing it. However several team members have spent hours and hours looking through the Android Developer Docs and Stackoverflow and we seem no closer to an answer, so here we are.

如果我创建提醒并将其设置为在未来5分钟内触发通知,则该通知会正常触发。

If I create a reminder and set it to trigger the notification 5 minutes in the future, the notification fires just fine.

我怀疑这可能是Android P中引入的节电,唤醒锁等更改引起的问题,因为在更新目标之前我们没有这个问题从SDK升级到28。话虽如此,但我不能肯定这是唯一的问题,但我可以在运行Android P的Pixel和Pixel 3 XL上始终如一地重现该问题。

I suspect this may be a problem caused by the changes to battery saving, wake lock, etc introduced in Android P as we did not have this problem prior to updating our target SDK to 28. That being said I am not positive that this is the only problem but I can consistently reproduce the issue on a Pixel and Pixel 3 XL running Android P.

例如,当用户将提醒设置为半夜的某个时间(大概是在用户处于睡眠状态,因此几小时内不会使用电话)时,就会发生通知未触发的示例。这些提醒从未触发过。

An example where the notification is not firing occurs when for example a user sets the reminder to sometime in the middle of the night (presumably when the user is asleep and thus will not have used the phone for several hours). These reminders are not firing ever.

我目前正在尝试使用警报管理器来完成此操作。

I am currently trying to accomplish this using the Alarm Manager.

此问题似乎与使用我们发现的警报管理器的setRepeating 方法不起作用。相反,我们使用警报管理器的setExactAndAllowWhileIdle方法。我们还尝试使用警报管理器 setAlarmClock 方法,根据Android文档,该方法即使系统处于低功耗空闲(也称为打ze)模式,也将被允许触发,但这还是没有成功。

This problem appears to be similar to another question that uses the Alarm Manager's setRepeating method which we have found does not work. We are instead using the Alarm Manager's setExactAndAllowWhileIdle method. We also tried this same implementation with the Alarm Managers setAlarmClock method which according to the Android documentation "will be allowed to trigger even if the system is in a low-power idle (a.k.a. doze) mode" however this was also unsuccessful.

我怀疑这不起作用的原因是,当手机处于打ze模式时,setExactAndAllowWhileIdle不会触发,类似于这个问题。此问题建议使用Firebase JobDispatcher,但由于这是内部通知,因此无论是否具有网络连接,我都需要触发该通知,这似乎消除了Firebase JobDispatcher的选择。这个问题还表明,一旦手机退出打ze模式,用户会收到通知,但我们从未收到通知,他们似乎因为缺少更好的条件而迷路了。

I suspect that the reason this is not working is because setExactAndAllowWhileIdle will not fire when the phone is in doze mode, similar to the problems expressed in this question. This question recommends using Firebase JobDispatcher but as this is an internal notification I am required to fire the notification with or without network connectivity which seems to eliminate the Firebase JobDispatcher as an option. This question also indicated that once the phone exits doze mode the user gets the notification however we are never getting the notification, they seem to be lost for lack of a better term.

我已将唤醒锁定权限添加到我的AndroidManifest.xml中:

I have added the wake lock permission to my AndroidManifest.xml:

<uses-permission android:name="android.permission.WAKE_LOCK" />

这是我的接收者在AndroidManifest.xml中的注册方式

Here is how my receiver is registered in the AndroidManifest.xml

<receiver android:name="com.myapp.receiver.AlarmReceiver"> </receiver>

这是我当前的实现方式:

Here is my current implementation:

待处理通知的意图

Intent i = new Intent(context, ScheduleAllReceiver.class); PendingIntent scheduleAllPendingIntent = PendingIntent.getBroadcast(context, SCHEDULER_DAILY_ALL, i, PendingIntent.FLAG_UPDATE_CURRENT);

我随后按如下方式调用方法 createAlarm

I subsequently call a method "createAlarm" as follows

createAlarm(context, scheduleAllPendingIntent, calendar.getTimeInMillis());

创建警报

public static void createAlarm(Context context, PendingIntent pendingIntent, long timeinMilli) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); if(alarmManager != null) { if (Build.VERSION.SDK_INT >= 23) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeinMilli, pendingIntent); } else { alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeinMilli, pendingIntent); } } }

推荐答案

添加意图标志 FLAG_RECEIVER_FOREGROUND

developer.android/reference/android/content/Intent#FLAG_RECEIVER_FOREGROUND 应该可以解决问题

Intent intent = new Intent(context, ScheduleAllReceiver.class); intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); PendingIntent scheduleAllPendingIntent = PendingIntent.getBroadcast(context, SCHEDULER_DAILY_ALL, intent, PendingIntent.FLAG_UPDATE_CURRENT);

更多推荐

应用程序处于打ze模式时,警报管理器触发的Android通知未触发

本文发布于:2023-11-27 04:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   警报   应用程序   模式   通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!