Push Notifications for Xamarin.iOS

Configuring Notification Hubs for Xamarin.iOS apps

Some user devices are iOS-based, some Android, some Windows Phone, and the list goes on. Each of the native platforms has its own push notification framework and backend service. Azure Mobile Services allows the developer to configure a single Notification Hub (based on Microsoft Azure Service Bus Notification Hubs) through which notifications may be sent. The notification goes to each user via the correct push service according to her device’s requirements. Broadcast to millions in consumer scenarios, to tagged groups, to individuals with the same ease.

To use notifications hub from Azure Mobile Services with your iOS apps, you need to generate a certificate, register your iOS app for push notifications, save your certificate to a file and upload to Azure Mobile Services Management Portal, into your Notifications Hub. The whole process of generating a certificate, and configuring Azure Notification Hubs for use with iOS apps here, which is fairly extensive and has some screenshots.

Code snippet: Xamarin.iOS app using Push Notification Hubs

We use Xamarin Forms for best cross-platform portability: we can re-use the same UI and most of the middle tier in our Android project as well.

1. In Xamarin Studio on your Mac, or Visual Studio on your PC, create a new Xamarin Forms project. We will need to use WindowsAzure.Messaging from GitHub. Compile WindowsAzure.Messaging.dll and reference it in your project. This assembly provides iOS bindings for Notification Hub:

clip_image002

2. For Xamarin.iOS project, we need to make some changes in the AppDelegate.cs file. First, start by adding:
SBNotificationHub _hub;

public const string _azureConnection = “<Azure connection string>”;

public const string _notificationHubPath = “<Azure hub path>”;

3. Next in FinishedLaunching method add this to register for remote notifications in your app (providing several types of notifications):

UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(

UIRemoteNotificationType.Alert |

UIRemoteNotificationType.Badge);

4. Add the following methods to handle registration and notification processing.

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)

{

_hub = new SBNotificationHub(_azureConnection, _notificationHubPath);

_hub.UnregisterAllAsync (deviceToken, (error) => {

if (error == null)

{

NSSet tags = null; // create tags if you want

_hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>

{

// handle errors here

});

}

else

{

// handle errors here

}

});

}

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary options)

{

if (null != options && options.ContainsKey(new NSString(“aps”)))

{

NSDictionary aps = options.ObjectForKey(new NSString(“aps”)) as NSDictionary;

string alert = string.Empty;

if (aps.ContainsKey(new NSString(“alert”)))

alert = (aps[new NSString(“alert”)] as NSString).ToString();

if (!string.IsNullOrEmpty(alert))

{

UIAlertView avAlert = new UIAlertView(“Notification”, alert, null, “OK”, null);

avAlert.Show();

}

}

}

We can now register for hub notifications and receive notifications, displaying alerts in our Xamarin.iOS apps.

Reference:

Get started with Notification Hubs with Xamarin.iOS
Xamarin.iOS: Push Notifications in iOS

Leave a Reply

Your email address will not be published. Required fields are marked *