【问题标题】:Is there a way to prevent users from receiving push notifications after they have been sent from Firebase?有没有办法阻止用户在从 Firebase 发送推送通知后收到推送通知?
【发布时间】:2017-06-08 21:17:50
【问题描述】:

我想知道我是否可以通过 Firebase 向用户发送多个推送通知,然后在用户在设备上看到它们之前拦截它们并允许我的应用选择性地允许通知。

这可能吗?

如果是这样,我需要使用哪种方法? (来自react-native-firebaseiOS

【问题讨论】:

  • 另一种选择是在 Firebase 中创建一个新的“受众”,而不是在推送通知发送后选择性地阻止它们。然后,当您想将this_push_notification 发送给特定的一组用户时,您只能将其发送给具有receive_this_push_notification = true 的用户组。这样,您只会将通知传递给应该首先收到通知的用户(过滤发生在 Firebase 端)。可以根据用户在应用中经历的特定自定义事件以及人口统计选项的数量来创建受众。
  • 不幸的是,我们需要的可能的受众组合数量有数万个

标签: ios firebase push-notification apple-push-notifications firebase-cloud-messaging


【解决方案1】:

您可以尝试使用UNNotificationServiceExtension(可从iOS 10 获得)。您需要为您的项目添加一个扩展 (example how to add)。

在您的扩展中实现方法- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler; 之后。

它将在通知显示给用户之前被调用,您可以尝试执行一些操作(例如:将一些数据保存到本地)。

注意它只适用于iOS 10 rich notifications (how to implement it with Firebase)。

附:希望对你有帮助!

编辑

Apple documentation写:

覆盖此方法并使用它来修改 UNNotificationContent 与通知一起传递的对象。

更新

您可以尝试从服务器发送静默推送通知,检查它并创建本地通知以在需要时显示给用户。

【讨论】:

  • 你知道这样可以忽略通知吗?
  • @MichaelCampsall,我编辑了我的答案,只是寻找它。
  • "UNNotificationContent:本地或远程通知的不可编辑内容。"来自 Apple 文档。此外,在文档中:“您也可以修改警报文本,只要您不删除它。如果内容对象不包含任何警报文本,系统将忽略您的修改并传递原始通知内容。”
  • 您可以从服务器发送静默推送通知,检查它并创建本地通知以在需要时显示给用户。
【解决方案2】:

当应用程序处于前台状态时,可以处理 FireBase 通知。首先,您可以在控制器和 info.plist 中从 Always 更改通知何时使用。 设置后,您可以在 App 委托或 viewController 中控制通知。

【讨论】:

    【解决方案3】:

    新的

    您可以向用户发送静默通知并将您的实际通知安排为本地通知

    参考This

    (在 Firebase 的情况下) 只有在应用程序处于前台时才可管理通知。 如果应用处于后台,则无法处理,因为用户将收到通知,并且在点击通知之前不会通知应用。

    【讨论】:

    • 我不建议这样做 - 很多事情都会阻止静默通知的传递。例如,低电量模式、弱电池信号等。文档解释说,只有当 iOS 认为这样做是个好主意时才会处理这些通知,例如,在插入电源时,或者在强 wifi 或蜂窝信号时。
    【解决方案4】:

    使用"content-available" : 1 发送静默远程通知,并决定是否要将其显示给用户。

    {
        "aps" = {
            "content-available" : 1,
            "sound" : ""
        };
        // add custom key value for message
    
    }
    

    如果您想向用户显示它,请获取消息的自定义键值对并触发本地通知以显示用户。它也将在后台工作。 UIBackgroundMode 需要启用远程通知。

    【讨论】:

      【解决方案5】:

      您可以发送将在前台和后台接收的数据通知。在 iOS 中,它们由委托方法 application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 处理,处理后您可以决定是否为用户创建推送。

      @update

      用于在低于 10 的 iOS 上接收数据消息(不推送)

      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
            handleNotification(data: userInfo)
            completionHandler(UIBackgroundFetchResult.newData)
      }
      

      在 iOS10 上

      func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
            handleNotification(data: remoteMessage.appData)
      }
      

      然后我只检查自定义字段是否有我需要的数据并使用 NotificationCenter 创建本地通知

      fileprivate func handleNotification(data: [AnyHashable : Any]) {
            guard let topic = data["topic"] as? String else { return }
            if data["receiver"] as? String == UserManager.shared.current(Profile.self)?.uuid && topic  == "coupons" {
              displayNotification(data: data)
            }
      }
      
      fileprivate func displayNotification(data: [AnyHashable : Any]) {
              if #available(iOS 10.0, *) {        
                  let content = UNMutableNotificationContent()
                  content.title = data["notification_title"] as! String
                  content.body = data["notification_body"] as! String
      
                  let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1, repeats: false)
                  let request = UNNotificationRequest.init(identifier: data["topic"] as! String, content: content, trigger: trigger)
      
                  UNUserNotificationCenter.current().add(request)
              } else {
                  if currentNotification != nil {
                      UIApplication.shared.cancelLocalNotification(currentNotification!)
                  }
      
                  let notification = UILocalNotification()
                  notification.fireDate = Date()
                  notification.category = data["topic"] as? String
                  notification.alertBody = data["notification_body"] as? String
                  if #available(iOS 8.2, *) {
                      notification.alertTitle = data["notification_title"] as? String
                  }
                  currentNotification = notification
                  UIApplication.shared.scheduleLocalNotification(currentNotification!)
              }
      }
      

      请记住,这适用于数据通知而不是推送通知,但在 Firebase 中您可以发送这两种类型。

      【讨论】:

      • 你能告诉我一些决定是否推送给用户的代码吗?
      • 我编辑了我的答案,仅使用内置的通知中心显示并没有什么花哨的,但如果需要一些解释,请随时询问
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多