【发布时间】:2021-12-06 04:53:42
【问题描述】:
我正在 Swift 中配置推送通知。到目前为止,我有 3 个场景。
1 - 前台应用 在前台,我认为我所做的一切都是正确的,因为我确实收到了推送通知数据。
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("userNotificationCenter willPresent")
let content = notification.request.content
UIApplication.shared.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
completionHandler([.alert, .sound])
}
2 - 用户点击推送通知横幅 这也很好用。
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("userNotificationCenter didReceive")
defer {
completionHandler()
}
guard response.actionIdentifier == UNNotificationDefaultActionIdentifier else {
return
}
let content = response.notification.request.content
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
3 - 应用在后台,然后用户进入应用 在这种情况下,推送通知会到达用户的手机。但是,他们没有点击推送通知本身,而是进入了应用程序。而且我无法从推送通知中获取任何信息
谁能帮忙配置第三种情况?谢谢。
【问题讨论】:
-
@Paulw11 你 100% 确定这一点吗?
-
其实他们已经添加了一个新的API——developer.apple.com/documentation/usernotifications/…如果用户在你的应用返回前台之前清除了通知那么他们就丢失了,但是如果他们还在通知中心你可以得到他们这样。通知不能保证送达,您不应将其作为更新应用的唯一方式
-
我听说它只在 ios 15 上可用。但我会检查一下。谢谢
标签: ios swift push-notification notifications firebase-cloud-messaging