【发布时间】:2017-07-14 20:13:02
【问题描述】:
我的设备没有收到后台推送通知。我束手无策,我已经尝试了所有可以找到并通过 Stack Overflow 搜索的教程。
- 设备和 APN 设置为调试/开发
- 我可以成功注册设备令牌
- 我有一个带有推送通知服务的活动 .p8 密钥
- APN 表示推送消息发送成功
- didReceiveRemoteNotification 没有触发
- 尝试在后台和前台使用应用程序
- 使用 AWS:入站 - 端口 22,出站 - 所有端口
这是回复:
{"sent":[{"device":"cc7ec9a821cf232f9510193ca3ffe7d13dd756351794df3f3e44f9112c037c2a"}],"失败":[]}
这是我的代码:
AppDelegate.swift
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var deviceTokenString: String = ""
for i in 0..<deviceToken.count {
deviceTokenString += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
}
UserDefaults.standard.set(deviceTokenString, forKey: "push_token")
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
print("Handle push from foreground")
print("\(notification.request.content.userInfo)")
completionHandler([.alert, .badge, .sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Handle push from background or closed")
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
print("\(response.notification.request.content.userInfo)")
completionHandler()
}
}
X代码:
Node.js
.p8 密钥来自开发者控制台:Keys > All
var apn = require('apn');
var apnProvider = new apn.Provider({
token: {
key: "/path/to/AuthKey_1234ABCD.p8",
keyId: "1234ABCD",
teamId: "1234ABCD"
},
production: false
});
var note = new apn.Notification();
note.topic = "com.company.app";
note.payload = {
aps: {
"content-available" : 1
},
custom_field: {}
};
apnProvider.send(note, push_token).then(result => {
console.log('result: ', result);
console.log("sent:", result.sent.length);
console.log("failed:", result.failed.length);
});
【问题讨论】:
-
我一直使用
cert.pem和key.pem发送iOS 推送消息。我不知道那个例子是否会有所帮助。我可以发布它,但只是想确认您的情况是否可行 -
@KukicVladimir 你用什么 node.js 库来发送推送通知?
-
我正在使用
apn,和你一样 -
你的 node-apn 是最新的吗?服务器名称最近更改了。你能检查通知被发送到哪些服务器名称吗?
标签: ios node.js swift apple-push-notifications