当我尝试login注册用户并通过parsing启用推送通知时,我的应用崩溃

我试图通过parsing启用推送通知。 parsing通知代码在已经有用户caching并login到应用程序的情况下工作。 如果我注销并尝试注册一个新的用户,但是,应用程序崩溃,我得到一个错误,指出:'NSInvalidArgumentException',原因:'不能使用零对PFObject上的键或值。 使用NSNull作为值。'…我相信问题是应用程序委托中的didRegisterForRemoteNotificationsWithDeviceToken方法。 由于没有当前用户login,当应用程序尝试创buildPFInstallation时,所有关联字段都返回nil。 在运行PFInstallation代码之前,我尝试了一个if语句检查currentUser,但是应用程序仍然崩溃。 我需要注册发生后注册通知,但我不知道该怎么做,因为RegistryForRemoteNotificationsWithDeviceToken需要发生在应用程序委托。 任何build议或解决scheme都很感激。 我的代码片段如下。 谢谢!

编辑:此代码现在可以工作!

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken { PFInstallation *currentInstallation = [PFInstallation currentInstallation]; if ([PFUser currentUser] != nil) { currentInstallation[@"currentUser"]=[PFUser currentUser]; } else { [currentInstallation removeObjectForKey:@"currentUser"]; } [currentInstallation setDeviceTokenFromData:newDeviceToken]; [currentInstallation saveInBackground]; 

}

用于发送推送通知

 - (IBAction)send:(id)sender { PFQuery *userQuery = [PFUser query]; [userQuery whereKey:@"objectId" equalTo:self.recipient.objectId]; PFQuery *query = [PFInstallation query]; [query whereKey:@"user" matchesQuery:userQuery]; NSString *sendingUser = self.currentUser.username; NSString *message = [NSString stringWithFormat:@"from %@: \n %@", sendingUser,self.message.text]; PFPush *push= [[PFPush alloc]init]; [push setQuery:query]; [push setMessage:message]; [push sendPushInBackground]; NSLog(@"Message sent!"); [self dismissViewControllerAnimated:NO completion:nil]; 

}

您仍然可以在没有当前用户的情况下注册安装,但是您必须确保将用户从注册中删除 –

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Store the deviceToken in the current installation and save it to Parse. PFInstallation *currentInstallation = [PFInstallation currentInstallation]; if ([PFUser currentUser] != nil) { currentInstallation[@"currentUser"]=[PFUser currentUser]; } else { [currentInstallation removeObjectForKey:@"currentUser"]; } [currentInstallation setDeviceTokenFromData:deviceToken]; [currentInstallation saveInBackground]; } 

然后,无论您处理login/注销事件,都可以更新当前的安装logging。 例如 –

 -(void) loggedIn { PFInstallation *currentInstallation=[PFInstallation currentInstallation]; currentInstallation[@"currentUser"]=[PFUser currentUser]; [currentInstallation saveInBackground]; } -(void) notLoggedIn { PFInstallation *currentInstallation=[PFInstallation currentInstallation]; [currentInstallation removeObjectForKey:@"currentUser"]; [currentInstallation saveInBackground]; }