【问题标题】:Push notification in NotificationCenter return nil in userinfo - Swift3NotificationCenter 中的推送通知在 userinfo 中返回 nil - Swift3
【发布时间】: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)");
}

【问题讨论】:

标签: ios push-notification swift3


【解决方案1】:

发布通知时传递用户信息 像这样:-

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多