【问题标题】:How to send voip push notification from node js? I can send voip push from curl but not node如何从节点 js 发送 voip 推送通知?我可以从 curl 但不能从节点发送 voip 推送
【发布时间】:2019-09-29 05:34:50
【问题描述】:

我正在使用 voip 推送通知制作 ios 应用程序。 我想从 node js 发送 voip 推送通知,但不是很好。

我阅读了本教程CallKit iOS Swift Tutorial for VoIP Apps (Super Easy),并使用 voip push 制作了 ios 应用程序。 我可以从 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' }

【问题讨论】:

  • 我正在努力实现同样的目标。你成功了吗?
  • 首先,如果您尝试将 Voip Push 用于发送呼叫以外的用途,它将无法正常工作,并且最终会从 iOS 13、Xcode 11 开始阻塞。无论如何,来自 Nodejs 的 VOIP 推送仍然是主题,请查看 stackoverflow.com/questions/36633188/…
  • 执行 curl 命令时,我得到 "* Connection #0 to host api.push.apple.com {"reason":"BadDeviceToken"}* Closing connection 0"

标签: ios node.js voip


【解决方案1】:
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))
  });

这是 apn voip push 的工作代码

也参考这个

https://alexanderpaterson.com/posts/send-ios-push-notifications-with-a-node-backend

【讨论】:

  • DeviceTokenNotForTopic 出现错误。我在 note.topic 中给出了我的应用标识符
  • 我认为您的手机可能有用于 voip 和其他通知的单独令牌
  • @Ansar 在捆绑 ID 的末尾添加“voip”。比如,notification.topic = "yourbundleid.voip"。
【解决方案2】:

我使用的是稍微不同的方法:

如果您不知道在哪里找到或不知道如何创建密钥、keyId 和 teamID,请查看 Medium 上的 this article。他详细解释了所有这些。

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 = "my.bundleid.voip";   // Make sure to append .voip here!
notification.payload = {
  "aps": { "content-available": 1 },
  "handle": "1111111",
  "callerName": "Richard Feynman",
  "uuid": uuidv4()
};

apnProvider.send(notification, deviceToken).then((response) => {
  console.log(response);
});

【讨论】:

  • 请提到在捆绑 ID 末尾添加“voip”。比如,notification.topic = "yourbundleid.voip"。
猜你喜欢
  • 2016-02-08
  • 1970-01-01
  • 2019-10-30
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 2017-11-29
  • 1970-01-01
相关资源
最近更新 更多