繁体   English   中英

如何在 iOS Swift 中使用 Firebase FCM 令牌在 PubNub 上注册推送通知

[英]How to register push notification on PubNub with Firebase FCM token in iOS Swift

我正在开发一个 iOS Swift 项目,在该项目中我使用 PubNub 信令服务器完成了 WebRTC 视频通话和聊天。 我还实现了 Firebase 推送通知。 这个推送通知工作正常。 但是当我接到视频电话或聊天时,后端团队会通过 Pubnub 发送通知。 因此,我检查了 Pubnub 文档以在 Pubnub 中使用 FCM 令牌注册设备。 但是我找到了两种注册方法,第一种是

public func modifyPushChannelRegistrations(
    byRemoving removals: [String],
    thenAdding additions: [String],
    for deviceToken: Data,
    of pushType: PushRouter.PushType = .apns,
    with networkConfiguration: NetworkConfiguration? = nil,
    respondOn queue: DispatchQueue = .main,
    completion: ((Result<ModifiedPushChannelsPayloadResponse, Error>) -> Void)?) {
         
 }

第二个是

public func modifyAPNSDevicesOnChannels(
    byRemoving removals: [String],
    thenAdding additions: [String],
    device token: Data,
    on topic: String,
    environment: PushRouter.Environment = .development,
    with networkConfiguration: NetworkConfiguration? = nil,
    respondOn queue: DispatchQueue = .main,
    completion: ((Result<ModifiedPushChannelsPayloadResponse, Error>) -> Void)?) {

}

我应该使用哪一种? 我也想使用 FCM 推送通知。 因此,我需要在参数“设备令牌”上传递 FCM 令牌,该令牌需要“数据”类型。 我使用 Pushtype 作为 .apns 的第一种方法,并将设备令牌作为 FCM 令牌传递(将字符串 FCM 令牌转换为数据类型),我收到失败响应并显示无效的推送令牌。 然后我将 Pushtype 更改为 .gcm,然后我得到了成功响应,但是当我在 Pubnub 调试控制台上进行测试时,我没有在此方法中收到任何回调。

func userNotificationCenter(_ center: UNUserNotificationCenter,
                  willPresent notification: UNNotification,
                  withCompletionHandler completionHandler:
                  @escaping (UNNotificationPresentationOptions) -> Void) {

}

我不知道我是否在尝试正确的方法。 如果你们中的任何人使用 FCM 令牌在 Pubnub 上注册推送通知,请帮助我。

这是我的代码,

var deviceToken = String()
var dataToken = Data()
if let notificationToken = UserDefaults.standard.value(forKey: "FCMToken") as? String {  
   deviceToken = notificationToken
   dataToken = Data(deviceToken.utf8) //Converting String device token to Data
}

var channelName = String()
self.pubnub.modifyPushChannelRegistrations(byRemoving: [], thenAdding: [callChannel, chatChannel], for: dataToken, of: .gcm) {
   result in
   switch result {
      case let .success(response):
         print("Successful Push Modification Response: \(response)")

      case let .failure(error):
         print("Failed Push List Response: \(error.localizedDescription)")
   }
}

斯威夫特推代码对于斯威夫特,确保您注册pushType使用.apns2

Android 推送代码对于 FCM 推送通知,您可以发送/接收两种类型:数据通知 通知类型将在后台自动显示在您的设备上。 需要明确处理和显示数据类型。

请参阅提到这一点的 Pubnub 文档,并且有一个指向 FCM 文档的链接,其中包含所有详细信息: https ://www.pubnub.com/docs/platform/resources/mobile-push-troubleshooting#check-fcm-payload

PubNub 文档上还有一些示例代码。 这是处理 Data 和 Notification 类型的回调方法的片段,并对两者进行了一些解释:

public void onMessageReceived(RemoteMessage remoteMessage) {
/*
STEP 5: receive push notifications from FCM
*/
  /*
    There are two types of messages data messages and notification messages.
    Data messages are handled here in onMessageReceived whether the app is in
    the foreground or background. Data messages are the type traditionally
    used with GCM. Notification messages are only received here in
    onMessageReceived when the app is in the foreground. When the app is in
    the background an automatically generated notification is displayed.
    When the user taps on the notification they're returned to the app. Messages
    containing both notification and data payloads are treated as notification
    messages. The Firebase console always sends notification messages.
    For more refer to: https://firebase.google.com/docs/cloud-messaging/concept-options
    */
}

此代码片段来自包含所有其他步骤的 PubNub 文档。 https://www.pubnub.com/docs/platform/messages/push/send#android-devices

希望能帮助您解决问题。 如果没有,请联系PubNub 支持并提供您的所有详细信息/错误,并包含指向此 SO 帖子的链接。

坚持使用 Pushtype 作为 .gcm 的第二个选项,然后一旦您收到您的令牌,尝试将成功消息作为对象处理回 PubNub。 否则你的代码看起来不错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM