1、打开钥匙串找到推送证书
2、到处证书为cert.cer
3、导出私钥为key.p12
4、转换格式
$ openssl
OpenSSL> x509 -in cert.cer -inform DER -outform PEM -out cert.pem
OpenSSL> pkcs12 -in key.p12 -out key.pem -nodes
5、创建node工程文件夹,安装apn
npm install apn
6、创建推送实现文件apn.js
"use strict";
/**
Send individualised notifications
i.e. Account updates for users with one-or-more device tokens
*/
const apn = require("apn");
let users = [
{ name: "Wendy", "devices": ["2f50bc64e1434b39da31dd417b05e414477545f29893a06c3cf5235bafb5de2f", "2f50bc64e1434b39da31dd417b05e414477545f29893a06c3cf5235bafb5de2f"]},
// { name: "John", "devices": ["2f50bc64e1434b39da31dd417b05e414477545f29893a06c3cf5235bafb5de2f"]},
];
let service = new apn.Provider({
cert: "cert.pem",
key: "key.pem",
});
users.forEach( (user) => {
let note = new apn.Notification();
// note.alert = `Hey ${user.name}, I just sent my first Push Notification`;
// note.type = 'activity';
//自定义json
// note.rawPayload = {"aps":{"alert":{"title":"标题","subtitle":"子标题","body":"内容内容 进入7171房间"},"mutable-content":1},"type":"xxx","id":"7171","roomSource":"35","roomMode":"0","media":{"url":"https://ares.xxxx.com/kktv/poster/20211125/14/5888_1936279.jpg"}};
note.rawPayload = {"aps":{"alert":{"title":"标题","subtitle":"子标题","body":"打开百度"},"mutable-content":1},"type":"activity","url":"https://www.baidu.com","media":{"url":"https://ares.xxxx.com/kktv/poster/20211125/14/5888_1936279.jpg"}};
// The topic is usually the bundle identifier of your application.
note.topic = "com.xxx.xxx";
console.log(`Sending: ${note.compile()} to ${user.devices}`);
service.send(note, user.devices).then( result => {
console.log("sent:", result.sent.length);
console.log("failed:", result.failed.length);
console.log(result.failed);
});
});
// For one-shot notification tasks you may wish to shutdown the connection
// after everything is sent, but only call shutdown if you need your
// application to terminate.
service.shutdown();