美文网首页
nodejs IOS消息远程推送

nodejs IOS消息远程推送

作者: 马贞晓 | 来源:发表于2018-11-28 15:36 被阅读0次
  • 在基本配置在conf里production true和false对应源码config.js中apple远程生产和开发地址,相当智能,省略原来配置gateway 和port,其中gateway在新的版本中没发现应该是被废除了。
let EndpointAddress = {
  production: "api.push.apple.com",
  development: "api.development.push.apple.com"
};
  • 具体实现代码
let tokens=['你的token'];//String|Array
 let conf = {
            cert:path.resolve(__dirname,"pem/apns-cer.pem"),
            key: path.resolve(__dirname,"/pem/apns-cer-key.pem"), 
            production:false,
            passphrase: "密码" //pem证书密码
          }

        let service = new apn.Provider(conf);
          
        var note = new apn.Notification();
            note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
            note.badge = 3;
            note.alert = "sinoocean提示新消息。";
          // 主题 一般取应用标识符(bundle identifier)
            note.topic = "com.sinoocean.MobileMarket";
 
            service.send(note, tokens).then( result => {

              console.log("sent:", result.sent.length);
              console.log("failed:", result.failed.length);
    
             if(result.sent.length){
                let id = result.sent.map(d=>d.device).join(",");
                Log.trace(id+"发送消息成功。")
             }
             if(result.failed.length){
                Log.error(result.failed);
             }
          });
          
         
    }

相关文章

网友评论

      本文标题:nodejs IOS消息远程推送

      本文链接:https://www.haomeiwen.com/subject/yktzqqtx.html