2017-07-10

Recently we started looking into developing an app for our community site with a notification mechanism so that we can get the updates to our mobile directly. It is still in the early stage of the design / development process and thought it would be nice to share some of the proof of concept work done for the above.

In this post we will see how to send a push notification to a IOS phone using the Azure Notification Hub and Azure Functions. We will see how to setup the APNS profiles, Azure Notification Hub and Azure functions, so that the mobile app will receive the notification.

Referenced:

  1. https://github.com/Azure/azure-notificationhubs-samples

​Setup the IOS Certificates and Profiles:

  • Enable the APNS option on the app identifer
  • Download and Export the certificate in the .p12 format using the Keychain Access

Setup the Azure Notification Hub:

More details about the Azure Notification Hub can be found here.  

Upload the APNS certificate

Copy / Notedown the connection string

Setup the Azure Function:

The below code was taken from the Azure github samples and modified slightly for the Azure function.  Replace the HubName and the Key from the access policy page.  The original code can be downloaded from the github examples.

#load "ConnectionString.csx"
using System.Text;
using System.Net;
using Microsoft.Azure;
public static async Task Run(HttpRequestMessage req, TraceWriter log)
{
string hubName = "";
string fullConnectionString = "Endpoint=sb://.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=");
string messageId = SendNativeNotificationREST(hubName, fullConnectionString, "APNS from the Azure Notification Hub.", "APNS").Result;
return req.CreateResponse(HttpStatusCode.OK, "Message sent.");
}
private static async Task SendNativeNotificationREST(string hubname, string connectionString, string message, string nativeType)
{
...
}
private static async Task ExecuteREST(string httpMethod, string uri, string sasToken, WebHeaderCollection headers = null, string body = null, string contentType = "application/json")
{
...
}

​Xamarin - IOS Register for Notification

On the IOS app, register with the notification hub using the "Xamarin.Azure.NotificationHubs.iOS".

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
Hub = new SBNotificationHub(, );
Hub.UnregisterAllAsync(deviceToken, (error) =>
{
if (error != null)
{
return;
}
NSSet tags = null; // create tags if you want
Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>
{
if (errorCallback != null)
Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
});
});
}

Handle the notification response with the custom delegate class derived from "UNUserNotificationCenterDelegate"

 public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
#region Override Methods
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Alert);
}
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
// Take action based on Action ID
switch (response.ActionIdentifier)
{
case "reply":
// Do something
break;
default:
if (response.IsDefaultAction)
{
}
else if (response.IsDismissAction)
{
}
break;
}
completionHandler();
}
}

Run the Azure function

 

(Visited 72 times, 1 visits today)

About the author 

Balamurugan Kailasam

Download 6 Free Ebooks (No email needed)

There are Beginner Ebooks on Power BI, Power Automate and Teams. 2 Microsoft Teams Governance Ebooks and a very popular Power Users Guide to SharePoint!