简体   繁体   中英

How can I get Expo scheduled (local) notifications to work while Android device is in doze mode?

so I have created a reminder app using React Native and Expo.

I followed the Expo docs which require that you set your app up with Firebase Cloud Messaging even if you're only sending local, scheduled notifications. I also added the exact alarm permission to the android manifest using app.json. Also, the AndroidImportance for the app's notifications channels is set to MAX. Lastly, the Android priority for the notification itself is also set to MAX, as well.

Everything in the app works great, except for one thing:

When the Android device is inactive for a while, the scheduled notifications do not appear at the scheduled time.

I am assuming this is because the device has entered doze mode which is part of Android's battery optimization features. Doze mode can delay notifications.

I am just surprised-- what is the point of a local, scheduled notification feature if it can't even get past doze mode?

I could be overlooking a solution. Does anyone have any ideas? If not, I may have to rewrite a lot of code so that the notifications are not local.

Thanks!

I first tried setting the notification's priority to high, and the notification still wouldn't appear during inactivity. So then I switched the notification's priority to MAX, and that didn't seem to work either.

There is a package on npm for disabling battery optimization in react native apps, but apparently that can get you banned by google, which I'd rather not get banned.

EDIT: One other relevant detail is that when I was at the stage of development early on where I was testing in the Expo Go app, scheduled, local notifications were not affected by doze mode at all. I'd schedule one in the evening, forget about it, and it would come the next morning. I'm wondering if this as an indicator of what needs changing??

So it turns out there are two places where you can set priority to "Notifications.AndroidNotificationPriority.MAX" --

Once when you send the notification, and once when you handle the notification. I had only set it when sending the notification.

For info go to docs Ctrl + F and search for "AndroidNotificationPriority"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM