Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly assigning delegate object to the UNUserNotificationCenter object for iOS Mobile Blazor Bindings

I was following this tutorial to implement notifications through FCM. At their Register for remote notifications section there is a code snippet:

Project.iOS/AppDelegate.cs:

// Register your app for remote notifications.
if (UIDevice.CurrentDevice.CheckSystemVersion (10, 0)) {
    
    // For iOS 10 display notification (sent via APNS)
    UNUserNotificationCenter.Current.Delegate = this;

    var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
    UNUserNotificationCenter.Current.RequestAuthorization (authOptions, (granted, error) => {
        Console.WriteLine (granted);
    });
} else {
    // iOS 9 or before
    var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
    var settings = UIUserNotificationSettings.GetSettingsForTypes (allNotificationTypes, null);
    UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
}

UIApplication.SharedApplication.RegisterForRemoteNotifications ();

So seems like there are no problem but when I implemented this to my MBB hybrid project the line: UNUserNotificationCenter.Current.Delegate = this; gives an error, saying Cannot implicitly convert type 'MyApp.iOS.AppDelegate' to UserNotifications.IUNUserNotificationsCenterDelegate'. An explicit conversion exists

It won't let me compile but if I explicitly cast it into what it wants it also gives error.

Found another example of Xamarin Forms at this post (I translated mostly since I don't know Spanish) and they're doing the same.

What's the correct way to cast register for notifications for Mobile Blazor Bindings project -if this is because I'm using MBB rather than Xamarin Forms?

Steps to Reproduce

  1. Create Hybrid Mobile Blazor Bindings Project (preview 5)
  2. At Project.iOS/AppDelegate.cs, adding UNUserNotificationCenter.Current.Delegate = this; gives error of Error CS0266 Cannot implicitly convert type 'Project.iOS.AppDelegate' to 'UserNotifications.IUNUserNotificationCenterDelegate'. An explicit conversion exists (are you missing a cast?)
  3. If explicit cast is applied, it gives error too saying it can't convert.

Expected Behavior

  • Receive Notifications successfully.

Actual Behavior

image

like image 797
Kaan Taze Avatar asked Nov 15 '25 13:11

Kaan Taze


1 Answers

After some research I found that I didn't include the necessary Interfaces. AppDelegate class should look like this.

    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate
    {
        //...
    }
like image 199
Kaan Taze Avatar answered Nov 18 '25 20:11

Kaan Taze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!