使用Microsoft Notification Hubs REST API创建注册



我一直在尝试在Amy PhoneGap应用程序中实现通知中心。由于是电话盖,我们没有其他选项,而不是使用通知轮毂REST API。所以我有以下

var REQUEST_URI = "https://{namespace}.servicebus.windows.net/{NotificationHub}/registrations/?api-version=2013-08";
            var token = global_getSelfSignedToken(REQUEST_URI, 60);
            console.log("token: " + token);
            var data = '<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<content type="application/xml">
    <GcmRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
        <Tags>myTag, myOtherTag</Tags>
        <GcmRegistrationId>{GCM Registration Id}</GcmRegistrationId> 
    </GcmRegistrationDescription>
</content>
</entry>';
            $.ajax({
                type: "POST",
                url: REQUEST_URI,
                data: data,
                headers: {
                    "Authorization": token,
                    "Content-Type": 'application/atom+xml;type=entry;charset=utf-8',
                    "x-ms-version": "2013-08"
                },
                beforeSend: function() {
                    console.log("Trying to reach Notification Hub");
                },
                success: function(data, status, response) {
                    console.log("success!!!");
                },
                error: function(response, status, error) {
                    console.log("status: " + status);
                    console.log("error: " + error);
                }
            });

和global_getSeeldSignedToken方法

function global_getSelfSignedToken(targetUri,
        expiresInMins) {
    targetUri = encodeURIComponent(targetUri.toLowerCase()).toLowerCase();
    //prepare SAS key
    var parts = NOTOFICATION_HUB_FULL_ACCESS_KEY.split(';');
    if (parts.length != 3)
        throw "Error parsing connection string";
    parts.forEach(function(part) {
        if (part.indexOf('Endpoint') == 0) {
            endpoint = 'https' + part.substring(11);
        } else if (part.indexOf('SharedAccessKeyName') == 0) {
            sharedKey = part.substring(20);
        } else if (part.indexOf('SharedAccessKey') == 0) {
            ruleId = part.substring(16);
        }
    });
// Set expiration in seconds
    var expireOnDate = new Date();
    expireOnDate.setMinutes(expireOnDate.getMinutes() + expiresInMins);
    var expires = Date.UTC(expireOnDate.getUTCFullYear(), expireOnDate
            .getUTCMonth(), expireOnDate.getUTCDate(), expireOnDate
            .getUTCHours(), expireOnDate.getUTCMinutes(), expireOnDate
            .getUTCSeconds()) / 1000;
    var tosign = targetUri + 'n' + expires;
// using CryptoJS
    var signature = CryptoJS.HmacSHA256(tosign, sharedKey);
    var base64signature = signature.toString(CryptoJS.enc.Base64);
    var base64UriEncoded = encodeURIComponent(base64signature);
// construct autorization string
    var token = "SharedAccessSignature sr=" + targetUri + "&sig="
            + base64signature + "&se=" + expires + "&skn=" + ruleId;
// console.log("signature:" + token);
    return token;
}

参考:http://msdn.microsoft.com/en-us/library/windowsazure/dn223265.aspxhttp://msdn.microsoft.com/en-us/library/windowsazure/dn495631.aspx

问题:我总是得到未经授权的响应

有人尝试过使用PhoneGap的NotificationHub?

应使用http协议传递targeturi参数,您将使用https传递。

最新更新