Azure Notification Hub registration from Javascrip

2019-06-27 06:20发布

I'm trying to get a registration on the Azure Notification Hub working from html/javascript code running in a web view host (Phonegap / Intel XDK). There is no client library available, so I try 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 use the Intel XDK with the 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:

  1. Is it possible to register a mobile device for Azure Notification Hub from javascript code using REST services?
  2. What could be wrong with the above code? Is the ChannelUri the correct Uri?

2条回答
2楼-- · 2019-06-27 06:32

I did post a server side snippet to register the device with token to the hub and to be able to send out the notification here :

How to register devices to Azure Notification Hub from server side(with NodeJS sdk) ?

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

查看更多
可以哭但决不认输i
3楼-- · 2019-06-27 06:35

It is definitely possible top use the REST interface from javascript. In your code there are two main problems:

  1. in the ChannelURI you should put the channelURI retrieved from the WindowsPhone HttpPushNotificationChannel (as in this tutorial).
  2. the authorization header is a token that is created for your specific request. As described here

A sample using WinJS is available. We will work on having a PhoneGap specific sample very soon!

查看更多
登录 后发表回答