APNS auth key制作教程

1.首先我们需要了解什么是apns auth key,下面是官方的描述

Use the Apple Push Notification service for your notification requests. One key is used for all of your apps.(使用苹果推送通知服务通知服务,一个密钥可以用于你所有的应用程序)

而且正常的证书对应不同的app,推送证书又有生产模式,开发者模式,使用起来非常的不方便。如果使用apns auth key就可以解决上述所有问题。

2.如何生成apns auth key?

(1)新建一个apns auth key,Name按照规则填写,然后选择APNs,点击continue

(2)之后点击confirm,最后下载证书到本地

(3)搭建测试环境,进入终端

mkdir apns
cd apns
npm init --yes  
npm install apn --save

(4)编辑app.js文件,并将刚才下载的证书命名为apns.p8拷贝到apns目录下,需要注意production字段,是否是生产还是开发者环境通过这个字段来判断

var apn = require('apn');

// Set up apn with the APNs Auth Key
var apnProvider = new apn.Provider({  
     token: {
        key: 'apns.p8', // Path to the key p8 file
        keyId: 'ABCD12345', // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/certificate/key)
        teamId: 'ABCD12345', // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/)
    },
    production: true // Set to true if sending a notification to a production iOS app
});

// Enter the device token from the Xcode console
var deviceToken = '需要推送设备的devicetoken';

// Prepare a new notification
var notification = new apn.Notification();

// Specify your iOS app's Bundle ID (accessible within the project editor)
notification.topic = 'App的Bundle id';

// Set expiration to 1 hour from now (in case device is offline)
notification.expiry = Math.floor(Date.now() / 1000) + 3600;

// Set app badge indicator
notification.badge = 1;

// Play ping.aiff sound when the notification is received
notification.sound = 'ping.aiff';

// Display the following message (the actual notification text, supports emoji)
notification.alert = 'Hello World \u270C';

// Send any extra payload data with the notification which will be accessible to your app in didReceiveRemoteNotification
notification.payload = {id: 123};

// Actually send the notification
apnProvider.send(notification, deviceToken).then(function(result) {  
    // Check the result for any failed devices
    console.log(result);
});

(5)运行app.js

node app.js

 运行结果

{ sent:
   [ { device: '设备的devicetoken' } ],
  failed: [] }

 

posted @ 2017-10-10 15:01  shuffle  阅读(3325)  评论(0编辑  收藏  举报