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

问题描述

我正在一个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],device token: Data,on topic: String,environment: PushRouter.Environment = .development,Error>) -> Void)?) {

}

我应该使用哪一个?我也想使用FCM推送通知。因此,我需要在参数“设备令牌”上传递FCM令牌,并且该参数需要“数据”类型。我使用Pushtype为.apns的第一种方法,并将设备令牌作为FCM令牌传递(将字符串FCM令牌转换为数据类型),并且得到失败响应并说无效的Push令牌。然后我将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)")
   }
}

解决方法

快速推送代码 对于Swift,请确保您使用pushType

注册.apns2

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

请参阅提及此内容的Pubnub文档,并提供指向FCM文档的链接,其中包含所有详细信息: https://www.pubnub.com/docs/platform/resources/mobile-push-troubleshooting#check-fcm-payload

PubNub文档上还有一些示例代码。这是处理数据和通知类型的回调方法的片段,并对这两种方法都有一些解释:

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 support联系,告知您所有详细信息/错误,并提供指向此SO帖子的链接。

,

选择使用Pushtype作为.gcm的第二个选项,然后在收到令牌后尝试将成功消息作为对象处理回PubNub。否则,您的代码看起来会很好。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...