iOS 8使设备在代码更新后不接收PUSH通知

时间:2014-09-18 09:56:20

标签: ios iphone notifications parse-platform push

我最近将我的一部测试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]显示PUSH已为设备启用,但我没有收到PUSH通知。

我正在使用Parse并尽可能地完成本书的所有内容(https://parse.com/tutorials/ios-push-notifications)。

有没有人遇到同样的问题?还有其他我可能会遗失的东西吗?

8 个答案:

答案 0 :(得分:67)

在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+上运行......

答案 1 :(得分:12)

.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

然后将调用通知委托方法。希望这会对你有所帮助!!!

答案 2 :(得分:4)

运行时和旧的编译器安全选项...如果您在旧的xcode(5.0或更早版本)中运行

    // changes of API in iOS 8.0
- (void) registerForPushNotification
{
    NSLog(@"registerForPushNotification");
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 //__IPHONE_8_0 is not defined in old xcode (==0). Then use 80000

        NSLog(@"registerForPushNotification: For iOS >= 8.0");

        [[UIApplication sharedApplication] registerUserNotificationSettings:
            [UIUserNotificationSettings settingsForTypes:
                (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                              categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
    } else {
        NSLog(@"registerForPushNotification: For iOS < 8.0");
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    }
}

答案 3 :(得分:4)

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)];
}

之后:

  1. 删除设备中不再有推送通知的应用。
  2. 完全关闭设备并重新打开。
  3. 转到设置并提前一天设置日期和时间
  4. 再次关闭设备然后将其关闭
  5. 再次安装应用程序,它会在第一次安装时通知您。
  6. 如果未自动设置,请重新设置日期/时间。
  7. 这将重置通知设置。重新安装应用程序,一切都会正常工作:)

答案 4 :(得分:1)

如果您同时拥有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)];
}

来自Apple的开发网站:

  

<强> UIUserNotificationSettings

     

UIUserNotificationSettings对象封装了类型   您的应用可以向用户显示的通知。应用程序   将可见或可听警报与本地或推送结合使用   通知必须注册他们使用的警报类型。 UIKit的   将您提供的信息与用户的偏好相关联   确定允许您的应用使用哪种类型的提醒。

     

使用此类封装初始注册请求并查看请求结果。创建此类的实例并指定首选设置后,请调用UIApplication类的registerUserNotificationSettings:方法以注册这些设置。根据用户首选项检查您的请求后,应用会将结果传递给其应用代理的application:didRegisterUserNotificationSettings:方法。传递给该方法的对象指定允许您的应用使用的通知类型。

     

除了注册应用程序的警报类型外,您还可以使用此类注册自定义操作组以与本地或推送通知一起显示。自定义操作表示应用可以响应通知执行的即时任务。您可以定义操作组并将整个组与给定通知相关联。显示相应的警报时,系统会为您指定的每个操作添加按钮。当用户点击其中一个操作的按钮时,系统会唤醒您的应用并调用其应用代理的application:handleActionWithIdentifier:forRemoteNotification:completionHandler:application:handleActionWithIdentifier:forLocalNotification:completionHandler:方法。使用这些方法执行请求的操作。

答案 5 :(得分:1)

非常简单:

didFinishLaunchingWithOptions

中的项目的xcode6中添加以下行
 [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
 [[UIApplication sharedApplication] registerForRemoteNotifications];

答案 6 :(得分: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;

}

答案 7 :(得分:0)

我试图获取设置对象的类型信息,发现types属性基本上是一个位掩码。以下是如何提取信息。

我的例子是在Swift和&gt; = iOS 8.0中。

    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!
    }