Click here to Skip to main content
15,880,469 members
Articles / Hosted Services / Azure
Tip/Trick

Azure Notificationhub - Sending Push Notification to Mobile Devices

Rate me:
Please Sign up or sign in to vote.
2.71/5 (3 votes)
26 Nov 2014CPOL3 min read 14.4K   3   4  
Azure Notificationhub - Sending Push notification to mobile devices

Introduction

This tip is about sending Push Notification using Azure Notification hubs.

Background

I was not able to find much articles on this and thus post migrating it to production, I am writing this blog to help developers integrate Azure for sending PN. Before you start, you should have a basic understanding of Azure cloud, Push notifications, C#.

Using the Code

Basic requirement to this code is creating the Service Bus on Azure. You need to have admin access to the Azure portal where you need to go to "Service Bus" and then click on "Create" on the bottom. (Please note that I still somehow prefer the old Azure interface. I am still not comfortable with the new interface. So you will find reference to that interface.)

Image 1

This step is manual but you can do this using SDK as well. Here, you need the SAS information ("Endpoint" information) to proceed further in the code. The information can be fetched from "Connection Information" button at the bottom. A popup will show up from which you can copy the endpoint information.

Image 2

The copied endpoint is very important and would look like:

"Endpoint=sb://<Name>.servicebus.windows.net/;
SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<Key>"

Keep the Name and Key safe as you would need this information to access the Notification Hubs. Maybe keep it in the web.config.

The next step is to programmatically create the Notification Hub. If you want the hubs to be created dynamically from code, you can use the "ApplePushNotificationHub" class to create. The name should be unique. You need to upload the certificate information while creating the hub which can be done from the following code:

C#
PushNotificationHub hub = PushNotificationHubFactory.CreateApplePushNotificationHub
(serviceDetails["ServiceBusName"], serviceDetails["ServiceBusSharedKey"]);
MyNotificationHubDescription notificationDesc = null;
notificationDesc = hub.CreateNotificationHub(PushNotificationHub.GetHubNameByAppId(item.ApplicationId), 
item.CertificateFile, item.FilePassword, isProductionEnvironment);

The above code would return the notificationDesc which will have Primary and Secondary keys which would be later used while sending the Push Notifications. You might want to save it in some table for future reference.

There are certain things that should be verified, otherwise the hub would not be created.

  1. Valid certificate file - Certificate file should not be expired
  2. Password should be correct

The next part would be registering the devices on the hub. All devices need to be registered to the hub. You can later call the Send PN method to simply send a push notification which will take care of actually sending the PN.

C#
// Create instance of service hub which would be used to send PN
PushNotificationHub hub = PushNotificationHubFactory.CreateApplePushNotificationHub
(serviceDetails["ServiceBusName"], serviceDetails["ServiceBusSharedKey"]);
// pnSignature is the Primary ket that was fetched in previous code
((ApplePushNotificationHub)hub).FullSharedAccessSignature = pnSignature;
// Code to send the PN
hub.SendPushNotificationByTagAsync(PushNotificationHub.GetHubNameByAppId
(pushNotification.ApplicationId), pushNotification.Message, tagList).Wait();

Use the code attached for the rest of the functionality.

Points of Interest

There are many pieces of code which are not mentioned in any document like:

  1. How to configure the sandbox mode - While development, you might want to unit test the application by sending PN. For that, you generate certificate from apple for developer. Below is the code which makes the hub for sandbox mode.
    C#
    apns.Endpoint = "gateway.sandbox.push.apple.com"; 
  2. Sending PN to Production - If you use the Azure portal for sending PN (debug in portal), then somehow the hub gets unusable for sending further PNs. So be careful with the production hubs.
  3. Sandbox hubs sends PN to 10 devices only. Don't expect all the devices to receive PN. However, portal shows the status of sent PNs.

History

  • 26th November, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Architect on Microsoft technology with hands on knowledge in Azure, iOS and Android plateforms

Comments and Discussions

 
-- There are no messages in this forum --