且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Javascript Azure的通知中心注册使用REST服务失败

更新时间:2023-12-03 17:19:34

这绝对是可能的顶部从JavaScript中使用REST接口。
在您的code有两个主要问题:


  1. 在ChannelURI你应该把从WindowsPhone的HttpPushNotificationChannel检索(如本教程)。

  2. 授权头是针对您的具体要求创建一个令牌。如上所述这里

使用WinJS样本是提供。我们将努力在很快有一个特定的PhoneGap样本!

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?

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!