2016-01-23 172 views
2

Facebook用戶應用程序在用戶與推送通知警報消息交互後自動關閉UIViewController。 我想知道他們如何檢測用戶點擊了警報視圖中的某個操作。UIAlert上的用戶交互

我試圖實現基於推送通知委託方法具體的解決方案:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) 
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) 

不幸的是這是不是真的可靠,特別是對於那些已經註冊的推送通知的設備。

這是一個YouTube視頻與Facebook的實現工作流程

https://www.youtube.com/embed/sCg3vYx9hlw

UPDATE:

的問題是,當用戶預先已刪除的應用程序,並已經註冊了這個方法被調用推送通知。在這種情況下,isRegisteredForRemoteNotifications是false,所以我要求registerForRemoteNotifications。顯示警報時 - 即使用戶未在警報對話框中單擊任何選項,也會調用應用程序:didRegisterForRemoteNotificationsWithDeviceToken: 。

回答

0

對於iOS8上,後來註冊的通知會是這樣的:

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil]; 
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 

而且在AppDelegate中didRegisterUserNotificationSettings方法調用時用戶與推送通知警報消息交互,在這裏您註冊通知。在這種代表方法中,您可以檢查是否允許應用程序接收推送通知以及使用何種通知類型:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { 
    // here you can check notification settings 
    // and do some ui changes 

    [application registerForRemoteNotifications]; 
} 
+0

我已更新答案並提供了更多詳細信息。即使用戶未在警報對話框中單擊任何選項,該方法有時也會被調用。 – Giuseppe

+0

你在談論'registerForRemoteNotifications'和'isRegisteredForRemoteNotifications'和'application:didRegisterForRemoteNotificationsWithDeviceToken:'。我正在討論'registerUserNotificationSettings'。請確保你沒有犯錯。 – shpasta