簡體   English   中英

為什么appdelegate中的函數沒有被調用?

[英]Why is the function in the appdelegate not being called?

我試圖將當前用戶添加到安裝類,但由於某種原因,該函數未被調用。 我究竟做錯了什么? 我該如何解決? 下面是finishlaunchingwithoptions和didregisterfornotifications。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.



    Parse.enableLocalDatastore()

    // Initialize Parse.
    Parse.setApplicationId("***********************",
        clientKey: "****************************")




    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    UIApplication.sharedApplication().registerForRemoteNotifications()

    if let launchOptions = launchOptions as? [String : AnyObject] {
        if let notificationDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
            self.application(application, didReceiveRemoteNotification: notificationDictionary)
        }
    }

    return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    // Store the deviceToken in the current Installation and save it to Parse
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation["user"] = PFUser.currentUser()
    installation.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
        if (error == nil){
            print("saved installation")
        }else{
            print(error)
        }
    })


}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!) {
    print("Couldn't register: \(error)")
}

這種情況的問題是你在iOS模擬器中運行,它不支持遠程通知。 因此,您的didRegisterForRemoteNotificationsWithDeviceToken永遠不會被調用,因為您在模擬器中。 試試設備,我懷疑它會第一次工作。

對於發現此問題並遇到類似問題的未來讀者,診斷問題的最佳方法是將其添加到您的應用代理:

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Failed to register for remote notifications: \(error.localizedDescription)");
}

我建議使用Installation類上的雲代碼beforeSave觸發器來完成此操作。 它比嘗試處理客戶端更簡單,更強大。 以下是完成工作所需的一切:

// Make sure all installations point to the current user
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {

    Parse.Cloud.useMasterKey();
    if (request.user) {
        request.object.set('user', request.user);
    } else {
        request.object.unset('user');
    }
    response.success();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM