嗨,我有一个应用程序,我已经设置了推送通知并且它们正在工作。我的最后一步是,当用户打开应用程序并询问他们是否要允许推送通知时,我想将该用户的指针设置为与该手机关联的安装 ID。我可以用这个来做到这一点
PFInstallation *currentInstalation = [PFInstallation currentInstallation];
NSString *installationObjectId = currentInstalation.objectId;
[currentUser setObject:installationObjectId forKey:@"installationString"];
[currentUser setObject:currentInstalation forKey:@"installation"];
这是我的用户类的一部分图片以澄清
但是我不想每次用户打开应用程序时都保存,如果尚未设置,我只想保存一次。所以我打算用这个 if 语句来检查它是否已经设置好了
if (![currentUser[@"installationString"] isEqual:installationObjectId]) {
//save it here
}
但是如果用户点击“不允许推送通知”就会出现问题,因为没有设置安装对象,所以该电话/用户的安装对象为空,上面的 if 语句给出了下面的错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Can't use nil for keys or values on PFObject. Use NSNull for values.'
应用程序失败。是否有另一种/更好的方法来检查指针是否已设置,这样如果用户点击“不允许”然后重新打开应用程序,它就不会退出。
提前感谢您的帮助,我真的很感激!!!
编辑
应用程序委托代码
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];
}
我之所以这样设置,是因为我有一个云功能,可以向用户发送推送,并从用户那里获得指向安装 ID 的指针来发送推送,也许我可以考虑翻转它?
如果用户点击不允许。Parse.com 不会保存该设备的安装 ID 吗?
谢谢