Method swizzling in Firebase Cloud Messaging

The FCM SDK performs method swizzling in two key areas:
mapping your APNs token
to the FCM registration token and capturing analytics data during
downstream message callback handling.
Developers who prefer not to use swizzling can
disable it by adding the flag FirebaseAppDelegateProxyEnabled in the app’s
Info.plist file and setting it to NO (boolean value). Relevant areas of the guides provide
code examples, both with and without method swizzling enabled.

Important: With the Firebase Unity SDK on iOS, do not disable method
swizzling. Swizzling is required by the SDK, and without it key Firebase
features such as FCM token handling do not function properly.

Make sure that you enter the ID that your app is
actually using. You cannot add or modify this value after you register
your app with your Firebase project.

Find this bundle ID by opening your app in XCode, then accessing the
General tab in the top-level Runner directory. The value of the
Bundle Identifier field is the iOS bundle ID (for example,
com.yourcompany.yourproject).

(Optional) Enter other app information as prompted by the setup workflow.

App nickname: An internal, convenience identifier that is only
visible to you in the Firebase console

To your Podfile, add the Firebase pods that you want to use in your app.

For an optimal experience with Firebase Cloud Messaging, we recommend
enabling Google Analytics
in your project. As part of setting up Google Analytics, you need to
add the Firebase SDK for Google Analytics to your app.

Browse to the location where you saved your key, select it, and click
Open. Add the key ID for the key (available in Certificates,
Identifiers & Profiles in the
Apple Developer Member Center) and click
Upload.

For devices running iOS 10 and above, you must
assign the UNUserNotificationCenter's
delegate property and FIRMessaging's
delegate property. For example, in an iOS app, assign it in the
applicationWillFinishLaunchingWithOptions: or
applicationDidFinishLaunchingWithOptions: method of the app delegate.

Access the registration token

By default, the FCM SDK generates a
registration token for the client app instance on app launch.
Similar to the APNs device token, this token allows you to send targeted notifications
to any particular instance of your app.

In the same way that iOS typically delivers an APNs device token on app start,
FCM provides a registration token via FIRMessagingDelegate's
messaging:didReceiveRegistrationToken: method.
The FCM SDK retrieves a new or existing token during initial app launch and
whenever the token is updated or invalidated.
In all cases, the FCM SDK calls messaging:didReceiveRegistrationToken:
with a valid token.

The registration token may change when:

The app is restored on a new device

The user uninstalls/reinstall the app

The user clears app data.

Set the messaging delegate

To receive registration tokens, implement the messaging delegate
protocol and set FIRMessaging's delegate property after calling
[FIRApp configure].
For example, if your application delegate conforms to the messaging delegate
protocol, you can set the delegate on application:didFinishLaunchingWithOptions:
to itself.

Swift

Messaging.messaging().delegate = self

Objective-C

[FIRMessaging messaging].delegate = self;

Fetching the current registration token

Registration tokens are delivered via the method
messaging:didReceiveRegistrationToken:. This method is called generally once per
app start with registration token. When this method is called, it is the ideal time to:

If the registration token is new, send it to your application server.

Subscribe the registration token to topics. This is required only for
new subscriptions or for situations where the user has re-installed the app.

You can retrieve the token directly using
instanceIDWithHandler:.
This callback provides an InstanceIDResult, which contains the token. A non null
error is provided if the InstanceID retrieval failed in any way.

Alternatively, you can listen for an NSNotification named
kFIRMessagingRegistrationTokenRefreshNotification
rather than supplying a delegate method. The token property always has the
current token value.

Swizzling disabled: mapping your APNs token and registration token

If you have disabled method swizzling, you'll need to explicitly
map your APNs token to the FCM registration token.
Override the methods didRegisterForRemoteNotificationsWithDeviceToken
to retrieve the APNs token, and then set FIRMessaging's APNSToken
property:

After the FCM registration token is generated, you can access it
and listen for refresh events using the same methods as with swizzling
enabled.

Import existing user APNs tokens

If you have an existing user base that you want to onboard to an
FCM client app, use the
batchImport
API provided by Instance ID. With this API, you can bulk import existing iOS
APNs tokens into FCM, mapping them to new, valid registration tokens.

Prevent auto initialization

FCM generates an Instance ID, which is used as a registration
token within FCM. When an Instance ID is generated the library will upload the
identifier and configuration data to Firebase.If you want to get an explicit
opt-in before using Instance ID, you can prevent generation at configure time by
disabling FCM. To do this, add a metadata value to your Info.plist (not your
GoogleService-Info.plist):

FirebaseMessagingAutoInitEnabled = NO

To re-enable FCM, you can make a runtime call:

Swift

Messaging.messaging().autoInitEnabled = true

Objective-C

[FIRMessaging messaging].autoInitEnabled = YES;

This value persists across app restarts once set.

Next steps

After you have set up your iOS client, you're ready to add message handling
and other, more advanced behavior
to your app. See these guides for more information: