如何使用Azure SDK为节点安排推送通知

时间:2019-07-18 03:57:15

标签: node.js azure apple-push-notifications azureservicebus azure-notificationhub

我知道在.net中是可行的,我可以在这里https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-send-push-notifications-scheduled上找到参考。但是我想知道如何在节点中做到这一点。有人可以指导我吗?

1 个答案:

答案 0 :(得分:1)

您可以使用REST API在Node中发送计划的通知。使用规范发送常规通知,并将/messages替换为/schedulednotifications。您还需要添加一个标题,指定日期时间为ServiceBusNotification-ScheduleTime

使用模板架构的示例:

var CryptoJS = require("crypto-js");
var axios = require("axios");


var getSelfSignedToken = function(targetUri, sharedKey, keyName,
    expiresInMins) {
    targetUri = encodeURIComponent(targetUri.toLowerCase()).toLowerCase();

    // 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="
    + base64UriEncoded + "&se=" + expires + "&skn=" + keyName;
    // console.log("signature:" + token);
    return token;
    };


var keyName = "<mykeyName>";
var sharedKey = "<myKey>";
var uri = "https://<mybus>.servicebus.windows.net/<myhub>";
var expiration = 10;

var token = getSelfSignedToken(uri, sharedKey, keyName, expiration);

const instance = axios.create({
    baseURL: uri,
    timeout: 100000,
    headers: {
        'Content-Type': 'application/octet-stream',
        'X-WNS-Type': 'wns/raw',
        'ServiceBusNotification-Format' : 'template',
        'ServiceBusNotification-ScheduleTime': '2019-07-19T17:13',
        'authorization': token}
  });

var payload = { 
            "alert" : " This is my test notification!"
    };


instance.post('/schedulednotifications?api-version=2016-07', payload)
  .then(function (response) {
    console.log(response);
  }).catch(function (error) {
    // handle error
    console.log(error);
  });