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

时间:2015-09-21 13:33:02

标签: ios swift parse-platform push-notification

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

我实施了以下方法:

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()
    }

我在app启动时成功进入此方法,但我的对象ID始终为nil,这是第一次安装应用程序。

如果用户取消应用并重新启动,则objectid不再为零,我可以使用每个用户的正确标识符更新我的后端,但是,我的问题是将应用程序置于后台的人员甚至不知道如何杀掉一个应用程序。

我该如何解决这个问题?我可以在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)
}

1 个答案:

答案 0 :(得分:1)

我不确定为什么安装对象的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!)")
    }
}