xamarin-docs icon indicating copy to clipboard operation
xamarin-docs copied to clipboard

iOSNotificationManager => Initialize() is missing?

Open cakirpro opened this issue 4 years ago • 0 comments

Firstly, I'd like to mention that the documentation about using local notifications in Xamarin is very useful.

However, while getting help about the iOS implementation, I faced a small problem and felt necessary to share it hoping if it could help anyone or if it can make the documentation better.

  1. The Android implementation calls the Initialize() method from its constructor. However it is missing in iOS implementation.
public iOSNotificationManager() => Initialize();
  1. Since the notifications are dependent upon this permission, it might be better to assign the delegate UNUserNotificationCenter.Current.Delegate after getting this permission.

The Initialize() method would then look like:

// Request Notifications Permission
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
{
    // persist if necessary. For example start your business logic which later sends local notifications.
    DependencyService.Get<IConfigService>().HasNotificationsPermission = approved;
    // start notification receiver
    UNUserNotificationCenter.Current.Delegate = new iOSNotificationReceiver();
});

PS: INotificationManagerhas actually single responsibility Send/Receive Notifications. Wouldn't it be better to seperate the requesting for permissions from this class?

So far I could test it with a Simulator under MacOS using Visual Studio Mac + Xcode.

As a result, after making this change, both Android and iOS Apps are working as expected.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

cakirpro avatar Jul 25 '21 12:07 cakirpro