2017-01-11 57 views
1

嗨,我是新的iOS應用程序開發。我正在使用Swift 3.0代碼。我們可以通過檢查isRegisteredForLocalNotifications更新ViewRegister從didRegisterForRemoteNotificationsWithDeviceToken

在我的應用程序中,我正在獲取設備令牌,並傳遞給我的服務器。將令牌值從Appdelegate傳遞給我使用UserDefaults.standard的UIViewController。它工作正常。在第一次應用程序加載期間,我顯示了系統的推送通知提示。如果用戶單擊確定(允許)或不允許。應用程序使用以下代碼檢查它。

//Checking Notification State 
    let isRegisteredForLocalNotifications = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert) ?? false 

    if isRegisteredForLocalNotifications{ 
     print("Notification allow") 
    }else{ 
     print("Notification Don't allow") 
    } 

注意:上述代碼在didRegisterForRemoteNotificationsWithDeviceToken中正常工作。

我對上面的代碼有以下問題。

1)如果我在ViewController中使用相同的代碼,它的工作,但我的應用程序不等待系統的推送通知提示結果。我在AppDelegate中獲得結果。

因此如何從didRegisterForRemoteNotificationsWithDeviceToken重新加載ViewController?有沒有辦法做到這一點。

2)我可以暫停viewController,直到用戶響應系統的推送通知提示。但我不確定上面的代碼會在ViewController中工作與否。

請指教。 感謝您的幫助。

回答

1

對於你的問題的第一部分,你可以創建一個觀察報:

NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil) 

是處理它會是這樣的功能:

func methodOfReceivedNotification(notification: Notification){ 
    //Take Action on Notification 
} 

,然後你可以如下調用它:

NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil) 

你可以調用上面的代碼,只要你想更新你的viewController

1

您可以從didRegisterForRemoteNotificationsWithDeviceToken更新視圖控制器嗎?通過郵寄通知。

let notificationIdentifier: String = "NotificationIdentifier" 

//write this to didRegisterForRemoteNotificationsWithDeviceToken 
// Post notification 
    NotificationCenter.default.post(name: notificationName, object: nil) 
//write this to your View Controller 
let notificationIdentifier: String = "NotificationIdentifier" 

// Register to receive notification 
NotificationCenter.default.addObserver(self, selector:#selector(YourClassName.methodOfReceivedNotification), name: notificationName, object: nil) 



// Stop listening notification 
NotificationCenter.default.removeObserver(self, name: notificationName, object: nil); 


func methodOfReceivedNotification(notification: Notification) { 
} 
相關問題