如何从节点 js 发送 voip 推送通知?我可以从 curl 发送 voip 推送,但不能从节点发送

J.O*_*Ono 4 voip node.js ios

我正在使用 voip 推送通知制作 ios 应用程序。我想从节点 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
Run Code Online (Sandbox Code Playgroud)

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')

Run Code Online (Sandbox Code Playgroud)
[Object: null prototype] {
  ':status': 405,
  'apns-id': 'xxxxxxx-xxxx-xxxx-xxx-xxxxxxxx' }
Run Code Online (Sandbox Code Playgroud)

Rah*_*raj 8

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))
  });
Run Code Online (Sandbox Code Playgroud)

这是 apn voip push 的工作代码

也参考这个

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

  • 发生 DeviceTokenNotForTopic 错误。我在 note.topic 中给出了我的应用程序标识符 (2认同)

gig*_*gnu 6

我正在使用稍微不同的方法:

如果您不知道在哪里找到或如何创建密钥、keyId 和 teamID,请查看Medium 上的这篇文章。他详细解释了这一切。

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);
});
Run Code Online (Sandbox Code Playgroud)