【发布时间】:2015-09-21 13:33:01
【问题描述】:
我正在一个 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)
}
【问题讨论】:
标签: ios swift parse-platform push-notification