FCM-OnDeviceNotificationScheduler icon indicating copy to clipboard operation
FCM-OnDeviceNotificationScheduler copied to clipboard

alarmManager.set does not work

Open wellbranding opened this issue 4 years ago • 13 comments

Hello, I have tried using alarManager.set, but it does not work with all devices, particularly with Huawei or Xiaomi. I tried to use setExactAndAllowWhileIdle, but it also does not work if app is closed. What are your suggestions? Will you update your sample to tackle these issues?

wellbranding avatar Jun 11 '20 14:06 wellbranding

It's problem of Chinese ROMs actually. If you go to the app settings and turn off background restrictions and set autostart ON and remove other restrictions too it'll surely work. I'm also using Xiaomi Phone and tested on it. Try it and LMK if it works

PatilShreyas avatar Jun 11 '20 14:06 PatilShreyas

Actually it did not helped for all devices. It worked on Pixel 3 XL, but it does not work on Nexus 6P Emulator. Here is what I did:

  1. I created an alarm at particular time.
  2. I ran adb shell dumpsys alarm > dump.txt and it showed Pending Intent
  3. I also checked to the app settings and removed battery optimisation. There weren't any settings like autostart or background restrictions.
  4. Then I swipe app out application.
  5. I ran adb shell dumpsys alarm > dump.txt and no Pending intents were available anymore.

The only solution which actually works is to start foreground service. If I am correct indeed I would like Firebase team to adress this issue and perhaps think of better solution, possibly create delayed notifications in the cloud.

wellbranding avatar Jun 12 '20 20:06 wellbranding

Foreground service will also not work once it's removed or swiped out. Right?

PatilShreyas avatar Jun 13 '20 02:06 PatilShreyas

Screenshot_2020-06-13-08-26-19-584_com miui securitycenter Here's Autostart permission and turn off batter saving.

PatilShreyas avatar Jun 13 '20 02:06 PatilShreyas

Yes, foreground service will not work if user disables it. Thanks, I think we are getting closer! However, I can't find this exact menu like in the screenshot on Nexus 6P (API 29). I long press on application and then press App Info, and advanced. How did you find this menu or is it Xiaomi specific ?

I checked on my Xioami MI A2 (API 29, Android P) and could not find this menu. Screenshot_20200613-184320

wellbranding avatar Jun 13 '20 15:06 wellbranding

Yeah it's Xiaomi specific

On Sat 13 Jun, 2020, 9:08 PM Viktor Vostrikov, [email protected] wrote:

Yes, foreground service will not work if user disables it. Thanks, I think we are getting closer! However, I can't find this exact menu like in the screenshot on Nexus 6P. I long press on application and then press App Info, and advanced. How did you find this menu or is it Xiaomi specific ?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/PatilShreyas/FCM-OnDeviceNotificationScheduler/issues/6#issuecomment-643639828, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEVWFOENIQF2633ZN2U4AXTRWOMQNANCNFSM4N3OLL5A .

PatilShreyas avatar Jun 13 '20 15:06 PatilShreyas

I updated my question with Xiaomi MI A2 screenshot. However it is stock android, so it does not use MIU... Still it does not work on Stock android as well as NEXUS 6P. You can test yourself :)

wellbranding avatar Jun 13 '20 15:06 wellbranding

I found exact issue on Reddit. Seems that the only solution as I said is Foreground service, which is a hack and could be disabled...

wellbranding avatar Jun 13 '20 15:06 wellbranding

Okay. Thanks for sharing this. I'll surely look into it.

On Sat 13 Jun, 2020, 9:19 PM Viktor Vostrikov, [email protected] wrote:

I found exact issue on Reddit: https://www.reddit.com/r/androiddev/comments/gepps0/pendingintent_and_alarmmanager_dont_work_when_the/ http://url Seems that the only solution as I said is Foreground service, which is a hack and could be disabled...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/PatilShreyas/FCM-OnDeviceNotificationScheduler/issues/6#issuecomment-643641175, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEVWFOGGA3B3TETVHO3HZ4TRWONYVANCNFSM4N3OLL5A .

PatilShreyas avatar Jun 13 '20 16:06 PatilShreyas

@PatilShreyas Any news? This is critical for our implementation. I have checked and OneSignal offers solution with scheduled notifications. Is there alternative with FCM without any hacky solution? :)

wellbranding avatar Jun 17 '20 08:06 wellbranding

Yes. You can use Firebase Cloud Function with Cron jobs which will automatically execute on specified time and date (or periodic) which will send FCM message.

See resources below:

  • https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html
  • https://firebase.google.com/docs/functions/schedule-functions

PatilShreyas avatar Jun 17 '20 09:06 PatilShreyas

Thanks, I looked at them. They seem more like a cron job for period requests. Can I declare specific date for creating a notification? If yes, what should I pass as parameter in functions.pubsub.schedule()?

Also, if I need to create 3 notifications at random dates every day, I would need to create a 3 different pub sub requests?

wellbranding avatar Jun 17 '20 11:06 wellbranding

I personally didn't try it so can't give you a satisfactory solution. I think you should ask a question on StackOverflow or write a tweet by tagging Firebase.

PatilShreyas avatar Jun 17 '20 11:06 PatilShreyas