ios - 在 iOS 中集成 FCM 以进行推送通知时,是否使用了 Device Token?

标签 ios firebase swift3 push-notification firebase-cloud-messaging

我有以下情况

使用 APNS

用于接收我的原生 iOS 应用程序的远程通知。使用它时,我们需要创建 .p12 证书,并且我们需要在注册推送通知时发送在 Appdelegate.m 文件中生成的设备 token 。因此,我们采用了将设备 ID 发送到后端的方法,将通知发送到该特定设备。

使用 FCM 时

我通过了 FCM 并且还知道我们需要将 .p12 文件上传到他们的控制台。到此为止一切都很好。但是当谈到设备 token 部分时,我不清楚“设备 token ”的“Swizzling”过程。 firebase 会生成设备 token 还是我们需要设置在 didRegisterforRemoteNotification 中生成的设备 token ?

    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 文档 here , 它告诉我们:

The registration token may change when:

  1. The app is restored on a new device

  2. The user uninstalls/reinstall the app

  3. The user clears app data.


而对于“Swizzling”。这个概念是,如果它被禁用,那么你必须重写方法 didRegisterForRemoteNotificationsWithDeviceToken检索 APNs token ,然后调用 setAPNSToken .因为你已经在这样做了。
Here文档中关于 的内容启用/禁用方法混合
如下:

Method swizzling available with FCM simplifies your client code. However, for developers who prefer not to use it, FCM allows you to disable method swizzling by adding the FirebaseAppDelegateProxyEnabledflag in the app’s Info.plist file and setting its value to NO (boolean value).

FCM swizzling affects how you handle the default registration token, and how you handle downstream message callbacks. Where applicable, this guide provides migration examples both with and without method swizzling enabled.


希望你清楚!

关于ios - 在 iOS 中集成 FCM 以进行推送通知时,是否使用了 Device Token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44260575/

相关文章:

ios - Swift 2.1 中的嵌套闭包

ios - "project.app"需要-Onone Swift 优化级别来使用预览

ios:将新密码添加到 iCloud 钥匙串(keychain)

firebase - 从firebase中的文件导入环境变量

android - 防止 firestore 中出现重复条目

swift - 在 UIView.Animate 中执行 2 CGAffineTransform - Swift

swift - 如何正确地将 WKWebView 嵌入自定义 UIView 中?

ios - 'AppDelegate' 不符合协议(protocol) 'GIDSignInDelegate'

ios - 如何停止 UIViewAnimationOptionRepeat UIView 动画?

firebase - 错误 "Try replacing the reference to the instance member with a different "