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

问题描述:

我有一个应用程序,我使用APNS发送推送通知。我曾尝试使用下面的代码推送通知不适用于iOS 7,但适用于iOS8

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设备上它甚至做了询问通知启用,但它似乎在通知设置配置应用程序的通知。出了什么问题?

+0

您是否收到过两个ios版本的设备令牌? – Surjeet

+0

@Surjeet是我收到的令牌为两个和在iOS 8它的工作原理,尝试在iPad 2以及iPhone 4 –

+0

您可以检查以下方法中的任何错误? - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err –

试试这个....

#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 
+0

没什么不同 –

+0

请参阅respondsToSelector行.... – Rahul

+0

哦,很好地尝试过,代码没有工作,我认为该行是为任何东西比操作系统8更大,这里它已经在工作8 –

去为以下之一,此代码是最近测试和推送通知工作完美。

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