Xamarin.Forms Push notifications for iOS with Azure


To start the registration for push notifications you need to execute the below code.

// registers for push
var settings = UIUserNotificationSettings.GetSettingsForTypes(
    | UIUserNotificationType.Badge
    | UIUserNotificationType.Sound,
    new NSSet());


This code can either be ran directly when the app starts up in the FinishedLaunching in the AppDelegate.cs file. Or you can do it whenever a user decides that they want to enable push notifications.

Running this code will trigger an alert to prompt the user if they will accept that the app can send them notifications. So also implement a scenario where the user denies that!

Push notifications dialog

These are the events that need implementation for implementing push notifications on iOS. You can find them in the AppDelegate.cs file.

// We've successfully registered with the Apple notification service, or in our case Azure
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
   // Modify device token for compatibility Azure
    var token = deviceToken.Description;
    token = token.Trim('<', '>').Replace(" ", "");
    // You need the Settings plugin for this!
    Settings.DeviceToken = token;
    var hub = new SBNotificationHub("Endpoint=sb://xamarinnotifications-ns.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=<your own key>", "xamarinnotifications");
    NSSet tags = null; // create tags if you want, not covered for now
    hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>
        if (errorCallback != null)
            var alert = new UIAlertView("ERROR!", errorCallback.ToString(), null, "OK", null);

// We've received a notification, yay!
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
    NSObject inAppMessage;

    var success = userInfo.TryGetValue(new NSString("inAppMessage"), out inAppMessage);

    if (success)
        var alert = new UIAlertView("Notification!", inAppMessage.ToString(), null, "OK", null);

// Something went wrong while registering!
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
   var alert = new UIAlertView("Computer says no", "Notification registration failed! Try again!", null, "OK", null);

When a notification is received this is what it looks like.

Push notification on iOS