gpt4 book ai didi

代码更新后启用 iOS 8 的设备未收到推送通知

转载 作者:IT王子 更新时间:2023-10-29 07:47:04 24 4
gpt4 key购买 nike

我最近将我的一部测试 iphone 升级到 iOS 8,然后升级了 PUSH 注册码如下(使用 xCode 6)

-(BOOL)hasNotificationsEnabled {

NSString *iOSversion = [[UIDevice currentDevice] systemVersion];
NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject];
float versionVal = [prefix floatValue];


if (versionVal >= 8)
{

NSLog(@"%@", [[UIApplication sharedApplication] currentUserNotificationSettings]);
//The output of this log shows that the app is registered for PUSH so should receive them

if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {

return YES;

}

}
else
{
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types != UIRemoteNotificationTypeNone){
return YES;
}

}

return NO;
}

-(void)registerForPUSHNotifications {

NSString *iOSversion = [[UIDevice currentDevice] systemVersion];
NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject];
float versionVal = [prefix floatValue];


if (versionVal >= 8)
{


//for iOS8
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];


}
else
{

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];


}
}

尽管进行了这次升级,并且 [[UIApplication sharedApplication] currentUserNotificationSettings] 显示设备已启用推送,但我没有收到推送通知。

我正在使用 Parse 并按照书上的规定做所有事情 (https://parse.com/tutorials/ios-push-notifications)。

有人遇到同样的问题吗?还有什么我可能遗漏的吗?

最佳答案

注册推送通知的方式在 iOS 8 中已经改变:以下是 iOS 9 之前所有版本的代码:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}

如果您想检查是否启用了推送通知,请使用以下代码:

- (BOOL) pushNotificationOnOrOff
{
if ([UIApplication instancesRespondToSelector:@selector(isRegisteredForRemoteNotifications)]) {
return ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]);
} else {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (types & UIRemoteNotificationTypeAlert);
}
}

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:@"declineAction"]){
}
else if ([identifier isEqualToString:@"answerAction"]){
}
}
#endif

以上代码只能在 Xcode 6+ 上运行...

关于代码更新后启用 iOS 8 的设备未收到推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25909568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com