简体   繁体   English

Swift & Push Notification:application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 没有被调用

[英]Swift & Push Notification : application(_:didReceiveRemoteNotification:fetchCompletionHandler:) does not get called

I'm configuring push notification is swift.我正在快速配置推送通知。 But I noticed that the method below doesn't get called for some reason.但我注意到下面的方法由于某种原因没有被调用。 Could you tell me why?你能告诉我为什么吗? Are there any necessary setups I should have done?有没有我应该做的必要设置? Thank you谢谢

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print("Received data message: \(userInfo)")
        
        let name = Notification.Name(rawValue: K.Event.pushRequest)
        
        completionHandler(.newData)
    }

need post需要发帖

NotificationCenter.default.post(name:object:) NotificationCenter.default.post(name:object:)

//AppDelegate
func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable:Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print("Received data message: \(userInfo)")

    let name = Notification.Name(rawValue: K.Event.pushRequest)
    NotificationCenter.default.post(name: name, object: nil) //or your object

    completionHandler(.newData)
}

also, you need to addObserver in UIViewController另外,您需要在 UIViewController 中添加观察者

addObserver(_:selector:name:object:) addObserver(_:selector:name:object:)

//your viewController
override func viewDidLoad() {
    super.viewDidLoad()
    
    let name = Notification.Name(rawValue: K.Event.pushRequest)
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(handleNotification),
                                           name: name, 
                                           object: nil) //or your object
}

@objc func handleNotification(_ notification: Notification) {
    print("notification")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 application:didReceiveRemoteNotification:fetchCompletionHandler Not Called - application:didReceiveRemoteNotification:fetchCompletionHandler Not Called 检测是否通过点击Notification Center中的通知调用application:didReceiveRemoteNotification:fetchCompletionHandler: - Detect if application: didReceiveRemoteNotification: fetchCompletionHandler: was called by tapping on a notification in Notification Center didReceiveRemoteNotification fetchCompletionHandler没有在后台调用以进行静默推送 - didReceiveRemoteNotification fetchCompletionHandler is not getting called in background for silent push 带有“ content-available”的推送通知:1个有效负载并不总是调用application:didReceiveRemoteNotification:fetchCompletionHandler: - Push notification with “content-available” : 1 payload not always calling application:didReceiveRemoteNotification:fetchCompletionHandler: didReceiveRemoteNotification:fetchCompletionHandler:从图标vs推送通知打开 - didReceiveRemoteNotification: fetchCompletionHandler: open from icon vs push notification iOS推送通知-didReceiveRemoteNotification:fetchCompletionHandler:从不在前台调用 - iOS push notifications - didReceiveRemoteNotification:fetchCompletionHandler: never called in foreground application:didReceiveRemoteNotification:fetchCompletionHandler:多次调用。如何避免? - application: didReceiveRemoteNotification: fetchCompletionHandler: called more than once.How to avoid? 每次推送通知都不会调用didReceiveRemoteNotification - didReceiveRemoteNotification is not called for every push notification applicationState在application:didReceiveRemoteNotification:fetchCompletionHandler中为UIApplicationStateInactive: - applicationState is UIApplicationStateInactive in application:didReceiveRemoteNotification:fetchCompletionHandler: 为什么didReceiveRemoteNotification没有被调用但是didReceiveRemoteNotification:当我的应用程序在前台时调用fetchCompletionHandler? - Why is didReceiveRemoteNotification not called but didReceiveRemoteNotification:fetchCompletionHandler called when my app is in the foreground?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM