Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS Swift中使用Firebase FCM令牌在PubNub上注册推送通知_Ios_Swift_Firebase_Pubnub - Fatal编程技术网

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

如何在iOS Swift中使用Firebase FCM令牌在PubNub上注册推送通知,ios,swift,firebase,pubnub,Ios,Swift,Firebase,Pubnub,我正在从事一个iOS Swift项目,在该项目中,我使用PubNub信令服务器进行了WebRTC视频通话和聊天。我还实现了Firebase推送通知。这个推送通知工作正常。但当我接到视频来电或聊天时,后端团队会通过Pubnub发送通知。因此,我检查了Pubnub文档,以便在Pubnub中使用FCM令牌注册设备。但是我发现了两种注册方法,第一种是 public func modifyPushChannelRegistrations( byRemoving removals: [String]

我正在从事一个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)?) {
         
 }
我不知道我是否在尝试正确的方法。如果你们中的任何人使用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 as.gcm的第二个选项,然后在收到令牌后,尝试将成功消息作为对象处理回PubNub。否则,您的代码看起来很好。

快速推送代码 对于Swift,请确保使用
.apns2

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

请参阅提到这一点的Pubnub文档,其中有一个指向FCM文档的链接,其中包含所有详细信息:

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文档。


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

Hi@Craig Conover Ok如果我选择推送类型为.apns,我是否需要在参数设备令牌中传递deviceToken或FCM令牌?因为我尝试了.apns,并且在传递FCM令牌(从字符串转换为数据)时,it响应失败..apns=certs,.apns2=tokens,.gcm=fcmIf如果您仍然存在问题,我建议您联系[PN Support}()有了所有这些详细信息并粘贴链接到此SO。@Hilaj-要直接回答您的问题,请使用
.fcm
.gcm
注册Android设备(如果fcm不是选项)。它们在我们的后端(fcm)上解析为同一类型。
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)")
   }
}
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
    */
}