【发布时间】:2016-10-04 10:10:58
【问题描述】:
我已将源代码从 Swift 2.2 更新到 Swift 3.0 以进行推送通知。但我无法获取用户信息。它将返回零。 有人可以帮忙吗?
在应用程序委托中接收推送通知:
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)
}
在其他 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)");
}
【问题讨论】:
-
您在 NotificationCenter.default.post(name: notificationName, object: nil) 发送 nil 用户信息
-
如果要发送用户信息 Post 必须是这样 NotificationCenter.default.post(name: notificationName, object: SOME_USER_INFO_OBJ)
标签: ios push-notification swift3