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.)
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.
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:
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.
- Valid certificate file - Certificate file should not be expired
- 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.
PushNotificationHub hub = PushNotificationHubFactory.CreateApplePushNotificationHub
(serviceDetails["ServiceBusName"], serviceDetails["ServiceBusSharedKey"]);
((ApplePushNotificationHub)hub).FullSharedAccessSignature = pnSignature;
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:
- 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.
apns.Endpoint = "gateway.sandbox.push.apple.com";
- 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.
- 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