.NET 9 MAUI Scheduled notifications do not work
Describe the bug I have a very common usecase. I have a language learning app and I want to remind user to open the app if he doesn't use it for more than 24 hours.
For Android I added only minimum permissions. I did not add optional permissions.
If I add optional permissions I get this error on publish: You must let us know whether your app uses any exact alarm permissions.
How I impemented it according to the documentation and my understanding of the logic:
At the start of my app I ran notificationService.CancelAll() and then create a new notification with Schedule.NotifyTime = DateTime.Now.AddMinutes(-1) and Schedule.RepeatType to NotificationRepeat.Daily
For iOS it works, for Android it does not.
- Did I implement it correctly?
- Does my app need optional permissions (USE_EXACT_ALARM, SCHEDULE_EXACT_ALARM) for android?
- Is my app considered as an alarm or calendar app?
I don't think you need USE_EXACT_ALARM or SCHEDULE_EXACT_ALARM, as the notification doesn't need to show at an exact time.
Can you attach a sample project here?
It started to work both on Google (Galaxy S20) and iOS after I did these steps:
- As https://github.com/Kebechet suggested I added android.permission.SCHEDULE_EXACT_ALARM and Android = new AndroidNotificationPermission() { RequestPermissionToScheduleExactAlarm = true,} https://github.com/thudugala/Plugin.LocalNotification/issues/465#issuecomment-2149146200
When I add USE_EXACT_ALARM Google does not allow me to publish my app.
- I set Schedule.NotifyTime = DateTime.Now.AddDays(1).AddMinutes(-1) and Schedule.RepeatType = NotificationRepeat.Daily. If I add only Days then I get the notification immediatelly on iOS. If I only substract 1 minute it works as expected on iOS but does not work on Android. It seems that for Android the NotifyTime is not only Time it also reads Date part and it must be in the future to work.
And now I have another issue. LocalNotificationCenter.Current.NotificationActionTapped does not fire on iOS. It is issue for me because I want to generate new text for the next notification every time user dismisses notification. I'm considering to switch to NotificationRepeat.Weekly and to add 7 notifications with different text for every day of the week. And when user opens app next time I just clear all notifications and again create 7 new. In that way notifications will not be with the same text every day.
My app is quite big. Sorry I don't have time now to make a test app. And thank you for your work. Notification APIs in mobile apps is nightmare.
Hi @thudugala first of all thanks for the great work! Really appriciate the Package. Regarding what you said about USE_EXACT_ALARM or SCHEDULE_EXACT_ALARM
I don't think you need USE_EXACT_ALARM or SCHEDULE_EXACT_ALARM, as the notification doesn't need to show at an exact time.
I tested it on my app and as soon as I remove this notifications are not working anymore at all. So I am prettty sure, this is mandatory.
Regarding iOS: In my app everything is working fine on android also with daily reminderst etc. On iOS on the other hand notifications are not working at all. I already added necessary code to Info.plist and AppDelegate.cs I think:
Info.plist
<array> <string>remote-notification</string> </array> <key>NSUserNotificationUsageDescription</key>
and in AppDelegate.cs
// Request notification permissions UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (granted, error) => { if (granted) { InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications); } });
is there any working sample for current iOS and Android versions?
Hi @OliverZott can you please attach a sample project?
Hi @Elvin-Thudugala-1164538 thanks for the qucik reply. I prepared an example in one of my unfinished (WIP) template projects:
https://github.com/OliverZott/maui-template
Just run the Emulator and go to the SettingsView via flyout menu. There you can choose time and toggle notification. I made 2 observations:
- On iOS it doesn't work at all
- On Android it works fine, unless you remove USE_EXACT_ALARM and SCHEDULE_EXACT_ALARM lines from AndroidManifest.xml it DOESNT work on Android either
If you need more information or anything else I can help with, please tell me.
@OliverZott , I had no issue showing notifications in Android using your sample after removing USE_EXACT_ALARM and SCHEDULE_EXACT_ALARM?
Please don't set ChannelId as you have not defined one.
Android = new Plugin.LocalNotification.AndroidOption.AndroidOptions
{
ChannelId = "default"
}
Works in iOS for me
Hi @thudugala Thanks for your reply, I will remove
Android = new Plugin.LocalNotification.AndroidOption.AndroidOptions { ChannelId = "default" }
and test again. Another quick question: In wiki you wrote:
if the pattern is TimeInterval you must set NotifyRepeatInterval in NotificationRequest in iOS, not allowed delay and repeat when using TimeInterval
so is it better to use
Schedule = new NotificationRequestSchedule { NotifyTime = notifyTime, RepeatType = NotificationRepeat.Daily }
instead of
Schedule = new NotificationRequestSchedule { NotifyTime = notifyTime, RepeatType = NotificationRepeat.TimeInterval, NotifyRepeatInterval = TimeSpan.FromHours(24) },
??
@OliverZott yes. As the document says in iOS you can set start Time for schedule notification.