简体   繁体   English

unregisterForRemoteNotifications不适用于iOS8-推送通知

[英]unregisterForRemoteNotifications doesn't work for iOS8 - Push Notification

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. 我在我的应用程序中添加了这些行,以使其支持iOS 7和iOS 8,并且在关闭推送通知时它可以正常工作。 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. 但是当我决定打开开关并关闭应用程序时,当我再次打开它并又回到OFF而不是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. 因此,我必须转到“设置”->“通知中心”->“我的应用程序”,然后将所有内容重新打开,因为它们已关闭...很奇怪,当我在iOS 7上对其进行测试时,它可以工作,但不适用于iOS 8.任何建议表示赞赏。 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];

           }
   }
}

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. 另一方面,iOS8提供了API来打开Settings.app,用户可以在其中使用开关控件为您的应用禁用通知。 Usage sample: 用法样本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM