启用iOS 8的设备在代码更新后未接收推送通知
我最近将一部测试版iPhone升级到iOS 8,然后升级了推送注册码 如下所示(使用xCode 6) 尽管进行了此升级,而且[[UIApplication sharedApplication]currentUserNotificationSettings]显示设备已启用推送功能,但我没有收到推送通知 我正在使用Parse,并按照书中的内容来做一切事情()启用iOS 8的设备在代码更新后未接收推送通知,ios,iphone,notifications,parse-platform,push,Ios,Iphone,Notifications,Parse Platform,Push,我最近将一部测试版iPhone升级到iOS 8,然后升级了推送注册码 如下所示(使用xCode 6) 尽管进行了此升级,而且[[UIApplication sharedApplication]currentUserNotificationSettings]显示设备已启用推送功能,但我没有收到推送通知 我正在使用Parse,并按照书中的内容来做一切事情() 有人遇到过同样的问题吗?还有什么我可能遗漏的吗?在iOS 8中,注册推送通知的方式已更改: 以下是iOS 9之前所有版本的代码: if ([[
有人遇到过同样的问题吗?还有什么我可能遗漏的吗?在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+上运行…将这一行添加到
.m
文件的顶部
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
1) 在IOS8
中注册推送通知时,必须设置条件。
将此代码添加到应用程序did finish launch
中
if(IS_IOS_8_OR_LATER) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
2) 然后为IOS8
#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(5.0或更早版本)中运行
The code below resolved:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIUserNotificationType types;
types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
if (types & UIUserNotificationTypeAlert)
pushEnabled=YES;
else
pushEnabled=NO;
}
else
{
UIRemoteNotificationType types;
types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert)
pushEnabled=YES;
else
pushEnabled=NO;
}
//iOS 8.0中API的更改
-(无效)注册登记通知
{
NSLog(@“registerForPushNotification”);
如果([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
{
#如果在旧的xcode(=0)中未定义uuu IPHONE\u OS\u版本\u最大允许值>=\uu IPHONE\u 8\u 0/\uu IPHONE\u 8\u 0,则使用80000
NSLog(@“registerForPushNotification:For iOS>=8.0”);
[[UIApplication sharedApplication]注册表通知设置:
[UIUserNotificationSettings设置类型:
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
类别:无]];;
[[UIApplication sharedApplication]注册表项的删除];
#恩迪夫
}否则{
NSLog(@“registerForPushNotification:对于iOS<8.0”);
[[UIApplication sharedApplication]RegisterForRemotionTifications类型:
(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
}
如果您同时拥有iOS 8和旧版本,请尝试以下操作:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// For iOS 8
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
// For iOS < 8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
if([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)])){
//对于iOS 8
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings设置类型:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)类别:nil];
[[UIApplication sharedApplication]注册表项的删除];
}否则{
//对于iOS<8
[[UIApplication sharedApplication]RegisterForRemotionTifications类型:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
从苹果的开发网站:
UIUserNotificationSettings
UIUserNotificationSettings对象封装了
应用程序可以向用户显示的通知。应用程序
将可视或音频警报与本地或推送按钮结合使用
通知必须注册其使用的警报类型。UIKit
将您提供的信息与用户的首选项关联到
确定您的应用程序允许使用的警报类型
使用此类封装初始注册请求并查看请求结果。创建此类的实例并指定首选设置后,调用UIApplication类的registerUserNotificationSettings:
方法来注册这些设置。根据用户首选项检查您的请求后,应用程序将结果发送到其应用程序委托的应用程序:didRegisterUserNotificationSettings:
方法。传递给该方法的对象指定允许应用程序使用的通知类型
除了注册应用程序的警报类型外,您还可以使用此类注册要与本地或推送通知一起显示的自定义操作组。自定义操作表示应用程序可以响应通知执行的即时任务。您可以定义动作组,并将整个组与给定的通知相关联。当显示相应的警报时,系统会为您指定的每个操作添加按钮。当用户点击其中一个操作的按钮时,系统将唤醒您的应用程序,并调用其应用程序委托的应用程序:handleActionWithIdentifier:forRemoteNotification:completionHandler:
或应用程序:handleActionWithIdentifier:forLocalNotification:completionHandler:
方法。使用这些方法执行请求的操作
非常简单: 在
xcode6
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
if([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)])){
//对于iOS 8
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings设置类型:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)类别:nil];
[[UIApplication sharedApplication]注册表项的删除];
}
其他的
{
//对于iOS<8
[[UIApplication sharedApplication]RegisterForRemotionTifications类型:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
在此之后:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// For iOS 8
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
// For iOS < 8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
// For iOS 8
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
// For iOS < 8
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
if settings.types.rawValue & UIUserNotificationType.Alert.rawValue == UIUserNotificationType.Alert.rawValue
{
// can receive alert!
}
else
{
// if the user is not even able to receive alerts, show him a hint on how to reenable notifications in system settings
}
if settings.types.rawValue & UIUserNotificationType.Badge.rawValue == UIUserNotificationType.Badge.rawValue
{
// can receive badge!
}
if settings.types.rawValue & UIUserNotificationType.Sound.rawValue == UIUserNotificationType.Sound.rawValue
{
// can receive sound!
}