当我尝试登录注册用户并通过解析启用推送通知时,我的应用程序崩溃 [英] My app crashes when I try to log sign up a user and enable push notifications through parse

查看:17
本文介绍了当我尝试登录注册用户并通过解析启用推送通知时,我的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过解析启用推送通知.如果已经有用户缓存并登录到应用程序,则解析通知代码有效.但是,如果我注销并尝试注册一个新用户,应用程序会崩溃并且我收到一条错误消息:NSInvalidArgumentException",原因:不能将 nil 用于 PFObject 上的键或值.使用 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 %@: 
 %@", 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆