【问题标题】:Objective-C - Detect when user change the app's notifications settingsObjective-C - 检测用户何时更改应用程序的通知设置
【发布时间】:2012-04-28 18:42:18
【问题描述】:

我需要始终知道用户在推送通知设置中选择了哪些选项。
(选项有 - 警报、声音和徽章)

所以当我的应用启动时,我会调用:

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

并检测用户选择了什么。

但是如何检测用户是否在应用生命周期的后期更改了设置?
当此设置发生更改时,是否有一些委托方法会被调用?

【问题讨论】:

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


    【解决方案1】:

    在您的应用激活时检查它,而不仅仅是在启动时检查。

    【讨论】:

      【解决方案2】:

      没有代表。您需要定期查询UIApplication 属性enabledRemoteNotificationTypes,例如applicationDidBecomeActive:

      有关详细信息,请查看以下答案:

      Determine on iPhone if user has enabled push notifications

      View In Lock Screen and enabledRemoteNotificationTypes - iOS5

      编辑:
      如果您需要重置推送通知设置和权限警报,请查看Apple technical note TN2265。在 “重置 iOS 上的推送通知权限警报”部分中,他们解释了如何在 iOS 上重置设置。但是,许多开发人员抱怨该程序不起作用。不确定this link 是否有效,您需要访问 Apple 论坛,但这是有关此问题的主题之一。

      我自己想知道 Apple 是否已经删除了 iOS 5.1 中的权限对话框。否则他们为什么会要求应用程序显示警报?根据AppStore review guidelines until June 2016

      5.3 未经用户同意就发送推送通知的应用将被拒绝

      例如,Path(应用程序)要求用户在注册过程中选择加入推送通知,而不是在应用程序第一次启动时。

      不确定提示的目的是什么,因为应用程序无法查询通知设置的状态。特别是,应用程序可以检查启用了哪些通知类型(使用enabledRemoteNotificationTypes),但不能检查是否启用或禁用了特定应用程序的推送通知(顶部的通知中心开/关开关)。至少这是 iOS 5.1 中的行为。即使用户禁用了该应用程序的通知,该应用程序仍然可以注册推送通知(使用registerForRemoteNotificationTypes)并会收到一个 APNS 令牌。

      【讨论】:

      • 谢谢:) 你知道我应该怎么做才能让用户在第一次启动应用程序时得到“允许推送此应用程序”弹出框吗?我需要它来调试不同的场景,我试图删除应用程序并重新安装但没有弹出框...
      • 我编辑了答案以包含一些可能有用的链接。然而,许多开发人员抱怨 Apple 概述的程序不起作用。我在开发我的应用程序时从未见过权限对话框,所以直到阅读其他开发人员的一些帖子时我才知道它的存在。
      • 最后一部分不正确,至少在 iOS 7 上,enabledRemoteNotificationTypes 仅适用于您的应用,如果用户将通知设置为无,您将不会收到 APNs 令牌。
      • enabledRemoteNotificationTypes 总是给我 UIRemoteNotificationTypeNone。有什么原因吗?
      • 如果您在模拟器中看到,那是正常的。推送通知在模拟器中不起作用,因此您不会获得令牌
      【解决方案3】:

      这是一个通过 UrbanAirship 实现 Push 的示例。每次当用户选择加入/选择退出推送以下委托时,使用下面的方法,您可以检查 (YES/NO)。

      如果不使用 UrbanAirship,也可以使用 UIApplication 委托来实现。

      - (void)registrationSucceededForChannelID:(NSString )channelID deviceToken:(NSString )deviceToken
          {
              NSLog(@"registrationSucceededForChannelID : %@",[self appRegisterForPushNotification]?@"YES":@"NO");
          }
      
      
          - (BOOL)appRegisterForPushNotification {
              if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
                  UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
                  return ((types & UIUserNotificationTypeAlert) || (types & UIUserNotificationTypeSound));
              }
              return NO;
          }
      

      【讨论】:

        【解决方案4】:

        除了答案@Nick Bull 将您的敏感 ViewController 订阅到 UIApplicationWillEnterForegroundNotification 当用户完成设置工作并返回您的应用时收听:

        [[NSNotificationCenter defaultCenter] addObserver: self
                                                 selector: @selector(appEnterForeground:)
                                                     name: UIApplicationWillEnterForegroundNotification object:nil];
        

        Swift 变体在这里:How to check that user is back from Settings

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-04
          • 2018-11-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多