我正在尝试将当前用户添加到安装类,但由于某种原因,该函数未被调用。我究竟做错了什么?我该如何解决?下面是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)")
}
答案 0 :(得分:5)
这种情况下的问题是你在iOS模拟器中运行,它不支持远程通知。因此,您的didRegisterForRemoteNotificationsWithDeviceToken
永远不会被调用,因为您在模拟器中。试试设备,我怀疑它会第一次运行。
对于发现此问题并遇到类似问题的未来读者,诊断问题的最佳方法是将其添加到您的应用代理中:
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to register for remote notifications: \(error.localizedDescription)");
}
答案 1 :(得分:2)
我建议使用Installation
课程中的cloud code beforeSave trigger来完成此操作。它比尝试处理客户端更简单,更强大。以下是完成工作所需的一切:
// Make sure all installations point to the current user
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {
Parse.Cloud.useMasterKey();
if (request.user) {
request.object.set('user', request.user);
} else {
request.object.unset('user');
}
response.success();
});