如何从应用程序 iOS 8 启用/禁用推送通知

Posted

技术标签:

【中文标题】如何从应用程序 iOS 8 启用/禁用推送通知【英文标题】:How to enable/disable push notification from the app 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

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

【问题讨论】:

请提供任何帮助;需要任何人给我指南 【参考方案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)。

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

【讨论】:

我的问题是关于从我的应用程序的设置视图中启用/禁用推送通知的正确方法

以上是关于如何从应用程序 iOS 8 启用/禁用推送通知的主要内容,如果未能解决你的问题,请参考以下文章

如何部分启用/禁用远程推送通知客户端?

实现细粒度启用/禁用推送通知

如何启用和禁用解析推送通知 Android

如何正确禁用/启用推送通知

iOS:如果之前启用,则禁用远程通知

如何将未读消息的iOS推送通知分组为一个