gpt4 book ai didi

ios - NotificationCenter 中的推送通知在用户信息中返回 nil - Swift3

转载 作者:行者123 更新时间:2023-11-29 00:39:31 25 4
gpt4 key购买 nike

我已将源代码从 Swift 2.2 更新到 Swift 3.0 以用于推送通知。但我无法获取用户信息。它将返回零。谁能帮忙?

在应用程序委托(delegate)中接收推送通知:

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)");
}

最佳答案

发布通知时传递 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)
}

关于ios - NotificationCenter 中的推送通知在用户信息中返回 nil - Swift3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39849617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com