Jessica thompson
Jessica thompson

Reputation: 407

Is there any use of Device Token while integrating FCM for push notification in iOS?

I have following scenarios

Using APNS

For receiving the remote notification for my native iOS apps. While using that we need to create the .p12 certificate and we need to send the Device Token which was generated in Appdelegate.m file while registering for push notification. So we followed the approach of sending the device ID to the backend to send the notification to that particular device.

While Using FCM

I went through the FCM and also got that we need to upload the .p12 file to their console . Everything is fine till this. But when comes to the Device Token part i am unclear about "Swizzling" process of the 'Device Token'. Does firebase generates a Device token or do we need to set the device token generated in 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)
}

Upvotes: 1

Views: 1411

Answers (1)

Anurag Sharma
Anurag Sharma

Reputation: 4855

As per in the Firebase documentation here, It tells that:

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.

And for "Swizzling". The concept is if it is disabled then you have to override the methods didRegisterForRemoteNotificationsWithDeviceToken to retrieve the APNs token, and then call setAPNSToken. As you are already doing this.

Here what the documentation says about Enabling/disabling method swizzling as follows:

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.

Hope it is clear to you!

Upvotes: 2

Related Questions