【问题标题】:How to check that notifications are enabled on iPhone如何检查 iPhone 上是否启用了通知
【发布时间】:2012-01-25 16:06:14
【问题描述】:

如何检查 iPhone 上是否启用了通知? 我安装了应用程序,应用程序说确认为应用程序启用推送通知,我单击确定。但如果在 iPhone 上禁用通知,则此操作不会启用通知。 如何检查?

【问题讨论】:

    标签: iphone xcode push-notification apple-push-notifications


    【解决方案1】:

    这应该有效:

    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types == UIRemoteNotificationTypeNone) 
       // Disabled
    

    【讨论】:

      【解决方案2】:

      这在 iOS8 中发生了变化。支持iOS8及更低版本

      + (BOOL)notificationServicesEnabled {
          BOOL isEnabled = NO;
      
          if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){
              UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
      
              if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
                  isEnabled = NO;
              } else {
                  isEnabled = YES;
              }
          } else {
              UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
              if (types & UIRemoteNotificationTypeAlert) {
                  isEnabled = YES;
              } else{
                  isEnabled = NO;
              }
          }
      
          return isEnabled;
      }
      

      【讨论】:

      • 你是把这个放在AppDelegate还是ViewController
      • 你可以把这个 sn-p 放在任何地方,因为它从单例对象中获取数据。个人来说,我把代码放在了一个DeviceNotificationsManagerClass里面,里面封装了手机通知子系统相关的逻辑。这样外部世界(即应用程序的其余部分)只引用DeviceNotificationsManager,而不必详细了解 iOS 的通知系统。
      • 嘿@ShaheenGhiassy,上面的sn-p有效。是否有任何特定原因反转 if & else 部分的逻辑。在 if 部分我们检查 UIUserNotificationTypeNone 而在 else 我们检查它的 UIRemoteNotificationTypeAlert。
      猜你喜欢
      • 1970-01-01
      • 2016-12-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      相关资源
      最近更新 更多