unregisterForRemoteNotifications doesn't work

2019-05-22 19:51发布

问题:

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