简体   繁体   English

Parse.com-将指针从用户类设置为安装类

[英]Parse.com - Set pointer from user Class to Installation class

Hi I have an app that I have set up with push notifications and they are working. 嗨,我有一个使用推送通知设置的应用程序,它们正在运行。 My last step is that when a user opens the app and is asked if they want to allow push notification, I want to set a pointer from that user to the installation id associated with that phone. 我的最后一步是,当用户打开应用程序并询问他们是否要允许推送通知时,我想设置一个指向该用户的指针,该指针指向与该电话关联的安装ID。 I can do that using this 我可以用这个

 PFInstallation *currentInstalation = [PFInstallation currentInstallation];
 NSString *installationObjectId = currentInstalation.objectId;
[currentUser setObject:installationObjectId forKey:@"installationString"];
[currentUser setObject:currentInstalation forKey:@"installation"];

Here is a pic of part of my user class to clarify 这是我的用户类的图片,以澄清

在此处输入图片说明

But I don't want to make this save every time the user opens the app, I just want to do it the once if it has not been set yet. 但是,我不想每次用户打开应用程序时都保存此文件,我只想执行一次(如果尚未设置)。 So I was going to use this if statement to check if it had been set yet 所以我要使用这个if语句来检查它是否已经设置

if (![currentUser[@"installationString"] isEqual:installationObjectId]) {
//save it here
}

But the problem comes if the user taps, "don't allow push notifications" because then there is no installation object set, so the installation object for that phone/ user is null and the above if statement gives the error below 但是问题出在用户点击“不允许推送通知”,因为没有设置安装对象,因此该电话/用户的安装对象为空,并且上述if语句给出以下错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'Can't use nil for keys or values on PFObject. Use NSNull for values.'

And the app fails. 应用失败。 Is there another/ better way to check if the pointer has been set, so that if the user taps "don't allow" and then reopens the app it won't quit out. 还有另一种/更好的方法来检查指针是否已设置,以便如果用户点击“不允许”然后重新打开该应用程序,它将不会退出。

Thanks for the help in advance I really appreciate it!!! 谢谢您的帮助,我非常感谢!!!

EDIT 编辑

APP DELAGTE CODE 应用程式代号

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                             categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
} else {
    // Register for Push Notifications before iOS 8
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                     UIRemoteNotificationTypeAlert |
                                                     UIRemoteNotificationTypeSound)];
}
- (void)application:(UIApplication *)application 

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [PFPush handlePush:userInfo];
}

And I have it set this way because I have a cloud function that send pushes to user and and get a pointer from the user to the installation id to send the push, maybe I could consider flipping that? 我将其设置为这种方式,因为我有一个云功能,可以将推送发送给用户,并从用户那里获得指向安装ID的指针以发送推送,也许我可以考虑将其翻转?

And if the user clicks don't allow. 并且,如果用户点击,则不允许。 Doesn't Parse.com not save an installation id for that device? Parse.com不会不为该设备保存安装ID吗?

Thanks 谢谢

This should help PFInstallation *currentInstalation = [PFInstallation currentInstallation]; NSString *installationObjectId = currentInstalation.objectId; [currentUser setObject:installationObjectId forKey:@"installationString"]; [currentUser setObject:currentInstalation forKey:@"installation"]; 这应该有助于PFInstallation *currentInstalation = [PFInstallation currentInstallation]; NSString *installationObjectId = currentInstalation.objectId; [currentUser setObject:installationObjectId forKey:@"installationString"]; [currentUser setObject:currentInstalation forKey:@"installation"]; PFInstallation *currentInstalation = [PFInstallation currentInstallation]; NSString *installationObjectId = currentInstalation.objectId; [currentUser setObject:installationObjectId forKey:@"installationString"]; [currentUser setObject:currentInstalation forKey:@"installation"];

SWIFT: 迅速:

   var newUser = PFUser()
   var currentInstallation = PFInstallation.currentInstallation()
   let installationObjectId: NSString = currentInstallation.installationId

        newUser.setObject(installationObjectId, forKey: "installationString")
        newUser.setObject(currentInstallation, forKey: "installation")
        newUser.saveInBackgroundWithBlock { (success, error) -> Void in
            if error == nil {
              print("success")
            } else {

                print(error)
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM