[英]How to send voip push notification from node js? I can send voip push from curl but not node
I am making ios app using voip push notification.我正在使用 voip 推送通知制作 ios 应用程序。 I want to send voip push notification from node js, but not well.我想从节点 js 发送 voip 推送通知,但效果不佳。
I read this tutorial CallKit iOS Swift Tutorial for VoIP Apps (Super Easy) and I made ios app using voip push.我阅读了本教程CallKit iOS Swift VoIP 应用程序教程(超级简单) ,我使用 voip push 制作了 ios 应用程序。 I can send voip push from curl command.我可以从 curl 命令发送 voip push。
curl -v -d '{"aps":{"alert":"hello"}}' --http2 --cert chara.pem:passphase https://api.push.apple.com/3/device/ede0d5e78f771d5916345aa48bd098e86aeab40b5e7d985fb9c74586d1a5a681
node index.js
const http2 = require('http2');
const fs = require('fs');
exports.handler = async (event) => {
const bundleID = 'com.swiswiswift.CharacterAlarm'
const deviceToken = 'ede0d5e78f771d5916345aa48bd098e86aeab40b5e7d985fb9c74586d1a5a681'
const headers = {
'apns-topic': bundleID
};
const options = {
protocol: 'https:',
method: 'POST',
hostname: 'api.push.apple.com',
path: '/3/device/' + deviceToken,
headers: headers,
cert: fs.readFileSync('chara.pem'),
passphrase: "passphrase"
};
const client = http2.connect('api.push.apple.com')
const req = client.request(options)
req.setEncoding('utf8')
req.on('response', (headers, flags) => {
console.log(headers)
});
let data = ''
req.on('data', (d) => data += d)
req.on('end', () => client.destroy())
req.end()
}
exports.handler('local-test')
[Object: null prototype] {
':status': 405,
'apns-id': 'xxxxxxx-xxxx-xxxx-xxx-xxxxxxxx' }
var apn = require("apn");
var deviceToken = "device token";
var service = new apn.Provider({
cert: '.path to /cert.pem', key:'pat to ./key.pem'
});
var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 60; // Expires 1 minute from now.
note.badge = 3;
note.sound = "ping.aiff";
note.alert = " You have a new message";
note.payload = {'messageFrom': 'Rahul test apn'};
note.topic = "(Bundle_id).voip";
note.priority = 10;
note.pushType = "alert";
service.send(note, deviceToken).then( (err,result) => {
if(err) return console.log(JSON.stringify(err));
return console.log(JSON.stringify(result))
});
This is a working code for apn voip push这是 apn voip push 的工作代码
also refer this也参考这个
https://alexanderpaterson.com/posts/send-ios-push-notifications-with-a-node-backend https://alexanderpaterson.com/posts/send-ios-push-notifications-with-a-node-backend
I'm using a slightly different approach:我正在使用稍微不同的方法:
If you don't know where to find or how to create the key, keyId and teamID, have a look at this article on Medium.如果您不知道在哪里找到或如何创建密钥、keyId 和 teamID,请查看 Medium 上的这篇文章。 He explains all of this in detail.他详细解释了这一切。
var apn = require('apn');
const { v4: uuidv4 } = require('uuid');
const options = {
token: {
key: 'APNKey.p8',
keyId: 'S83SFJIE38',
teamId: 'JF982KSD6f'
},
production: false
};
var apnProvider = new apn.Provider(options);
// Sending the voip notification
let notification = new apn.Notification();
notification.body = "Hello there!";
notification.topic = "com.myapp.voip";
notification.payload = {
"aps": { "content-available": 1 },
"handle": "1111111",
"callerName": "Richard Feynman",
"uuid": uuidv4()
};
apnProvider.send(notification, deviceToken).then((response) => {
console.log(response);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.