Get started with Azure Notification Hubs for Xamarin.iOS apps



To complete this tutorial, you must have an active Azure account. If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see Azure Free Trial.

This tutorial shows you how to use Azure Notification Hubs to send push notifications to an iOS application. You'll create a blank Xamarin.iOS app that receives push notifications by using the Apple Push Notification Service (APNs).

When you're finished, you'll be able to use your notification hub to broadcast push notifications to all the devices running your app. The finished code is available in the NotificationHubs app sample.

This tutorial demonstrates the simple push message broadcast scenario with Notification Hubs.


This tutorial requires the following:

  • Latest version of XCode
  • An iOS 10 (or later version) compatible device
  • Apple Developer Program membership.
  • Visual Studio for Mac


    Because of configuration requirements for iOS push notifications, you must deploy and test the sample application on a physical iOS device (iPhone or iPad) instead of in the simulator.

Completing this tutorial is a prerequisite for all other Notification Hubs tutorials for Xamarin.iOS apps.

Generate the Certificate Signing Request file

The Apple Push Notification Service (APNS) uses certificates to authenticate your push notifications. Follow these instructions to create the necessary push certificate to send and receive notifications. For more information on these concepts see the official Apple Push Notification Service documentation.

Generate the Certificate Signing Request (CSR) file, which is used by Apple to generate a signed push certificate.

  1. On your Mac, run the Keychain Access tool. It can be opened from the Utilities folder or the Other folder on the launch pad.
  2. Click Keychain Access, expand Certificate Assistant, then click Request a Certificate from a Certificate Authority....

  3. Select your User Email Address and Common Name , make sure that Saved to disk is selected, and then click Continue. Leave the CA Email Address field blank as it is not required.

  4. Type a name for the Certificate Signing Request (CSR) file in Save As, select the location in Where, then click Save.

    This saves the CSR file in the selected location; the default location is in the Desktop. Remember the location chosen for this file.

Next, you will register your app with Apple, enable push notifications, and upload this exported CSR to create a push certificate.

Register your app for push notifications

To be able to send push notifications to an iOS app, you must register your application with Apple and also register for push notifications.

  1. If you have not already registered your app, navigate to the iOS Provisioning Portal at the Apple Developer Center, log on with your Apple ID, click Identifiers, then click App IDs, and finally click on the + sign to register a new app.

  2. Update the following three fields for your new app and then click Continue:

    • Name: Type a descriptive name for your app in the Name field in the App ID Description section.
    • Bundle Identifier: Under the Explicit App ID section, enter a Bundle Identifier in the form <Organization Identifier>.<Product Name> as mentioned in the App Distribution Guide. The Organization Identifier and Product Name you use must match the organization identifier and product name you will use when you create your XCode project. In the screeshot below NotificationHubs is used as a organization idenitifier and GetStarted is used as the product name. Making sure this matches the values you will use in your XCode project will allow you to use the correct publishing profile with XCode.
    • Push Notifications: Check the Push Notifications option in the App Services section, .

      This generates your App ID and requests you to confirm the information. Click Register to confirm the new App ID.

      Once you click Register, you will see the Registration complete screen, as shown below. Click Done.

  3. In the Developer Center, under App IDs, locate the app ID that you just created, and click on its row.

    Clicking on the app ID will display the app details. Click the Edit button at the bottom.

  4. Scroll to the bottom of the screen, and click the Create Certificate... button under the section Development Push SSL Certificate.

    This displays the "Add iOS Certificate" assistant.


    This tutorial uses a development certificate. The same process is used when registering a production certificate. Just make sure that you use the same certificate type when sending notifications.

  5. Click Choose File, browse to the location where you saved the CSR file that you created in the first task, then click Generate.

  6. After the certificate is created by the portal, click the Download button, and click Done.

    This downloads the certificate and saves it to your computer in your Downloads folder.


    By default, the downloaded file a development certificate is named aps_development.cer.

  7. Double-click the downloaded push certificate aps_development.cer.

    This installs the new certificate in the Keychain, as shown below:


    The name in your certificate might be different, but it will be prefixed with Apple Development iOS Push Services:.

  8. In Keychain Access, right-click the new push certificate that you created in the Certificates category. Click Export, name the file, select the .p12 format, and then click Save.

    Make a note of the file name and location of the exported .p12 certificate. It will be used to enable authentication with APNS.


    This tutorial creates a QuickStart.p12 file. Your file name and location might be different.

Create a provisioning profile for the app

  1. Back in the iOS Provisioning Portal, select Provisioning Profiles, select All, and then click the + button to create a new profile. This launches the Add iOS Provisiong Profile Wizard

  2. Select iOS App Development under Development as the provisiong profile type, and click Continue.
  3. Next, select the app ID you just created from the App ID drop-down list, and click Continue

  4. In the Select certificates screen, select your usual development certificate used for code signing, and click Continue. This is not the push certificate you just created.

  5. Next, select the Devices to use for testing, and click Continue

  6. Finally, pick a name for the profile in Profile Name, click Generate.

  7. When the new provisioning profile is created click to download it and install it on your Xcode development machine. Then click Done.

Configure your Notification Hub for iOS push notifications

This section walks you through the steps to create a new notification hub and configure authentication with APNS using the .p12 push certificate that you previously created. If you want to use a notification hub that you have already created, you can skip to step 5.

  1. Sign in to the Azure portal.

  2. Select Create a resource > Web + Mobile > Notification Hub.

    Azure portal - create a notification hub

  3. In the Notification Hub box, type a unique name. Select your Region, Subscription, and Resource Group (if you have one already).

    If you don't already have a service bus namespace, you can use the default name, which is created based on the hub name (if the namespace name is available).

    If you already have a service bus namespace that you want to create the hub in, follow these steps

    a. In the Namespace area, select the Select Existing link.

    b. Select Create.

    Azure portal - set notification hub properties

  4. After you've created the namespace and notification hub, open it by selecting All resources and then select the created notification hub from the list.

    Azure portal - notification hub portal page

  5. Select Access Policies from the list. Note the two connection strings that are available to you. You need them to handle push notifications later.


    Do NOT use the DefaultFullSharedAccessSignature in your application. This is meant to be used in your back-end only.

    Azure portal - notification hub connection strings

  1. Click the Notification Services button, then select Apple (APNS). Make sure to select Certificate, click the file icon, and select the .p12 file that you exported earlier. Make sure you also specify the correct password.

    Make sure to select Sandbox mode since this is for development. Only use the Production if you want to send push notifications to users who purchased your app from the store.

    Configure APNS in Azure portal

    Configure APNS certification in Azure portal

Your notification hub is now configured to work with APNS, and you have the connection strings to register your app and send push notifications.

Connect your app to the notification hub

Create a new project

  1. In Visual Studio, create a new iOS project and select the Single View App template, and click Next

    Visual Studio - Select Application Type

  2. Enter your App Name and Organization identifier, then hit Next, then Create

  3. From the Solution view, double click Info.plist and under Identity make sure your Bundle Identifier matches the one used when creating your provisioning profile. Under Signing ensure that your Developer account is selected under Team, "Automatically manage signing" is selected and your Signing Certificate and Provisioning Profile are automatically selected.

    Visual Studio- iOS App Config

  4. Add the Azure Messaging package. In the Solution view, right-click the project and select Add > Add NuGet Packages. Search for Xamarin.Azure.NotificationHubs.iOS and add the package to your project.

  5. Add a new file to your class, name it Constants.cs and add the following variables and replace the string literal placeholders with your hub name and the DefaultListenSharedAccessSignature noted earlier.

        // Azure app-specific connection string and hub path
        public const string ConnectionString = "<Azure connection string>";
        public const string NotificationHubPath = "<Azure hub path>";
  6. In AppDelegate.cs, add the following using statement:

        using WindowsAzure.Messaging;
  7. Declare an instance of SBNotificationHub:

        private SBNotificationHub Hub { get; set; }
  8. In AppDelegate.cs, update FinishedLaunching() to match the following:

        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
            if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
                var pushSettings = UIUserNotificationSettings.GetSettingsForTypes (
                       UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                       new NSSet ());
                UIApplication.SharedApplication.RegisterUserNotificationSettings (pushSettings);
                UIApplication.SharedApplication.RegisterForRemoteNotifications ();
            } else {
                UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
            return true;
  9. Override the RegisteredForRemoteNotifications() method in AppDelegate.cs:

        public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
            Hub = new SBNotificationHub(Constants.ConnectionString, Constants.NotificationHubPath);
            Hub.UnregisterAllAsync (deviceToken, (error) => {
                if (error != null)
                    Console.WriteLine("Error calling Unregister: {0}", error.ToString());
                NSSet tags = null; // create tags if you want
                Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) => {
                    if (errorCallback != null)
                        Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
  10. Override the ReceivedRemoteNotification() method in AppDelegate.cs:

        public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
            ProcessNotification(userInfo, false);
  11. Create the following ProcessNotification() method in AppDelegate.cs:

        void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
            // Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
            if (null != options && options.ContainsKey(new NSString("aps")))
                //Get the aps dictionary
                NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
                string alert = string.Empty;
                //Extract the alert text
                // NOTE: If you're using the simple alert by just specifying
                // "  aps:{alert:"alert msg here"}  ", this will work fine.
                // But if you're using a complex alert with Localization keys, etc.,
                // your "alert" object from the aps dictionary will be another NSDictionary.
                // Basically the JSON gets dumped right into a NSDictionary,
                // so keep that in mind.
                if (aps.ContainsKey(new NSString("alert")))
                    alert = (aps [new NSString("alert")] as NSString).ToString();
                //If this came from the ReceivedRemoteNotification while the app was running,
                // we of course need to manually process things like the sound, badge, and alert.
                if (!fromFinishedLaunching)
                    //Manually show an alert
                    if (!string.IsNullOrEmpty(alert))
                        UIAlertView avAlert = new UIAlertView("Notification", alert, null, "OK", null);


    You can choose to override FailedToRegisterForRemoteNotifications() to handle situations such as no network connection. This is especially important where the user might start your application in offline mode (for example, Airplane) and you want to handle push messaging scenarios specific to your app.

  12. Run the app on your device.

Sending Test Push Notifications

You can test receiving notifications in your app with the Test Send option in the Azure portal. This sends a test push notification to your device.

Azure portal - Test Send

Push notifications are normally sent in a back-end service like Mobile Apps or ASP.NET using a compatible library. If a library is not available for your back-end you can also use the REST API directly to send notification messages.

As a next step it is recommended to view the Use Notification Hubs to push notifications to users tutorial as the next step for sending notifications from an ASP.NET back-end. However, the following approaches can be used for sending notifications:

Here is a list of some other tutorials you may want to review for sending notifications:

Next steps

In this simple example, you broadcasted push notifications to all your iOS devices. In order to target specific users, refer to the tutorial Use Notification Hubs to push notifications to users. If you want to segment your users by interest groups, you can read Use Notification Hubs to send breaking news. Learn more about how to use Notification Hubs in Notification Hubs Guidance and in the Notification Hubs How-To for iOS.