为什么userNotificationCenter didReceive没有被触发? [英] Why userNotificationCenter didReceive is not fired?

查看:573
本文介绍了为什么userNotificationCenter didReceive没有被触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码的一部分.我想使用UNUserNotificationCenterUNUserNotificationCenterDelegate处理通知事件.

This is a part of my code. I want to use UNUserNotificationCenter and UNUserNotificationCenterDelegate to handle notification events.

当应用程序处于前台状态时,此代码捕获通知事件.但是"didReceive"不会针对背景状态触发.

This code catches the notification event when the app is in a foreground state. But "didReceive" is not fired for a background state.

func application(_ application: UIApplication,
                         didFinishLaunchingWithOptions launchOptions:
            [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
            UNUserNotificationCenter.current().delegate = self
    application.registerForRemoteNotifications()
    return true
    }    
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification,
                                    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
        {
            print("willPresent") /// This works
            completionHandler([.alert, .badge, .sound])
        }
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {
            print("didReceive") /// This doesn't work
            completionHandler()
        }

但是,如果我不使用UNUserNotificationCenter.current().delegate = self,则在后台正确触发了委托方法.

But if I don't use UNUserNotificationCenter.current().delegate = self the delegate method is correctly fired in the background.

func application(_ application: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping FetchCompletionHandler) {
        print("didReceiveRemoteNotification... \(userInfo)")
}

如何使用"didReceive"?我想在后台处理通知.

How can I use "didReceive"? I want to handle the notification in the background.

推荐答案

application(_:didReceiveRemoteNotification:fetchCompletionHandler:).当应用程序处于后台状态时,可以传递静默的推送通知,iOS唤醒应用程序以执行用户不可见的后台处理.

application(_:didReceiveRemoteNotification:fetchCompletionHandler:) is called when the application receives a silent push notification. Silent push notifications can be delivered in when the application is in the background state, iOS wakes the application to perform background processing invisible to the user.

无声推送通知的content-available标志设置为1.

A silent push notification has the content-available flag set to 1.

静音推送通知中不应包含警报,徽章或声音.静默推送并不意味着用户可见,只是向应用程序暗示新的远程内容可用.

Silent push notifications should not include an alert, badge, or sound. Silent push is not meant to be visible to the user, it is only a hint to the application that new remote content is available.

从推送通知有效负载中删除content-available标志将使iOS将其作为常规通知进行处理.将调用用户通知中心委托方法,而不是application(_:didReceiveRemoteNotification:fetchCompletionHandler:),但是您的应用程序将无法执行由通知触发的后台处理.

Removing the content-available flag from your push notification payload will cause iOS to handle it as a regular notification. The user notification center delegate methods will be called instead of application(_:didReceiveRemoteNotification:fetchCompletionHandler:) but your application will be unable to do background processing triggered by the notification.

您可以使用此工具

这篇关于为什么userNotificationCenter didReceive没有被触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆