gpt4 book ai didi

未调用 iOS 10 UNUserNotificationCenterDelegate。推送通知不起作用

转载 作者:IT王子 更新时间:2023-10-29 05:08:10 26 4
gpt4 key购买 nike

为了让推送通知在 iOS10 中工作,我把头发扯下来。当前设置:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool :

if #available(iOS 10.0, *) {

let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

if error == nil {
print("DID REQUEST THE NOTIFICATION")
UIApplication.shared.registerForRemoteNotifications()
}
}
print("DID SET DELEGATE")
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) :
print("DID REGISTER FOR A REMOTE NOTIFICATION AND THE TOKEN IS \(deviceToken.base64EncodedString())"           
let request = UpdatePushNotificationSubscription_Request(deviceToken: deviceToken)
updatePushNotificationSubscriptionWorker.updateSubscription(request)

我已检查 token 是否正确上传到后端,并且确实匹配。

我还实现了:
    @available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

print("GOT A NOTIFICATION")

}


@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

//This is for the user tapping on the notification
print("GOT A NOTIFICATION")
}

我已经为所有目标设置了权利并启用了推送:

enter image description here

现在,当我尝试从后端发送消息时,设备什么也没有收到。没有召集代表。不知道我在这里做错了什么。 Push 适用于 iOS9 和 android 设备。任何指向我可能做错了什么的指针?

最佳答案

此答案适用于 iOS 10+,使用 UserNotifications框架。

您需要一个符合 UNUserNotificationCenterDelegate 的类协议(protocol)。如果您为此创建一个新类,或者将其添加到您的 AppDelegate 中,则无关紧要。类(class)。不过,我建议创建一个专门的类(class)。出于此答案的目的,假设您创建了 UserNotificationController为它上课。

该类可以具有以下方法:
optional func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)optional func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
然后在你的AppDelegate.application(_:didFinishLaunchingWithOptions:)方法,你需要设置delegateUNUserNotificationCenter.current()反对您的 UserNotificationController 的实例类(class)。您可能想要使用共享实例。

使用 UNUserNotificationCenter.requestAuthorization(options:completionHandler:) 请求用户授权以启用通知方法,并在 completionHandler ,查看granted值(value)。如果 true , 调用UIApplication.shared.registerForRemoteNotifications() 注册远程通知.

现在,当应用程序收到推送通知时,可能会发生几种不同的情况。我将尝试在此处列出最常见的情况。

本地通知:

如果应用程序在前台,应用程序将调用 UserNotificationController .userNotificationCenter(_:willPresent:withCompletionHandler:) .

如果应用程序在后台(运行与否),则在用户点击通知之前不会调用任何内容,此时,应用程序将打开并调用 UserNotificationController .userNotificationCenter(_:didReceive:withCompletionHandler:) .

远程通知:

有效载荷的内容将影响发生的事情。有效载荷有三种情况,a) 只是正常的 alert , badge , 和 sound选项 b) 包括 content-available选项(设置为 1true ) c) 包括 mutable-content选项(设置为 1true )。另外,从技术上讲,d) 你同时拥有 content-availablemutable-content ,但这只会触发这两种情况。

对于 a) 只需 alert , sound , badge信息:

这与本地通知的工作方式相同。

对于 b) content-available == 真:

如果应用程序在前台,UserNotificationController .userNotificationCenter(_:willPresent:withCompletionHandler:)叫做。

如果应用程序在后台(运行与否),AppDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:)被调用,而不是您的 UserNotificationController 中的方法之一类(class)。

对于 c) mutable-content == 真:

如果您添加了 UNNotificationServiceExtension对于您的应用程序,它将处理通知并可以修改内容。无论您的主应用程序的状态如何,都会发生这种情况。如果用户点击了(可选修改的)通知,则会像上面的本地通知一样对其进行处理。

没有 UNNotificationServiceExtension ,通知被视为上面的普通远程通知。

附加说明:

使用 mutable-content 时,您必须包含 alert有效载荷中的信息,否则系统会将其视为不可变的并且不会调用您的UNNotificationServiceExtension .您修改后的通知仍必须包含 alert info,否则将使用原始通知有效负载。遗憾的是,没有办法阻止通知出现在用户面前。

使用 content-available 时,如果用户在上次使用时强行退出应用,系统不会重新启动应用或调用AppDelegate.application(_:didReceiveRemoteNotification:fetchCompletionHandler:) .尽管它仍会显示任何警报、播放声音并按照有效负载中的指示更新角标(Badge)。

关于未调用 iOS 10 UNUserNotificationCenterDelegate。推送通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39826905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com