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 remote data messages received via FCM for devices running iOS 10 or above.

    Declaration

    Swift

    weak var remoteMessageDelegate: FIRMessagingDelegate? { get set }

    Objective-C

    @property (readwrite, nonatomic, nullable) id<FIRMessagingDelegate>
        remoteMessageDelegate;
  • 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;
  • 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(completion handler: @escaping FIRMessagingConnectCompletion)

    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.

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

    .googleapis.com`.

    messageID

    The ID of the message. This is generated by the application. It must be unique for each message generated by this application. It allows error callbacks and debugging, to uniquely identify each message.

    ttl

    The time to live for the message. In case we aren’t able to send the message before the TTL expires we will send you a callback. If 0, we’ll attempt to send immediately and return an error if we’re not connected. Otherwise, the message will be queued. As for server-side messages, we don’t return an error if the message has been dropped because of TTL; this can happen on the server side, and it would require extra communication.

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

    Declaration

    Swift

    func appDidReceiveMessage(_ message: [AnyHashable : Any]) -> FIRMessagingMessageInfo

    Objective-C

    - (nonnull FIRMessagingMessageInfo *)appDidReceiveMessage:
        (nonnull NSDictionary *)message;

    Parameters

    message

    The downstream message received by the application.

    Return Value

    Information about the downstream message.