PatilShreyas / FCM-OnDeviceNotificationScheduler

Demo implementation to Schedule FCM Notifications on Android Device using AlarmManager + WorkManager.
MIT License
119 stars 28 forks source link

alarmManager.set does not work #6

Open wellbranding opened 4 years ago

wellbranding commented 4 years ago

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?

PatilShreyas commented 4 years ago

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

wellbranding commented 4 years ago

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.

PatilShreyas commented 4 years ago

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

PatilShreyas commented 4 years ago

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

wellbranding commented 4 years ago

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

PatilShreyas commented 4 years ago

Yeah it's Xiaomi specific

On Sat 13 Jun, 2020, 9:08 PM Viktor Vostrikov, notifications@github.com 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 .

wellbranding commented 4 years ago

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 commented 4 years ago

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...

PatilShreyas commented 4 years ago

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

On Sat 13 Jun, 2020, 9:19 PM Viktor Vostrikov, notifications@github.com 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 .

wellbranding commented 4 years ago

@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? :)

PatilShreyas commented 4 years ago

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:

wellbranding commented 4 years ago

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?

PatilShreyas commented 4 years ago

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.