【问题标题】:How to enable/disable push notification from the app iOS 8如何从应用程序 iOS 8 启用/禁用推送通知
【发布时间】:2014-11-22 20:53:13
【问题描述】:

我有一个 iOS 应用程序,我需要从我的应用程序的设置页面启用/禁用推送通知;我使用以下代码启用推送通知

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge |  UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

此代码禁用推送通知

[[UIApplication sharedApplication] unregisterForRemoteNotifications];

但它不适用于 iOS 8 我在调试时收到以下消息

enabledRemoteNotificationTypes is not supported in iOS 8.0 and later

谁能给我建议一个解决方案,从应用程序打开/关闭通知中心的应用程序状态?

【问题讨论】:

  • 请提供任何帮助;需要任何人给我指南

标签: objective-c iphone ios8 push-notification apple-push-notifications


【解决方案1】:

注册:

UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                        UIUserNotificationTypeBadge |
                                                        UIUserNotificationTypeSound);

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];

注销:

[[UIApplication sharedApplication] unregisterForRemoteNotifications];

【讨论】:

    【解决方案2】:

    我尝试使用下面的方法,它成功了..

    #ifdef __IPHONE_8_0
      //Right, that is the point
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
    |UIRemoteNotificationTypeSound
    |UIRemoteNotificationTypeAlert) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    #else
        //register to receive notifications
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    #endif
    

    为 iOS 8.0 添加以下方法

    #ifdef __IPHONE_8_0
    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
    {
        //register to receive notifications
        [application registerForRemoteNotifications];
    }
    
    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
    {
        //handle the actions
        if ([identifier isEqualToString:@"declineAction"]){
        }
        else if ([identifier isEqualToString:@"answerAction"]){
        }
    }
    #endif
    

    【讨论】:

      【解决方案3】:

      registerForRemoteNotificationTypes 方法是 deprecated。 Apple 列出了您应该使用的新方法 (registerForRemoteNotifications)。

      很难说您的问题是关于您收到的警告,还是关于它按您预期的方式运行。

      【讨论】:

      • 我的问题是关于从我的应用程序的设置视图中启用/禁用推送通知的正确方法
      猜你喜欢
      • 2012-11-28
      • 2012-05-16
      • 1970-01-01
      • 2013-06-23
      • 2012-10-24
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多