推送通知不适用于iOS 7,但适用于iOS8

时间:2015-05-04 10:58:03

标签: ios iphone push-notification

我有一个应用程序,我使用APNS发送推送通知。我已使用以下代码在应用中配置了通知

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

我发现ID通知正在使用iOS 8及更高版本的设备,但在iOS7设备上,它甚至要求启用通知,但它会显示在通知设置下。出了什么问题?

2 个答案:

答案 0 :(得分:0)

试试这个......

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000

    if ([[UIApplication sharedApplication]    respondsToSelector:@selector(registerUserNotificationSettings:)]) {

        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];

    } else {

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

#else

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

#endif

答案 1 :(得分:0)

转到下面的代码,最近对此代码进行了测试,推送通知工作正常。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

} //Register for notifications..
#ifdef __IPHONE_8_0
    if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                             |UIRemoteNotificationTypeSound
                                                                                             |UIRemoteNotificationTypeAlert) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
#endif
    {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }