FIRMessaging


@interface FIRMessaging : NSObject

Firebase Messaging lets you reliably deliver messages at no cost.

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

In order to receive FIRMessaging messages, declare application:didReceiveRemoteNotification:.

  • Delegate to handle FCM token refreshes, and remote data messages received via FCM for devices running iOS 10 or above.

    Declaration

    Swift

    weak var delegate: MessagingDelegate? { get set }

    Objective-C

    @property (readwrite, nonatomic, nullable) id<FIRMessagingDelegate> delegate;
  • Delegate to handle remote data messages received via FCM for devices running iOS 10 or above.

    Declaration

    Swift

    weak var remoteMessageDelegate: MessagingDelegate? { get set }

    Objective-C

    @property (readwrite, nonatomic, nullable) id<FIRMessagingDelegate>
        remoteMessageDelegate;
  • When set to YES, Firebase Messaging will automatically establish a socket-based, direct channel to the FCM server. You only need to enable this if you are sending upstream messages or receiving non-APNS, data-only messages in foregrounded apps. Default is NO.

    Declaration

    Swift

    var shouldEstablishDirectChannel: Bool { get set }

    Objective-C

    @property (assign, readwrite, nonatomic) BOOL shouldEstablishDirectChannel;
  • Returns YES if the direct channel to the FCM server is active, NO otherwise.

    Declaration

    Swift

    var isDirectChannelEstablished: Bool { get }

    Objective-C

    @property (readonly, nonatomic) BOOL isDirectChannelEstablished;
  • FIRMessaging

    Declaration

    Swift

    class func messaging() -> Self

    Objective-C

    + (nonnull instancetype)messaging;

    Return Value

    An instance of FIRMessaging.

  • Unavailable. Use +messaging instead.

    Declaration

    Objective-C

    - (nonnull instancetype)init;
  • This property is used to set the APNS Token received by the application delegate.

    FIRMessaging uses method swizzling to ensure the APNS token is set automatically. However, if you have disabled swizzling by setting FirebaseAppDelegateProxyEnabled to NO in your app’s Info.plist, you should manually set the APNS token in your application delegate’s -application:didRegisterForRemoteNotificationsWithDeviceToken: method.

    If you would like to set the type of the APNS token, rather than relying on automatic detection, see: -setAPNSToken:type:.

    Declaration

    Swift

    var apnsToken: Data? { get set }

    Objective-C

    @property (readwrite, copy, nonatomic, nullable) NSData *APNSToken;
  • Set APNS token for the application. This APNS token will be used to register with Firebase Messaging using FCMToken or tokenWithAuthorizedEntity:scope:options:handler.

    Declaration

    Swift

    func setAPNSToken(_ apnsToken: Data, type: MessagingAPNSTokenType)

    Objective-C

    - (void)setAPNSToken:(nonnull NSData *)apnsToken
                    type:(FIRMessagingAPNSTokenType)type;

    Parameters

    apnsToken

    The APNS token for the application.

    type

    The type of APNS token. Debug builds should use FIRMessagingAPNSTokenTypeSandbox. Alternatively, you can supply FIRMessagingAPNSTokenTypeUnknown to have the type automatically detected based on your provisioning profile.

  • The FCM 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 that sending messages to the FCM token will be delivered over APNS.

    The FCM token is sometimes refreshed automatically. You can be notified of these changes via the FIRMessagingDelegate method -message:didRefreshRegistrationToken:, or by listening for the FIRMessagingRegistrationTokenRefreshedNotification notification.

    Once you have an FCM token, you should send it to your application server, so it can use the FCM token to send notifications to your device.

    Declaration

    Swift

    var fcmToken: String? { get }

    Objective-C

    @property (readonly, nonatomic, nullable) NSString *FCMToken;
  • Retrieves an FCM registration token for a particular Sender ID. 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.

    Declaration

    Swift

    func retrieveFCMToken(forSenderID senderID: String, completion: @escaping MessagingFCMTokenFetchCompletion)

    Objective-C

    - (void)retrieveFCMTokenForSenderID:(nonnull NSString *)senderID
                             completion:
                                 (nonnull FIRMessagingFCMTokenFetchCompletion)
                                     completion;

    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.

    Declaration

    Swift

    func deleteFCMToken(forSenderID senderID: String, completion: @escaping MessagingDeleteFCMTokenCompletion)

    Objective-C

    - (void)deleteFCMTokenForSenderID:(nonnull NSString *)senderID
                           completion:(nonnull FIRMessagingDeleteFCMTokenCompletion)
                                          completion;

    Parameters

    senderID

    The senderID for a particular Firebase project.

    completion

    The completion handler to handle the token deletion.

  • Create a FIRMessaging data connection which will be used to send the data notifications sent by your server. It will also be used to send ACKS and other messages based on the FIRMessaging ACKS and other messages based on the FIRMessaging protocol.

    Declaration

    Swift

    func connect(handler: @escaping MessagingConnectCompletion)

    Objective-C

    - (void)connectWithCompletion:(nonnull FIRMessagingConnectCompletion)handler;

    Parameters

    handler

    The handler to be invoked once the connection is established. If the connection fails we invoke the handler with an appropriate error code letting you know why it failed. At the same time, FIRMessaging performs exponential backoff to retry establishing a connection and invoke the handler when successful.

  • Disconnect the current FIRMessaging data connection. This stops any attempts to connect to FIRMessaging. Calling this on an already disconnected client is a no-op.

    Call this before teardown when your app is going to the background. Since the FIRMessaging connection won’t be allowed to live when in background it is prudent to close the connection.

    Declaration

    Swift

    func disconnect()

    Objective-C

    - (void)disconnect;
  • Asynchronously subscribes to a topic.

    Declaration

    Swift

    func subscribe(toTopic topic: String)

    Objective-C

    - (void)subscribeToTopic:(nonnull NSString *)topic;

    Parameters

    topic

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

  • Asynchronously unsubscribe from a topic.

    Declaration

    Swift

    func unsubscribe(fromTopic topic: String)

    Objective-C

    - (void)unsubscribeFromTopic:(nonnull NSString *)topic;

    Parameters

    topic

    The name of the topic, for example @sports.

  • Sends an upstream (device to cloud) message.

    The message is queued if we don’t have an active connection. You can only use the upstream feature if your FCM implementation uses the XMPP server protocol.

    Declaration

    Swift

    func sendMessage(_ message: [AnyHashable : Any], to receiver: String, withMessageID messageID: String, timeToLive ttl: Int64)

    Objective-C

    - (void)sendMessage:(nonnull NSDictionary *)message
                     to:(nonnull NSString *)receiver
          withMessageID:(nonnull NSString *)messageID
             timeToLive:(int64_t)ttl;

    Parameters

    message

    Key/Value pairs to be sent. Values must be String, any other type will be ignored.

    receiver

    A string identifying the receiver of the message. For FCM project IDs the value is SENDER_ID@gcm.googleapis.com.

    messageID