显示实现APNS 的方式有两种,一种是之前的push 证书的管理,另一种就是apns auth key。
推送证书有生产模式,开发者模式,使用起来非常的不方便。如果使用apns auth key就可以解决上述所有问题。所以还是建议大家使用auth key.
1.如何生成apns auth key?
首先你的是developer.apple.com 的admin权限 会看到证书那一列有个Keys 选项
<1>新建一个apns auth key,Name按照规则填写,然后选择APNs,点击continue

<2.>
点击 “Continue” 让你确认信息,再点击 “confirm”,就可以下载该 key了。(.p8 密钥文件)
(注意:记下 key id(.p8 密钥文件),而且只可以下载一次,请妥善保存。)

<3.>获取你之前创建过的应用的 Bundle ID
<4.>获取 Team ID
<5.>在推送平台控制台上,进入你应用的应用设置中 iOS 的鉴权方式选择 “Token Authentication”,上传 auth key 文件,并填写你的 KEY ID,TeamID,和指定应用的 BundleID。推送平台会在后台为你的应用进行鉴权

新的token验证推送,一个认证密钥可用于多个App服务,没有时效性。不用为每个App需要单独配置两个(开发环境和生产环境)证书,还要注意过没过期。
如何本地验证Auth key 配置的APNS
1.搭建测试环境,进入终端
mkdir apns
cd apns
npm init --yes
npm install apn --save
2.编辑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);
});
3.运行app.js
node app.js
运行结果
{ sent:
[ { device: '设备的devicetoken' } ],
failed: [] }
如果推送成功就会直接推送到该设备上
如果推送失败 可以对应的查看相关错误
//可以查看官方文档
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html



网友评论