// 引入
var apn = require('apn');
// 配置
let options = {
token: {
key: "./assets/AuthKey_QW76FB2PKU.p8", // 开发者账号中生成的.p8文件
keyId: "QW76FB2PKU", // 开发者账号中生成的key
teamId: "4CA9RGENWW" // 开发者账号中设置的teamId
},
production: false
};
// 推送消息
function apnsPush(note, devToken) {
var apnProvider = new apn.Provider(options);
apnProvider.send(note, devToken).then((result) => {
// 发送成功后关闭服务
apnProvider.client.endpointManager._endpoints.forEach(endpoint => endpoint.destroy());
apnProvider.shutdown();
}).catch(err => console.log(err));
}
// 使用
var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 10;
note.badge = 3;
note.sound = "ping.aiff";
note.payload = {
'description': 'msg'
};
note.topic = "Tigerbimeeting";
note.alert = "收到一条新消息";
apnsPush(note, devices[0].DeviceToken);
yiidot / apnspush Goto Github PK
View Code? Open in Web Editor NEWIOS推送通知--push notifications