本文介绍了APN 节点:加载 PEM 文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 apn-node 推送到我的设备.服务器托管在 Heroku 上,因此我不想提交该文件.另外,我不想从远程服务器获取它,而是将它放在一个环境变量中.

I am trying to get apn-node to push to my devices. The server is hosted on Heroku, so I do not want to commit the file. Also, I do not want to fetch it from a remote server but instead put it in an environment variable.

我已经尝试了以下操作():
我从 Apple 创建并下载了证书,现在它在我的钥匙串中.我将其导出为 *.p12 文件,并使用 openssl pkcs12 -in dev.p12 -out dev.pem -nodes 将其转换为 *.pem 文件.

I already tried the following (source):
I created and downloaded the certificate from Apple and now have it in my Keychain. I exported it as a *.p12 file and converted it with openssl pkcs12 -in dev.p12 -out dev.pem -nodes into a *.pem file.

为了设置环境变量,我做了export APN_CERT="$(cat dev.pem)".当我在我的应用程序中打印出来时,它显示的证书非常好.但是,当我实际发送通知(并且 node-apn 打开连接)时,它会抛出一个 [Error: wrong tag].
此错误由加密模块发出:

To set the environment variable, I did export APN_CERT="$(cat dev.pem)". When I print it out in my application it shows the certificate perfectly fine.However, when I actually send a notification (and node-apn opens the connection) it throws an [Error: wrong tag].
This error is emitted by the crypto module:

apn Raising error: +4ms [Error: wrong tag] undefined undefined
 apn Error occurred with trace: +1ms Error: wrong tag
  at Object.exports.createCredentials (crypto.js:176:17)
  at Object.exports.connect (tls.js:1344:27)
  at apnSocketLegacy

该模块还抛出一个APN传输错误:moduleInitialisationFailed (Code: 513).

除了这可能与节点本身的加密模块本身有关之外,我找不到任何有用的信息.这就是为什么我怀疑我在创建证书时做错了什么,但感谢您提供任何指导性建议.

I was unable to find any useful information other than that this could be related to the crypto module itself of node itself. That's why I suspect I did something wrong when creating the certificate but thankful for any guiding advice.

推荐答案

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))
  });

加载 pem 文件并使用令牌运行

Load the pem files and run with the tokens

这篇关于APN 节点:加载 PEM 文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:08