【问题标题】:can app decide to show alert based on user dictionary when receiving a push notification?应用程序可以决定在收到推送通知时根据用户字典显示警报吗?
【发布时间】:2011-09-16 03:36:58
【问题描述】:

当您收到推送通知时,是否有可能让您的应用程序决定是否显示指示您收到推送通知的 AlertView?

或者操作系统只是自动控制这个而不让你对此有任何控制?

我想收到一些通知给我的应用程序,然后让我的应用程序根据通过didReceiveRemoteNotification 传入的字典中的一些数据来决定是否显示警报或某些用户可见通知?这有可能以任何方式吗?

【问题讨论】:

    标签: ios apple-push-notifications


    【解决方案1】:

    您无法控制UIAlertView 的外观关于通过您的应用程序推送。但可能是服务器可以:尝试发送没有 alert 信息的推送通知。

    请告诉我,它是如何工作的。 TY

    【讨论】:

      【解决方案2】:

      距离提出这个问题已有 10 年,还有 17 天。现在,您可以,因为 iOS 10.0`。 ?

      我刚刚给how to get the userInfo in other delegate methods写了一个答案。在您的具体情况下,您希望实现userNotificationCenter(_:willPresent:withCompletionHandler:)

      使用notification.request.content.userInfo 在该委托方法中获取userInfo 字典。如果您在传递给 APNs 的 data 有效负载中设置了诸如 foregroundNotification: true 之类的键/值,您的设备可以在此处看到。

      你所要做的就是:

      let userInfo = notification.request.content.userInfo
      if let foregroundNotification = userInfo["foregroundNotification"] as? Bool, foregroundNotification == true {
          completionHandler([.banner])
      }
      

      更高级的处理(声音,支持 iOS 10+,包括弃用)

      请记住,alert 在 iOS 14 中已被弃用,您可能需要添加 iOS 版本检查、声音、列表。

      func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
          var options: UNNotificationPresentationOptions = []
          let userInfo = notification.request.content.userInfo
          
          if let foregroundSound = userInfo["foregroundSound"] as? Bool, foregroundSound == true {
              options.insert(.sound)
          }
          
          if let foregroundBadge = userInfo["foregroundBadge"] as? Bool, foregroundBadge == true {
              options.insert(.badge)
          }
          
          if let foregroundList = userInfo["foregroundList"] as? Bool, foregroundList == true {
              if #available(iOS 14.0, *) {
                  options.insert(.list)
              }
          }
          
          if let foregroundNotification = userInfo["foregroundNotification"] as? Bool, foregroundNotification == true {
              if #available(iOS 14.0, *) {
                  options.insert(.banner)
              } else {
                  options.insert(.alert)
              }
          }
          
          completionHandler(options)
      }
      

      【讨论】:

        猜你喜欢
        • 2011-12-19
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-12
        • 2012-07-28
        • 2015-09-15
        • 1970-01-01
        相关资源
        最近更新 更多