在iOS中集成FCM进行推送通知时是否使用了设备令牌?

时间:2017-05-30 11:08:06

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

我有以下情景

使用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)
}

1 个答案:

答案 0 :(得分:2)

根据Firebase文档here,它告诉我们:

  

注册令牌可能会在以下情况下发生变化:

     
      
  1. 该应用已在新设备上恢复

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

  4.   
  5. 用户清除应用数据。

  6.   

对于" Swizzling"。概念是,如果它被禁用,那么您必须覆盖方法setKeepAlive以检索APNs令牌,然后调用didRegisterForRemoteNotificationsWithDeviceToken。正如你已经这样做了。

Here文档中有关启用/禁用方法调配的内容 如下:

  

FCM提供的方法调配可简化您的客户端代码。   但是,对于不想使用它的开发人员,FCM允许您   通过添加禁用方法调配   应用程序的Info.plist文件中的FirebaseAppDelegateProxyEnabledflag和   将其值设置为NO(布尔值)。

     

FCM混合会影响您处理默认注册令牌的方式,   以及如何处理下游消息回调。如适用,   本指南提供了有和没有方法的迁移示例   调整启用。

希望你清楚!