user831098
user831098

Reputation: 1823

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. But I can not get the userinfo. It will return 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:

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

Upvotes: 0

Views: 647

Answers (1)

SBK
SBK

Reputation: 101

Pass UserInfo When Post Notification Like This :-

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

Upvotes: 1

Related Questions