object id nil 第一次每次 swift parse
object id nil first time every time swift parse
我正在 swift 项目中实现解析。我使用设备的 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()
}
我在应用程序启动时成功进入此方法,但我的对象 ID 始终为零,这是第一次安装该应用程序。
如果用户关闭应用程序并重新启动,则 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)
}
我不确定为什么安装对象的 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!)")
}
}
我正在 swift 项目中实现解析。我使用设备的 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()
}
我在应用程序启动时成功进入此方法,但我的对象 ID 始终为零,这是第一次安装该应用程序。
如果用户关闭应用程序并重新启动,则 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)
}
我不确定为什么安装对象的 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!)")
}
}