0

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)");
}
user831098
  • 1,803
  • 6
  • 27
  • 47
  • see this once http://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called-ios-10/39383027#39383027 – Anbu.Karthik Oct 04 '16 at 10:12
  • you are sending nil user info at NotificationCenter.default.post(name: notificationName, object: nil) – AleyRobotics Oct 04 '16 at 10:17
  • if you want to send user info Post must be like this NotificationCenter.default.post(name: notificationName, object: SOME_USER_INFO_OBJ) – AleyRobotics Oct 04 '16 at 10:18
  • See my ans. https://stackoverflow.com/questions/39504334/push-notification-not-receiving-in-ios-10/39694689#39694689 – Keyur Hirani Oct 04 '16 at 10:20

1 Answers1

1

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)
    }
SBK
  • 101
  • 5