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

2024-01-05

我正在尝试通过在 Web 视图主机(Phonegap / Intel XDK)中运行的 html/javascript 代码在 Azure 通知中心上进行注册。没有可用的客户端库,因此我尝试使用 REST API(文档:)。

我有以下 JavaScript 代码:

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!");
}

在上述情况下,我使用 Intel XDK 以及在 WP8 设备上运行的代码,因此我注册了 MPNS(Microsoft 推送通知服务)。

上面的代码失败,并返回,但没有有关错误原因的描述信息。

问题:

  1. 是否可以使用 REST 服务从 JavaScript 代码注册 Azure 通知中心的移动设备?
  2. 上面的代码可能有什么问题? ChannelUri 是正确的 Uri 吗?

绝对可以使用 javascript 的 REST 接口。 您的代码中有两个主要问题:

  1. 在 ChannelURI 中,您应该将从 WindowsPhone HttpPushNotificationChannel 检索到的 channelURI 放入其中(如this http://www.windowsazure.com/en-us/manage/services/notification-hubs/get-started-notification-hubs-wp8/教程)。
  2. 授权标头是为您的特定请求创建的令牌。如上所述here http://msdn.microsoft.com/en-us/library/windowsazure/dn170477.aspx

使用 WinJS 的示例是可用的 http://code.msdn.microsoft.com/windowsapps/Service-Bus-Notification-3bfcd161。我们将很快制作一个特定于 PhoneGap 的示例!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐