thudugala / Plugin.LocalNotification

The local notification plugin provides a way to show local notifications from .Net MAUI and Xamarin Forms apps .
MIT License
401 stars 66 forks source link

Users can't enable the reminder for the app #503

Open erossini opened 1 month ago

erossini commented 1 month ago

In my NET8 MAUI apps, I want to send a local notification every day to remind the user to use the app. For that, I'm using the component Plugin.LocalNotification. For Android, in the AndroidManifest.xml, I added those lines

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

<!--Required so that the plugin can reschedule notifications upon a reboot-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Then I created a service to manage the notification and checked if the user gave the consent to use the app.

INotificationService _notificationService;
var notifyDateTime = DateTime.Parse($"{DateTime.Now.ToShortDateString()} {settings.StudyAlertTime}");
var notification = new NotificationRequest
{
    NotificationId = 100,
    Title = AppResources.NotificationsTitle,
    Description = AppResources.NotificationDescription,
    CategoryType = NotificationCategoryType.Status,
    Android = new Plugin.LocalNotification.AndroidOption.AndroidOptions
    {
        AutoCancel = true,
        IconSmallName =
        {
            ResourceName = "noti_liulogo",
        },
        IconLargeName =
        {
            ResourceName = "liulogo",
        },
        LaunchAppWhenTapped = true,
        Priority = AndroidPriority.High
    },
    iOS =
    {
        HideForegroundAlert = true,
        PlayForegroundSound = true
    },
    Schedule =
    {
        NotifyTime = notifyDateTime,
        RepeatType = NotificationRepeat.Daily
    }
};

await _notificationService.Show(notification);

When the app runs this code, the user is redirected to the system page to enable Alarms and reminders but the switch is disabled.

enter image description here

What have I done wrong?

erossini commented 3 weeks ago

An update. If I added in the AndroidManifest.xml the lines

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

the application can send notifications but rejected by Google Play. Without those lines, the app shows the authorization page but I can't enable the notifications.

image

This is the same in emulators or real devices with Android API 33 or above.

thudugala commented 3 weeks ago

@erossini can you set Unrestricted battery mode in Android settings for your app and try again

erossini commented 3 weeks ago

@erossini can you set Unrestricted battery mode in Android settings for your app and try again

What permission is that?

thudugala commented 3 weeks ago

@erossini try https://www.youtube.com/watch?v=U0EsEtJrZ7g&ab_channel=ITJungles