推送通知“ willPresent”未调用,但“ didReceive”有效

时间:2020-06-15 21:25:54

标签: swift push-notification apple-push-notifications sendbird

我正在开发一个处理推送通知的应用程序。

出于某种原因,

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

可以正常工作,但是

func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

从不被调用。

有人遇到过这种情况吗?

所有常规犯罪嫌疑人都在位:

UNUserNotificationCenter.current().delegate = self

设置为didFinishLaunchingWithOptions

let center  = UNUserNotificationCenter.current()
        UNUserNotificationCenter.current().delegate = self
        let options: UNAuthorizationOptions = [.alert, .badge, .sound]
        center.requestAuthorization(options:options) { (granted, error) in
            if error == nil{
                DispatchQueue.main.async() {
                    UIApplication.shared.registerForRemoteNotifications()
                    completion(granted)
                }
            }
        }

返回true(已授予权限)

其他值得一提的项目:

  • 推送通知通过SendBird发送

  • 当应用程序在后台运行时,一切正常。

1 个答案:

答案 0 :(得分:0)

一次又一次地阅读文档。

Apple声明func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

向委托人询问如何处理在前台运行应用程序时到达的通知。

也就是说,只有当您的应用程序在FORGROUND中运行并且您收到通知时,系统才会调用此方法以询问您是否仍应向用户显示此通知,因为它们已经在您的应用程序内。您可以调用completionHandler并传递诸如alertsound之类的选项,以便用户仍然可以看到横幅并听到声音。如果您只传回sound,则用户看不到横幅,只听到通知声音。