unregisterForRemoteNotifications doesn't work for iOS8 - Push Notification

老子叫甜甜 提交于 2019-12-07 19:54:45

问题


I decided to turn off my notification on "reminders" switch control from my app. I added those lines in my app to make it support both iOS 7 and iOS 8 and it is working when I switched off push notification. BUT when I decided to turn on the switch and closed the app,when I opened it again and it went back to OFF instead of being ON. So I have to go to Settings --> Notification Center --> "my app" and turn all the things back on because they are off... Very strange that when I test it on iOS 7 it is working but not for iOS 8. Any suggestion appreciated. Thanks.

- (IBAction)reminderSwitchToggled:(id)sender {

      UIApplication *application = [UIApplication sharedApplication];

      if ([sender isOn]) {

         #ifdef __IPHONE_8_0
             if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
                 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
                 [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

             }
              else
         #endif
                 {
                   [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeSound];
                 }

     } else {

         #ifdef __IPHONE_8_0
           if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {

               [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                             settingsForTypes:
                                                                             (UIUserNotificationType)
                                                                             (UIUserNotificationTypeNone) 
                                                                             categories:nil]];

               [application unregisterForRemoteNotifications];

        }

          else
      #endif
          {
               [application unregisterForRemoteNotifications];

           }
   }
}

回答1:


You should implement disabling push notifications on the server side. Apple docs said that you should call

[[UIApplication sharedApplication] unregisterForRemoteNotifications];

only if you will not provide notifications in app any more. Link

In other hand, iOS8 provides API to open Settings.app, where user can disable notifications for your app using switch control. Usage sample:

if (&UIApplicationOpenSettingsURLString != NULL)
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];   


来源:https://stackoverflow.com/questions/25457655/unregisterforremotenotifications-doesnt-work-for-ios8-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!