【问题标题】:How may I trigger banner notifications using FCM?如何使用 FCM 触发横幅通知?
【发布时间】:2017-04-07 05:31:08
【问题描述】:

如何使用 Firebase 的 FCM 功能在我的应用中显示横幅?我能够通过苹果提供的委托方法接收和显示横幅通知:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  completionHandler([.alert, .badge, .sound])
}

但是,一旦我尝试使用 FCM,就不再调用此方法。相反,接收通知信息的是这个方法:

extension FirebaseNotificationManager: FIRMessagingDelegate {
  func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
    let appData = remoteMessage.appData
    appData.forEach { print($0) }
  }
}

我可以获得appData 的打印输出,但我不确定如何实际显示横幅警报。

感谢任何帮助!

【问题讨论】:

    标签: ios swift firebase firebase-cloud-messaging


    【解决方案1】:

    我在 iOS 10 上使用它并为我工作。

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler 
     {
            NSDictionary *userInfo = notification.request.content.userInfo;
    
            // Print full message.
             NSLog(@"%@", userInfo);
    
    // Change this to your preferred presentation option
          completionHandler(UNNotificationPresentationOptionAlert);
    
    }
    

    【讨论】:

    • 同时使用 iOS 10。出于某种原因,在我尝试使用 FCM 之后,该方法永远不会被调用。
    【解决方案2】:

    经过一天的研究和实验,我得出结论,对于这种情况,不需要连接到 Firebase FCM。

    如果您连接到 FCM,则需要依靠 Firebase 的 applicationReceivedRemoteMessage 方法来处理传入的推送通知。我仍然不知道如何通过该方法显示横幅通知。

    但是,主要目标是访问推送通知发送的键值对。无需通过 Firebase 的自定义对象来访问这些对象。

    每个UNNotification 对象都有一个userInfo 字典(尽管它的嵌套程度很高):

    let userInfo = response.notification.request.content.userInfo
    

    这将包含与FIRMessagingRemoteMessage 中相同的信息:

    let appData = remoteMessage.appData
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多