gpt4 book ai didi

node.js - 如何使用azure sdk for Node调度推送通知

转载 作者:行者123 更新时间:2023-12-02 23:28:05 25 4
gpt4 key购买 nike

我知道在.net中这是可能的,我可以在这里看到引用https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-send-push-notifications-scheduled 。但我想知道如何在 Node 中做到这一点。任何人都可以指导我吗?

最佳答案

您可以使用 REST API 在 Node 中发送计划通知。使用发送普通通知的规范,并将 /messages 替换为 /schedulednotifications。您还需要添加一个 header ,指定名为 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);
});

关于node.js - 如何使用azure sdk for Node调度推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57086915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com