2016-01-01 52 views
0

因此,我推送了通知設置,並將其作爲推送通知的一部分,我需要將當前用戶添加到安裝表中。這工作得很好了,直到沒有登錄用戶。解析註冊推送通知 - 當用戶未登錄時崩潰

這是我的代碼

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    let types:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] 
    let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil) 

    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 

    return true 
} 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    let installation = PFInstallation.currentInstallation() 
    installation.setObject(PFUser.currentUser()!, forKey: "user") 
    installation.setDeviceTokenFromData(deviceToken) 
    installation.saveInBackground() 
} 

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 
    if error.code == 3010 { 
     print("Push notifications are not supported in the iOS Simulator.") 
    } else { 
     print("application:didFailToRegisterForRemoteNotificationsWithError: %@", error) 
    } 
} 

我需要註冊到安裝表中的用戶沒有它崩潰如果在沒有用戶的一種方式,我會做一個簡單的檢查,看看是否有用戶登錄,然後在有用戶的情況下運行代碼,但如果有人向他們發送了通知,他們不會得到它,因爲他們的PFUser.currentUser()尚未添加。在此先感謝

回答