[英]Push notification in NotificationCenter return nil in userinfo - Swift3
I had update then source code from Swift 2.2 to Swift 3.0 for push notification. 我已经将源代码从Swift 2.2更新到了Swift 3.0,以进行推送通知。 But I can not get the userinfo. 但是我无法获得userinfo。 It will return nil. 它将返回nil。 Can anyone help? 有人可以帮忙吗?
Here in the app delegate to received the push notification: 在此应用程序委托中,可以接收到推送通知:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// Define identifier
let notificationName = Notification.Name("PushNotificationMessageReceivedNotification")
// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(MenuViewController.remoteNotificationReceived), name: notificationName, object: userInfo)
// Post notification
NotificationCenter.default.post(name: notificationName, object: nil)
}
In other viewController I want to received the push notification and do some action: 在其他viewController中,我想接收推送通知并执行一些操作:
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(MenuViewController.remoteNotificationReceived(_:)), name: NSNotification.Name(rawValue: "PushNotificationMessageReceivedNotification"), object: nil)
}
func remoteNotificationReceived(_ notification: Notification)
{
print("Notification:\(notification.userinfo)");
}
Pass UserInfo When Post Notification Like This :- 像这样发布通知时传递UserInfo:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// Define identifier
let notificationName = Notification.Name("PushNotificationMessageReceivedNotification")
// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(MenuViewController.remoteNotificationReceived), name: notificationName, object: userInfo)
// Post notification
NotificationCenter.default.post(name: notificationName, object: nil, userInfo :userInfo)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.