在用户点击通知后,实际上在 userNotificationCenter 中获取推送 PAYLOAD?
Get a push PAYLOAD, after user tap-on-notification, actually in userNotificationCenter?
在您的应用委托中
import UserNotifications
class AppDelegate: ... UNUserNotificationCenterDelegate {
如果你:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("notification tapped to open app ....")
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("notification arrived while app in foreground!")
completionHandler(.alert)
}
当你
- 获取通知
确实
- 用户点击打开通知
然后
- 您的应用程序将打开精美
和
- 的确,上面的函数会完美启动,
您将在控制台上看到 'notification tapped to open app ...'。
但是在那些函数中,
你怎么得到实际推送的 PAYLOAD?
(也就是说 .data
值......或者实际上是有效负载的任何部分。例如,如果您使用的是出色的 github.com/AndrewBarba/apns2 您会想要正是在它有例子 data: {
... )
的地方
你是怎么在 UserNotifications
委托函数中获得通知(负载)的?!
同样,这是用户 *** 实际打开应用程序的时候,只需点击(即 "slide, open")通知即可。
我不是在讨论在后台唤醒应用程序的难题(例如,下载文件等)。
你到底是怎么得到有效载荷的?
npm-apns2 的工作人员友情提供的答案
对于像
这样的数据
let bn = new BasicNotification(deviceToken, 'Teste', {
data: {
name: 'jack',
street: 'jones'
} })
所以...
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler:
@escaping () -> Void) {
print("notification tapped, app opens")
let userInfo = response.notification.request.content.userInfo
let name: String? = userInfo["name"] as! String?
let street: String? = userInfo["street"] as! String?
let test3: String? = userInfo["typo"] as! String?
print("> \(name)") // optional 'jack'
print("> \(street)") // optional 'jones'
print("> \(test3)") // nil
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("notification arrived while app in foreground (user has
not 'tapped on notification' - you get this immediately)")
let userInfo = notification.request.content.userInfo // sic
let name: String? = userInfo["name"] as! String?
let street: String? = userInfo["street"] as! String?
let test3: String? = userInfo["typo"] as! String?
print("> \(name)") // optional 'jack'
print("> \(street)") // optional 'main'
print("> \(test3)") // nil
completionHandler(.alert)
}
就是这样。
在您的应用委托中
import UserNotifications
class AppDelegate: ... UNUserNotificationCenterDelegate {
如果你:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("notification tapped to open app ....")
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("notification arrived while app in foreground!")
completionHandler(.alert)
}
当你
- 获取通知
确实
- 用户点击打开通知
然后
- 您的应用程序将打开精美
和
- 的确,上面的函数会完美启动,
您将在控制台上看到 'notification tapped to open app ...'。
但是在那些函数中,
你怎么得到实际推送的 PAYLOAD?
(也就是说 .data
值......或者实际上是有效负载的任何部分。例如,如果您使用的是出色的 github.com/AndrewBarba/apns2 您会想要正是在它有例子 data: {
... )
你是怎么在 UserNotifications
委托函数中获得通知(负载)的?!
同样,这是用户 *** 实际打开应用程序的时候,只需点击(即 "slide, open")通知即可。
我不是在讨论在后台唤醒应用程序的难题(例如,下载文件等)。
你到底是怎么得到有效载荷的?
npm-apns2 的工作人员友情提供的答案
对于像
这样的数据let bn = new BasicNotification(deviceToken, 'Teste', {
data: {
name: 'jack',
street: 'jones'
} })
所以...
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler:
@escaping () -> Void) {
print("notification tapped, app opens")
let userInfo = response.notification.request.content.userInfo
let name: String? = userInfo["name"] as! String?
let street: String? = userInfo["street"] as! String?
let test3: String? = userInfo["typo"] as! String?
print("> \(name)") // optional 'jack'
print("> \(street)") // optional 'jones'
print("> \(test3)") // nil
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("notification arrived while app in foreground (user has
not 'tapped on notification' - you get this immediately)")
let userInfo = notification.request.content.userInfo // sic
let name: String? = userInfo["name"] as! String?
let street: String? = userInfo["street"] as! String?
let test3: String? = userInfo["typo"] as! String?
print("> \(name)") // optional 'jack'
print("> \(street)") // optional 'main'
print("> \(test3)") // nil
completionHandler(.alert)
}
就是这样。