Register Azure Notification Hub register with Javascript using REST services

I am trying to get registered in the Azure Notification Hub working with html / javascript code running on the web view host (Phonegap / Intel XDK). There is no client library available, so I'm trying to use the REST API (documentation :).

I have the following Javascript code:

function registerWithAzureNotificationHub() { var sas = "Endpoint=sb://eventpusher-ns.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=69XuYoluyBKl6JkkN03Z1oNC7cFSZ4Ku0ZWmPuWoJzs="; var data = '<?xml version="1.0" encoding="utf-8"?>\ <entry xmlns="http://www.w3.org/2005/Atom">\ <content type="application/xml">\ <MpnsRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">\ <Tags>myTag, myOtherTag</Tags>\ <ChannelUri>https://eventpusher-ns.servicebus.windows.net/eventpusher</ChannelUri>\ </MpnsRegistrationDescription>\ </content>\ </entry>'; if (AppMobi.iswp8) { window.alert("IS WP8"); } else { window.alert("IS NOT WP8"); } $.ajax({ type:"POST", url: "https://eventpusher-ns.servicebus.windows.net/EVENTPUSHER/registrations/?api-version=2013-08", contentType: "application/atom+xml;type=entry;charset=utf-8", headers: { "Authorization": sas, "x-ms-version": "2013-08" }, dataType: "xml", data: data, success: function(d) { window.alert("SUCCESS!"); }, error: function(msg) { window.alert("FAILURE:" + JSON.stringify(msg)); } }); window.alert("SENT!"); } 

In the above case, I am using Intel XDK with code running on a WP8 device, so I register for MPNS (Microsoft Push Notification Service).

The above code fails and returns without descriptive information about the cause of the error.

Questions:

  • Is it possible to register a mobile device for the Azure Notification Hub from javascript code using REST services?
  • What could be wrong with the above code? Is ChannelUri the correct Uri?
+4
azure azure-notificationhub
source share
2 answers

It is definitely possible to use the REST interface from javascript. There are two main problems in the code:

  • in ChannelURI you must put the URI channel extracted from the HttpPushNotificationChannel of the WindowsPhone (as in this tutorial).
  • The authorization header is the token created for your specific request. As described here

Sample using WinJS is available . We will be working with a special PhoneGap sample very soon!

+4
source share

I posted a snippet on the server side to register the device with a marker in the hub and so that you can send a notification here:

How to register devices in the Azure Notification Hub from the server side (using NodeJS sdk)?

I also have client-side code using ngCordova and PushPlugin in Ionic, let me know if anyone wants to see it.

+1
source share

All Articles