2015-05-04 173 views
0

我有一個應用程序,我使用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 –

回答

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

沒什麼不同 –

+0

請參閱respondsToSelector行.... – Rahul

+0

哦,很好地嘗試過,代碼沒有工作,我認爲該行是爲任何東西比操作系統8更大,這裏它已經在工作8 –

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]; 
    } 
相關問題