iOS没有使用apn nodejs接收后台推送通知
我的设备没有收到后台推送通知。 我在智慧结束,我试过每个教程,我可以find并通过堆栈溢出search。
- 设备和APN被设置为debugging/开发
- 我可以成功注册设备令牌
- 我有一个活动的P8键与推送通知服务
- APN表示推送消息已成功发送
- 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键来自Developer Console: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); });
我不确定是否这样,因为您的服务器正在使用此APN节点模块,我从来没有使用它,但我使用Firebase,每次我尝试使用content_available : 1
,它不起作用。 什么工作是content_available : true
。
希望这对你有用。
我感谢大家的意见,我想出了这个问题。
'aps'不属于有效载荷字段,而是应该在Notification构造函数中。
Node.js的:
var note = new apn.Notification({ aps: { "content-available" : 1 } }); note.payload = { custom_field_1: {}, custom_field_2: '' };
你在你的AppDelegate上写了这个
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { print("Silent push notification received: \(userInfo)") }
但是你也写道:
center.delegate = notificationDelegate
这意味着你是AppDelegate 不是通知中心的委托。
要么改变
center.delegate = self
或者只是将该函数移除到采用它的类,即您的UYLNotificationDelegate
你似乎没事。 虽然我不知道如何读取node.js,所以你可能已经configuration你的负载不正确。 有时候,如果服务器没有正确pipe理多个设备令牌,您可能不会收到任何安全通知,或者我们与后端发生的另一个问题是,当我们testing通知时,他们正在将通知服务器redirect到本地计算机,显然他们没有进来!
苹果公司的示例有效载荷应该如下所示:
{ "aps" : { "content-available" : 1 }, "acme1" : "bar", "acme2" : 42 }
你确定你生成的有效载荷有一个相似的格式?!
-
我错误地build议,你应该改变
center.delegate = notificationDelegate
到center.delegate = self
但如果我是那绝望,我会给这个尝试:D。 -
确保背景应用程序刷新已启用(虽然仍然没有启用,你应该在前台收到
-
如果这不起作用,然后尝试发送一个警告/徽章,看看是否有效。 可能它不会工作:/
- 删除应用程序,然后再次安装。 有时如果您只是重新安装授权不正确的工作。
- 干净的派生数据,干净的生成,重新启动Xcode。
编辑完成后:
您不应该删除didReceive Remote Notification < – 用于检索具有content-available : 1
任何推送通知content-available : 1
。
-
willPresent
是callback如果应用程序在FOREGROUND中该怎么做。 仅当有效载荷在有效载荷中具有警报或徽章或声音时才适用 -
didReceiveNotificationResponse
不是你所想的那样。 它用于处理pipe理用户从通知中select的操作,例如,您的通知有两个操作(Snooze,删除),如下所示点击:
您使用此callback处理通知的响应 。
-
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
用于在到达时检索通知。
所以从技术上讲,如果你只是有一个沉默的通知,你只会得到第三个callback,而不是前两个。
如果你有一个应用程序在前台,有效载荷有: content-available :1
也有动作,用户点击动作,那么你将首先得到第三个callback,然后是第一个callback,然后是第二个callback。