2015-09-07 55 views
3

對於iOS> = 8,僅的iOS 8+遠程通知能力始終啓用

以我的AppDelegate,我註冊的用戶通知如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSLog(@"didFinishLaunchingWithOptions called"); 

    // iOS >= 8.0 

    // register to receive user notifications 
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 

    ... 
} 

在完成後,我註冊遠程通知作爲如下:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 
{ 
    NSLog(@"didRegisterUserNotificationSettings called"); 

    //register to receive remote notifications 
    [application registerForRemoteNotifications]; 
} 

,完成之後,我請檢查應用程序是否已註冊

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 

    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken called"); 

    BOOL pushNotificationOnOrOff = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; // always returns TRUE 
} 

但應用始終表示推送通知被啓用,即使用戶已明確設置應用程序的遠程通知功能關閉(通過通知權限警告該應用程序的第一次安裝後出現,或通過應用程序設置。)

我已將應用程序的背景模式/遠程通知設置爲TRUE。我一直在使用開發證書編譯的實際設備上進行調試(通過USB電纜進行連接)。

幫助,我一直在爲此奮鬥了幾個小時。

回答

5

這似乎是一個錯誤,我也發現iPhone 6,iOS 8.1.2上的相同行爲。

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]總是返回TRUE即使用戶拒絕推送通知權限(通過警報視圖)或通過Settings.app > Notifications手動禁用通知。

經過一番研究,我發現如果你有Background App Refresh爲應用程序啓用,那麼[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]將永遠返回TRUE

Background App Refresh設置爲FALSE然後[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]返回正確的值。

作爲一種變通方法,您可以評估[[UIApplication sharedApplication] currentUserNotificationSettings].types確定推送通知是否允許。

typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) { 
    UIUserNotificationTypeNone = 0,  // the application may not present any UI upon a notification being received 
    UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received 
    UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received 
    UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being received 
} NS_ENUM_AVAILABLE_IOS(8_0); 

希望這會有所幫助。

+0

只是要評論快速,我觀察到一個應用程序我現在工作的這個相同的行爲,在iPhone 5S 9.2.1運行。當背景刷新關閉時,該值按預期返回true或false。當背景刷新打開時,此值僅返回true。 –