FirebaseMessaging Framework Reference

Messaging

classMessaging:NSObject

Firebase Messaging lets you reliably deliver messages.

To send or receive messages, the app must get a registration token. This token authorizes an app server to send messages to an app instance.

In order to handle incoming Messaging messages, set theUNUserNotificationCenter‘sdelegate property and implement the appropriate methods.

  • Delegate to handle FCM token refreshes, and remote data messages received via FCM direct channel.

    Declaration

    Swift

    weakvardelegate:(anyMessagingDelegate)?{getset}
  • FIRMessaging

    Declaration

    Swift

    classfuncmessaging()->Self

    Return Value

    An instance of Messaging.

  • Unavailable

    Use +messaging instead.

    Unavailable. Use +messaging instead.

  • This property is used to set the APNs Token received by the application delegate.

    Messaging uses method swizzling to ensure that the APNs token is set automatically. However, if you have disabled swizzling by settingFirebaseAppDelegateProxyEnabled toNO in your app’s Info.plist, you should manually set the APNs token in your application delegate’sapplication(_:didRegisterForRemoteNotificationsWithDeviceToken:) method.

    If you would like to set the type of the APNs token, rather than relying on automatic detection, seesetAPNSToken(_:type:).

    Declaration

    Swift

    varapnsToken:Data?{getset}
  • Set the APNs token for the application. This token will be used to register with Firebase Messaging, and will be associated with the app’s installation ID in the form of an FCM token.

    Declaration

    Swift

    funcsetAPNSToken(_apnsToken:Data,type:MessagingAPNSTokenType)

    Parameters

    apnsToken

    The APNs token for the application.

    type

    The type of APNs token. Debug builds should useMessagingAPNSTokenTypeSandbox. Alternatively, you can supplyMessagingAPNSTokenTypeUnknown to have the type automaticallydetected based on your provisioning profile.

  • Is Firebase Messaging token auto generation enabled? If this flag is disabled, Firebase Messaging will not generate an FCM token automatically for message delivery.

    If this flag is disabled, Firebase Messaging does not generate new tokens automatically for message delivery. If this flag is enabled, FCM generates a registration token on application start when there is no existing valid token and periodically refreshes the token and sends data to the Firebase backend.

    This setting is persisted, and is applied on future invocations of your application. Once explicitly set, it overrides any settings in your Info.plist.

    By default, FCM automatic initialization is enabled. If you need to change the default (for example, because you want to prompt the user before getting a token), setFirebaseMessagingAutoInitEnabled to NO in your application’s Info.plist.

    Declaration

    Swift

    varisAutoInitEnabled:Bool{getset}
  • The FCM registration token is used to identify this device so that FCM can send notifications to it. It is associated with your APNs token when the APNs token is supplied, so messages sent to the FCM token will be delivered over APNs.

    The FCM registration token is sometimes refreshed automatically. In your Messaging delegate, the delegate methodmessaging(_:didReceiveRegistrationToken:) will be called once a token is available, or has been refreshed. Typically it should be called once per app start, but may be called more often if the token is invalidated or updated.

    Once you have an FCM registration token, you should send it to your application server, where it can be used to send notifications to your device.

    Declaration

    Swift

    varfcmToken:String?{get}
  • Asynchronously gets the default FCM registration token.

    This creates a Firebase Installations ID, if one does not exist, and sends information about the application and the device to the Firebase backend. A network connection is required for the method to succeed. To stop this, seeMessaging.isAutoInitEnabled,Messaging.delete(completion:) andInstallations.delete(completion:).

    Declaration

    Swift

    functoken()asyncthrows->String

    Parameters

    completion

    The completion handler to handle the token request.

  • Asynchronously deletes the default FCM registration token.

    This does not delete all tokens for non-default sender IDs, SeeMessaging.delete(completion:) for deleting all of them. To prevent token auto generation, seeMessaging.isAutoInitEnabled.

    Declaration

    Swift

    funcdeleteToken()asyncthrows

    Parameters

    completion

    The completion handler to handle the token deletion.

  • Retrieves an FCM registration token for a particular Sender ID. This can be used to allow multiple senders to send notifications to the same device. By providing a different Sender ID than your default when fetching a token, you can create a new FCM token which you can give to a different sender. Both tokens will deliver notifications to your device, and you can revoke a token when you need to.

    This registration token is not cached by FIRMessaging. FIRMessaging should have an APNs token set before calling this to ensure that notifications can be delivered via APNs using this FCM token. You may re-retrieve the FCM token once you have the APNs token set, to associate it with the FCM token. The default FCM token is automatically associated with the APNs token, if the APNs token data is available.

    This creates a Firebase Installations ID, if one does not exist, and sends information about the application and the device to the Firebase backend.

    Declaration

    Swift

    funcretrieveFCMToken(forSenderIDsenderID:String)asyncthrows->String

    Parameters

    senderID

    The Sender ID for a particular Firebase project.

    completion

    The completion handler to handle the token request.

  • Invalidates an FCM token for a particular Sender ID. That Sender ID cannot no longer send notifications to that FCM token. This does not delete the Firebase Installations ID that may have been created when generating the token. SeeInstallations.delete(completion:).

    Declaration

    Swift

    funcdeleteFCMToken(forSenderIDsenderID:String)asyncthrows

    Parameters

    senderID

    The senderID for a particular Firebase project.

    completion

    The completion handler to handle the token deletion.

  • Asynchronously subscribes to a topic. This uses the default FCM registration token to identify the app instance and periodically sends data to the Firebase backend. To stop this, seeMessaging.delete(completion:) andInstallations.delete(completion:).

    Declaration

    Swift

    funcsubscribe(toTopictopic:String)

    Parameters

    topic

    The name of the topic, for example, @“sports”.

  • Asynchronously subscribe to the provided topic, retrying on failure. This uses the default FCM registration token to identify the app instance and periodically sends data to the Firebase backend. To stop this, seeMessaging.delete(completion:) andInstallations.delete(completion:).

    Declaration

    Swift

    funcsubscribe(toTopictopic:String)asyncthrows

    Parameters

    topic

    The topic name to subscribe to, for example, @“sports”.

    completion

    The completion that is invoked once the subscribe call ends. On success, the error parameter is alwaysnil. Otherwise, an appropriate error object is returned.

  • Asynchronously unsubscribe from a topic. This uses a FCM Token to identify the app instance and periodically sends data to the Firebase backend. To stop this, seeMessaging.delete(completion:) andInstallations.delete(completion:).

    Declaration

    Swift

    funcunsubscribe(fromTopictopic:String)

    Parameters

    topic

    The name of the topic, for example @“sports”.

  • Asynchronously unsubscribe from the provided topic, retrying on failure. This uses a FCM Token to identify the app instance and periodically sends data to the Firebase backend. To stop this, seeMessaging.delete(completion:) andInstallations.delete(completion:).

    Declaration

    Swift

    funcunsubscribe(fromTopictopic:String)asyncthrows

    Parameters

    topic

    The topic name to unsubscribe from, for example @“sports”.

    completion

    The completion that is invoked once the unsubscribe call ends. In case of success, nil error is returned. Otherwise, an appropriate error object is returned.

  • Use this to track message delivery and analytics for messages, typically when you receive a notification inapplication:didReceiveRemoteNotification:. However, you only need to call this if you set theFirebaseAppDelegateProxyEnabled flag toNO in your Info.plist. IfFirebaseAppDelegateProxyEnabled is either missing or set toYES in your Info.plist, the library will call this automatically.

    Declaration

    Swift

    funcappDidReceiveMessage(_message:[AnyHashable:Any])->MessagingMessageInfo

    Parameters

    message

    The downstream message received by the application.

    Return Value

    Information about the downstream message.

  • Deletes all the tokens and checkin data of the Firebase project and related data on the server side. A network connection is required for the method to succeed.

    This does not delete the Firebase Installations ID. SeeInstallations.delete(completion:). To prevent token auto generation, seeMessaging.isAutoInitEnabled.

    Declaration

    Swift

    funcdeleteData()asyncthrows

    Parameters

    completion

    A completion handler which is invoked when the operation completes.error ==nil indicates success.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-03-11 UTC.