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:)
//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
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")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.