티스토리 뷰

Swift 2.2 용으로 편집 :

메서드 FirstViewController에서의 새 인스턴스를 만들고 application: didReceiveRemoteNotification있습니다. 그래서 당신은 FirstViewController당신이 의미 하는 것과 다른 인스턴스의 속성을 업데이트하고 있습니다 . 업데이트중인 항목이 표시되지 않습니다. updateText()호출되고 있지만 스토리 보드가 초기화되지 않았기 때문에 오류가 발생 하여 notificationLabel이 여전히 nil입니다.

하려는 작업을 수행하려면 대신 알림 게시물을 사용하여 뷰 컨트롤러에 알림을 전달하고 FirstViewController관찰자로 추가했습니다.

당신에 AppDelegate, 다음에 몸을 변경 :

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
             fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
             // If you are receiving a notification message while your app is in the background,
             // this callback will not be fired till the user taps on the notification launching the application.
             // TODO: Handle data of notification
             
             // Print message ID.
             
             print("Message ID: \(userInfo["gcm.message_id"]!)")
             NotificationCenter.default.post(name: "Add_Notification_Post_Name_Here", object: nil,
                                                                           userInfo: userInfo)
                                                                           

}

에서 FirstViewController알림 게시물을 수신 할 관찰자를 추가합니다.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: Some_Method_Name:, name: "Add_Notification_Post_Name_Here", object: nil)
        }
        

에서 FirstViewController,이 통지 게시물을 처리하는 방법을 추가 :

func Some_Method_Name(_ notification: NSNotification) {

guard let userInfo = notification.userInfo else {
        return
            }
                guard let payload = userInfo["Some_Message_Key_Here"] as? NSString  else {
                        return
                            }
                                notificationText = String(payload)
                                    updateText()
                                    }
                                    

이 메서드는 패키지를 String 유형의 페이로드로 변환하여 알림 텍스트에 할당합니다. 그런 다음 updateText () 함수를 호출합니다.



출처
https://stackoverflow.com/questions/39940093
댓글
공지사항
Total
Today
Yesterday
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30