2016-10-04 4 views
0

У меня было обновление с исходного кода от Swift 2.2 до Swift 3.0 для push-уведомления. Но я не могу получить userinfo. Он вернет нуль. Может ли кто-нибудь помочь?Push-уведомление в NotificationCenter return nil в userinfo - Swift3

Здесь в приложении делегат получил толчок уведомление:

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

см. Этот раз http://stackoverflow.com/q uestions/39382852/didreceiveremotenotification-not-called-ios-10/39383027 # 39383027 –

+0

Вы отправляете информацию о пользователе nil в NotificationCenter.default.post (имя: notificationName, object: nil) – AleyRobotics

+0

, если вы хотите отправить информацию о пользователе. Сообщение должно быть например, NotificationCenter.default.post (name: notificationName, object: SOME_USER_INFO_OBJ) – AleyRobotics

ответ

1

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