Nodejs给苹果客户端(iphone/ipad)发送推送apn
Nodejs给苹果客户端(iphone/ipad)发送推送apn
如题: 参考资料http://iosshare.cn/?p=1133 其中制作cert.pem 和 key.pem都成功 代码使用的是
var options = {
cert: '../pem/apns-prod-cert.pem', /* Certificate file path */
key: '../pem/apns-prod-key.pem', /* Key file path */
gateway: 'gateway.sandbox.push.apple.com',/* gateway address gateway.push.apple.com, port 2195*/
port: 2195, /* gateway port */
errorCallback: errorHappened , /* Callback when error occurs function(err,notification) */
};
var apnsConnection = new apns.Connection(options);
var myDevice = new apns.Device(goodToken);
var note = new apns.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 1;
note.sound = 'ping.aiff';
note.alert = 'You have a new message';
note.payload = {'messageFrom': 'Caroline'};
apnsConnection.pushNotification(note,myDevice);
note.device = myDevice;
apnsConnection.sendNotification(note);
当前确定这段代码是执行了的,没有问题,但是我的iphone设备无法接收到通知。
请问有没有哪位大侠曾经做过类似的东西,指点一二!
确定token正确吗 有没有把中间的空格去掉 发送的时候又什么输出信息么
token是正确的,空格已经去掉, 使用apn 时输出过
send:5423!!!(这里是我的token) open socket 这样两行文字在终端里
现在我node服务端已经把通知推送出去了,而且没报任何错误,但是手机就是接受不到,问什么?
根据你的描述,你已经成功生成了证书文件,并且按照参考资料中的代码尝试发送APNs(Apple Push Notification service)。但你的iPhone设备没有收到通知。以下是一些可能的原因和解决方案:
-
检查设备Token是否正确:确保
goodToken
变量中的设备Token是正确的。你可以通过调试或打印该变量来验证其值。 -
检查APNs环境:如果你的应用程序是在生产环境中运行的,那么你应该使用生产服务器的地址,而不是沙盒环境的地址。根据你的代码片段,你使用的是沙盒环境的地址:
gateway: 'gateway.sandbox.push.apple.com'
如果应用已上线到App Store,则需要将此地址更改为:
gateway: 'gateway.push.apple.com'
-
检查APNs证书:确保你上传到Apple开发者中心的APNs证书与你在本地使用的证书文件一致。同时,确保你的证书文件路径正确无误。
-
检查网络连接:确保你的服务器可以访问Apple的APNs网关地址。有时候防火墙或网络配置可能会阻止连接。
-
日志记录:增加日志记录以查看是否有任何错误发生。例如,在
errorCallback
函数中打印错误信息。
示例代码调整:
var options = {
cert: '../pem/apns-prod-cert.pem',
key: '../pem/apns-prod-key.pem',
gateway: process.env.NODE_ENV === 'production' ? 'gateway.push.apple.com' : 'gateway.sandbox.push.apple.com',
port: 2195,
errorCallback: errorHappened
};
function errorHappened(err, notification) {
console.error('Error happened:', err);
}
var apnsConnection = new apns.Connection(options);
var myDevice = new apns.Device(goodToken); // 确保这里的Token是正确的
var note = new apns.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // 有效期为1小时
note.badge = 1;
note.sound = 'ping.aiff';
note.alert = 'You have a new message';
note.payload = {'messageFrom': 'Caroline'};
apnsConnection.pushNotification(note, myDevice);
希望这些建议能帮助你解决问题!如果仍然有问题,请检查是否有其他环境因素影响。