从Javascript Azure的通知中心注册使用REST服务失败 [英] Azure Notification Hub registration from Javascript using REST services fails

查看:214
本文介绍了从Javascript Azure的通知中心注册使用REST服务失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让在Azure通知集线器从HTML /工作登记的javascript code运行在Web视图主机(的PhoneGap /英特尔XDK)。没有可用的客户端库,所以我尝试使用REST API(文件:)。

我有以下的Javascript code:

 函数registerWithAzureNotificationHub()
{
    VAR SAS = \"Endpoint=sb://eventpusher-ns.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=69XuYoluyBKl6JkkN03Z1oNC7cFSZ4Ku0ZWmPuWoJzs=\";
    VAR数据='< XML版本=1.0编码=UTF-8> \\
    <入境的xmlns =htt​​p://www.w3.org/2005/Atom> \\
        <内容类型=application / xml进行> \\
        < MpnsRegistrationDescription的xmlns:I =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns =htt​​p://schemas.microsoft.com/netservices/2010/10/servicebus/connect> \\
        <&标签GT; MyTag的,myOtherTag< /标签> \\
        &LT; ChannelUri&GT;的https://eventpusher-ns.servicebus.windows.net/eventpusher< / ChannelUri&GT; \\
        &LT; / MpnsRegistrationDescription&GT; \\
        &LT; /内容和GT; \\
    &LT; /进入&GT;';
    如果(AppMobi.iswp8){
        window.alert(IS WP8);
    }
    其他
    {
        window.alert(IS NOT WP8);
    }    $阿贾克斯({
        键入:POST,
        网址:https://eventpusher-ns.servicebus.windows.net/EVENTPUSHER/registrations/?api-version=2013-08
        的contentType:应用程序/原子+ XML;类型=项;字符集= UTF-8,
        标题:{
            授权:SAS,
            X-MS-版本:2013-08
        },
        数据类型:XML,
        数据:数据,
        成功:函数(D){window.alert(成功了!); },
        错误:函数(MSG){window.alert(失败:+ JSON.stringify(MSG)); }
    });
    window.alert(发送!);
}

在上述情况下,我使用英特尔XDK与WP8设备上运行的code,所以我注册MPNS(微软推送通知服务)。

以上code失败,没有对错误原因的描述性信息的回报。

问题:


  1. 是否有可能使用REST服务,注册从JavaScript code的Azure通知中心的移动设备?

  2. 什么可能是错误的上述code?是ChannelUri正确的URI?


解决方案

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


  1. 在ChannelURI你应该把从WindowsPhone的HttpPushNotificationChannel检索(如<一的channelURI href=\"http://www.windowsazure.com/en-us/manage/services/notification-hubs/get-started-notification-hubs-wp8/\"相对=nofollow>本教程)。

  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!

这篇关于从Javascript Azure的通知中心注册使用REST服务失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆