当我尝试登录用户并通过解析启用推送通知时,我的应用程序崩溃 [英] My app crashes when I try to log sign up a user and enable push notifications through parse
问题描述
我正在尝试通过解析启用推送通知.如果已经有用户缓存并登录到应用程序,则解析通知代码将起作用.但是,如果我注销并尝试注册新用户,则该应用程序将崩溃,并且会显示错误消息:"NSInvalidArgumentException",原因:对于PFObject上的键或值,不能使用nil.使用NSNull作为值.'...我相信问题出在应用程序委托内的didRegisterForRemoteNotificationsWithDeviceToken方法.由于没有currentUser登录,因此当应用尝试创建PFInstallation时,所有关联字段都返回nil.我已经在运行PFInstallation代码之前尝试过if语句来检查currentUser,但是该应用程序仍然崩溃.发生注册后,我需要注册通知,但是我无法弄清楚该怎么做,因为在应用程序委托中需要发生didRegisterForRemoteNotificationsWithDeviceToken.任何建议或解决方案表示赞赏.我的代码段如下.谢谢!
I'm trying to enable push notifications through parse. The parse notification code works if there is already a user cached and signed into the app. If I logout and try to signup a new user, however, the app crashes and I get an error that states: 'NSInvalidArgumentException', reason: 'Can't use nil for keys or values on PFObject. Use NSNull for values.'...I believe the issue is with the didRegisterForRemoteNotificationsWithDeviceToken method within the app delegate. Because there isn't a currentUser logged in, when the app tries creating a PFInstallation, all of the associating fields are returning nil. I have tried an if statement checking for a currentUser before running the PFInstallation code, but the app still crashes. I need to register for notifications after signup occurs, but I can't figure out how to do that seeing as didRegisterForRemoteNotificationsWithDeviceToken needs to occur in the app delegate. Any advice or solutions are appreciate. My code snippet is below. Thanks!
此代码现在可用!
- (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];
}
推荐答案
您仍然可以在没有当前用户的情况下注册安装,但是必须确保将该用户从注册中删除-
You can still register an installation without a current user, but you have to make sure that the user is removed from the registration -
- (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];
}
然后,无论您在哪里处理登录/注销事件,都可以更新当前安装记录.例如-
Then, wherever you handle login/logout events you can update the current installation record. For example -
-(void) loggedIn
{
PFInstallation *currentInstallation=[PFInstallation currentInstallation];
currentInstallation[@"currentUser"]=[PFUser currentUser];
[currentInstallation saveInBackground];
}
-(void) notLoggedIn
{
PFInstallation *currentInstallation=[PFInstallation currentInstallation];
[currentInstallation removeObjectForKey:@"currentUser"];
[currentInstallation saveInBackground];
}
这篇关于当我尝试登录用户并通过解析启用推送通知时,我的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!