[英]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)
}
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.