1.用nodejs-apn测试
先上可运行代码:
"use strict";
const apn = require("apn");
// token
let tokens = ["3122222d40f03b4024f81f04d8c2a3777b3ec35eb5604c7ab53d599bb84f59ff"];
let service = new apn.Provider({
cert: "./certdev.pem",//你的生成的push证书pem 对应环境(沙盒或正式)
key: "./keydev.pem",//你的生成的push私钥pem 对应环境(沙盒或正式)
// gateway: "api.development.push.apple.com",
gateway: "gateway.sandbox.push.apple.com",//沙盒环境地址
// gateway: "gateway.push.apple.com"; //正式环境地址
// port: 2195, //
pfx:"./xxxx.p12",//你生成的p12文件
topic:"com.xxx.yyy",//你的bundleid
passphrase: "123456"//你的私钥密码
});
//发送的推送通知
let note = new apn.Notification({
alert: "sssssss-788788",
});
note.topic = "com.xxx.yyy"//
console.log(`Sending: ${note.compile()} to ${tokens}`);
service.send(note, tokens).then( result => {
console.log("sent:", result.sent.length);
console.log("failed:", result.failed.length);
console.log(result.failed);
});
service.shutdown();
报错1原因:是因为service没填 pfx的参数
error: Error: Unable to load PFX certificate
报错2原因:填的topic不正确
failed: 2
[
{
device: 'xxx',
status: '400',
response: { reason: 'DeviceTokenNotForTopic' }
}
]
2.用smartpush,开源xcode工程测试。
因版本久远,沙盒环境下,要修改 SecManager.mm文件的 isPushCertificate方法,添加一行代码:
|| [name rangeOfString:@"Apple Sandbox Push Services:"].location != NSNotFound
刚上github,看到有人提交了,这个问题应该不久后会合并解决的。