Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send voip push notification from node js? I can send voip push from curl but not node

Tags:

node.js

ios

voip

I am making ios app using voip push notification. I want to send voip push notification from node js, but not well.

I read this tutorial CallKit iOS Swift Tutorial for VoIP Apps (Super Easy) and I made ios app using voip push. I can send voip push from curl command.

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' }
like image 656
J.Ono Avatar asked Dec 18 '22 15:12

J.Ono


1 Answers

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

also refer this

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

like image 137
Rahul Somaraj Avatar answered May 03 '23 21:05

Rahul Somaraj