【发布时间】:2015-12-17 18:46:19
【问题描述】:
我正在尝试将当前用户添加到安装类,但由于某种原因该函数没有被调用。我究竟做错了什么?我该如何解决?下面是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)")
}
【问题讨论】:
-
您注册远程通知了吗? :)
-
我该怎么做?在我的应用下的设置中,通知已启用
-
你需要打电话给
UIApplication.sharedApplication().registerForRemoteNotifications(),也可以打电话给UIApplication.sharedApplication().registerUserNotificationSettings()。 -
@TwoStraws 我更新了我的代码以包含
didFinishLaunchingWithOptions -
谢谢。为免生疑问,您是否也请将
didFailToRegisterForRemoteNotificationsWithError添加到您的应用程序委托中? iOS 可能会向您发送一条您没有看到的错误消息。
标签: ios swift parse-platform