我的設備沒有收到後臺推送通知。我很有智慧,我嘗試了每個可以通過Stack Overflow找到並搜索到的教程。iOS沒有收到使用apn nodejs的後臺推送通知
- 設備和APN設置爲調試/開發
- 我可以成功註冊設備令牌
- 我有推送通知服務
- APN積極.p8鍵說,推送消息發送成功
- didReceiveRemoteNotification不火
- 嘗試使用AWS在後臺應用和前景
- :入站 - 端口22,出結合 - 所有端口
這是響應:
{ 「發送」:[{ 「設備」: 「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()
}
}
的XCode:
Node.js的
.p8關鍵是從開發者控制檯:鍵>所有
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);
});
我已經發送的iOS使用'cert.pem'和'key.pem'推送消息。我不知道這個例子會有幫助。我可以發佈它,但只是想確認這是否可能在你的情況下 –
@KukicVladimir你用什麼node.js庫發送推送通知? – Andrew
我正在使用'apn',和你一樣 –