在集成FCM以在iOS中集成通知时,是否可以使用设备令牌



我有以下方案

使用apns

用于接收我本机iOS应用程序的远程通知。在使用时,我们需要创建.p12证书,并且我们需要在注册通知时在AppDelegate.m文件中发送设备令牌。因此,我们遵循将设备ID发送到后端将通知发送到该特定设备的方法。

使用FCM

我浏览了FCM,还知道我们需要将.p12文件上传到其控制台。一切都很好,直到这个。但是,当关于设备令牌部分时,我不清楚"设备令牌"的"滚动"过程。Firebase是否会生成设备令牌,还是我们需要设置在DiDregisterForremoteNotification中生成的设备令牌?

    import FirebaseMessaging
override func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  FIRApp.configure()
  NSNotificationCenter.defaultCenter().addObserver(self,
                                                   selector: #selector(tokenRefreshNotification(_:)),
                                                   name: kFIRInstanceIDTokenRefreshNotification,
                                                   object: nil)
}
// NOTE: Need to use this when swizzling is disabled
public func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
  FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}
func tokenRefreshNotification(notification: NSNotification) {
  // NOTE: It can be nil here
  let refreshedToken = FIRInstanceID.instanceID().token()
  print("InstanceID token: (refreshedToken)")
  connectToFcm()
}
func connectToFcm() {
  FIRMessaging.messaging().connectWithCompletion { (error) in
    if (error != nil) {
      print("Unable to connect with FCM. (error)")
    } else {
      print("Connected to FCM.")
    }
  }
}
public func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
  print(userInfo)
}

根据这里的firebase文档,它告诉:

当注册令牌可能会更改:

  1. 该应用程序已在新设备上还原

  2. 用户卸载/重新安装应用程序

  3. 用户清除应用数据。

和" swizzling"。这个概念是如果它被禁用,则必须覆盖方法didRegisterForRemoteNotificationsWithDeviceToken才能检索APNS令牌,然后致电setAPNSToken。当您已经这样做时。

这里的文档对启用/禁用方法swizzling 如下:

使用FCM可用的方法简化了您的客户端代码。但是,对于不愿使用它的开发人员,FCM允许您禁用方法通过添加firbaseappdelegateproxyenabledflag在应用程序的info.plist文件和将其值设置为no(布尔值(。

fcm swizzling会影响您处理默认注册令牌的方式,以及如何处理下游消息回调。在适用的情况下,本指南提供有或没有方法的迁移示例启用了启用。

希望您清楚!

相关内容

最新更新