对象id nil第一次每次快速解析



我在一个快速项目中实现解析。我使用设备的目标作为发送通知的唯一标识符。我获取这个值并将其存储在每个用户的数据库中。

我实现了以下方法:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        println("get in here every time?")
        let installation = PFInstallation.currentInstallation()
        installation.setDeviceTokenFromData(deviceToken)
        installation.saveInBackground()
        if installation.objectId != nil {
            installation.setObject(installation.objectId!, forKey: "userName")
            updateNotificationIDWebServiceCall(installation.objectId!)
        }
        else{
            println("object id was nil. Device token was: (deviceToken).")
            //JLToast.makeText("object id was nil. Device token was: (deviceToken).").show()
        }
        installation.saveInBackground()
    }

我成功地进入这个方法在应用程序启动,但我的对象id总是nil,第一次安装应用程序。

如果用户关闭应用程序并重新启动,objectid不再是nil,我可以用每个用户的正确标识符更新后端,但是,我的问题是那些将应用程序放在后台甚至不知道如何关闭应用程序的人。

我怎样才能绕过这个?我能把一些代码在didBecomeActive吗?我似乎找不到saveInBackground方法的回调。

还有我的didFinishLaunchingWithOptions:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        Prefs.notificationToShow = true
        // Override point for customization after application launch.
        //UIApplication.sharedApplication().applicationIconBadgeNumber = 0
        if (PFInstallation.currentInstallation().badge != 0) {
            PFInstallation.currentInstallation().badge = 0
            PFInstallation.currentInstallation().saveInBackground()
        }
        Parse.setApplicationId("xxx",
            clientKey: "yyy")
        // Register for Push Notitications
        if application.applicationState != UIApplicationState.Background {
            // Track an app open here if we launch with a push, unless
            // "content_available" was used to trigger a background push (introduced in iOS 7).
            // In that case, we skip tracking here to avoid double counting the app-open.
            let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
            let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
            var pushPayload = false
            if let options = launchOptions {
                pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
            }
            if (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
                PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
            }
        }
        if application.respondsToSelector("registerUserNotificationSettings:") {
            let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
            let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
        } else {
            let types = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
            application.registerForRemoteNotifications()
        }
        NSThread.sleepForTimeInterval(1);
        UIApplication.sharedApplication().statusBarHidden = true
        self.window?.makeKeyAndVisible()
        Fabric.with([Twitter(), Crashlytics()])
        return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

我不确定为什么安装对象的objectId是nil,但是我有一个您可能感兴趣的替代方法。

考虑在每个安装中保存指向当前用户的指针,而不是将安装的objectId保存给用户作为唯一标识符。每个设备都有自己的安装对象,一个用户可能有多个设备。

你需要有一种方式来发送推送通知到他们所有的设备,但在你目前的设计下,每个用户仅限于一个设备。

通过向每个安装对象添加用户指针,您将能够查询和创建用于发送通知的用户集。

添加一些调试代码以确保Parse在没有错误的情况下保存安装也是值得的。将您的didRegisterForRemoteNotificationsWithDeviceToken更改为以下内容:

installation.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
    if (success) {
        println("success - didRegisterForRemoteNotificationsWithDeviceToken")
    } else {
        //  Log details of the failure
        println("failure - didRegisterForRemoteNotificationsWithDeviceToken")
        println("Error: (error!) (error!.userInfo!)")
    }
} 

相关内容

最新更新