unregisterForRemoteNotifications doesn't work

2019-05-22 19:59发布

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条回答
孤傲高冷的网名
2楼-- · 2019-05-22 20:19

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]];   
查看更多
登录 后发表回答