UNPKG

@react-native-firebase/messaging

Version:

React Native Firebase - React Native Firebase provides native integration of Firebase Cloud Messaging (FCM) for both Android & iOS. FCM is a cost free service, allowing for server-device and device-device communication. The React Native Firebase Messaging

605 lines 22.4 kB
import type { ReactNativeFirebase } from '@react-native-firebase/app'; /** * The `RemoteMessage` interface describes an outgoing & incoming message from the remote FCM server. */ export interface RemoteMessage { /** * The collapse key a message was sent with. Used to override existing messages with the same * key. */ collapseKey?: string; /** * A unique ID assigned to every message. * * If not provided, a random unique ID is generated. */ messageId?: string; /** * The message type of the message. */ messageType?: string; /** * The topic name or message identifier. */ from?: string; /** * The address for the message. */ to?: string; /** * The time to live for the message in seconds. * * Defaults to 3600. */ ttl?: number; /** * The time the message was sent, in milliseconds since the start of unix epoch */ sentTime?: number; /** * Any additional data sent with the message. */ data?: { [key: string]: string | object; }; /** * Additional NotificationPayload data sent with the message */ notification?: NotificationPayload; /** * Whether the iOS APNs message was configured as a background update notification. */ contentAvailable?: boolean; /** * Whether the iOS APNs `mutable-content` property on the message was set * allowing the app to modify the notification via app extensions. */ mutableContent?: boolean; /** * The iOS category this notification is assigned to. */ category?: string; /** * An iOS app specific identifier used for notification grouping. */ threadId?: string; /** * Options for features provided by the FCM SDK for Web. */ fcmOptions: FcmOptions; /** * Priority - android-specific, undefined on non-android platforms, default PRIORITY_UNKNOWN */ priority?: MessagePriority; /** * Original priority - android-specific, undefined on non-android platforms, default PRIORITY_UNKNOWN */ originalPriority?: MessagePriority; } /** * Represents the priority of a RemoteMessage * * Note: this is an android-specific property of RemoteMessages */ export declare enum MessagePriority { /** * Unknown priority, this will be returned as the default on non-android platforms */ PRIORITY_UNKNOWN = 0, /** * High priority - Activities may start foreground services if they receive high priority messages */ PRIORITY_HIGH = 1, /** * Normal priority - Activities have restrictions and may only perform unobtrusive actions on receipt */ PRIORITY_NORMAL = 2 } /** * Options for features provided by the FCM SDK for Web. */ export interface FcmOptions { /** * The link to open when the user clicks on the notification. */ link?: string; /** * The label associated with the message's analytics data. */ analyticsLabel?: string; } /** * Options for `getToken()` and `deleteToken()` */ export interface NativeTokenOptions { /** * On Android, the app name of the FirebaseApp instance. */ appName?: string; /** * On iOS, the senderID for a particular Firebase project. */ senderId?: string; } /** * Options for `getToken()` */ export interface GetTokenOptions { /** * On Web, the VAPID key used to authenticate the push subscribers * to receive push messages only from sending servers * that hold the corresponding private key. */ vapidKey?: string; /** * On Web, the service worker registration for receiving push messaging. * If the registration is not provided explicitly, you need to * have a firebase-messaging-sw.js at your root location. */ serviceWorkerRegistration?: any; } /** * NotificationPayload is an alias for Notification. This is to keep it the same as * Firebase Web JS SDK v9 and to make it backwards compatible. */ type NotificationPayload = Notification; export interface Notification { /** * The native localization key for the notification title. */ titleLocKey?: string; /** * Any arguments that should be formatted into the resource specified by titleLocKey. */ titleLocArgs?: string[]; /** * The notification body content. */ body?: string; /** * Web only. The URL to use for the notification's icon. If you don't send this key in the request, * FCM displays the launcher icon specified in your app manifest. */ icon?: string; /** * Web only. The URL of an image that is downloaded on the device and displayed in the notification. */ image?: string; /** * Web only. The notification's title. */ title?: string; /** * The native localization key for the notification body content. */ bodyLocKey?: string; /** * Any arguments that should be formatted into the resource specified by bodyLocKey. */ bodyLocArgs?: string[]; ios?: { /** * The notification's subtitle. */ subtitle?: string; /** * The native localization key for the notification's subtitle. */ subtitleLocKey?: string; /** * Any arguments that should be formatted into the resource specified by subtitleLocKey. */ subtitleLocArgs?: string[]; /** * The value of the badge on the home screen app icon. * If not specified, the badge is not changed. * If set to 0, the badge has been removed. */ badge?: string; /** * The sound played when the notification was delivered on the device (if permissions permit). */ sound?: string | NotificationIOSCriticalSound; }; /** * Additional Android specific properties set on the notification. */ android?: { /** * The sound played when the notification was delivered on the device (channel settings permitted). * * Set as "default" if the default device notification sound was used. */ sound?: string; /** * The channel ID set on the notification. If not set, the notification uses the default * "Miscellaneous" channel set by FCM. */ channelId?: string; /** * The custom color used to tint the notification content. */ color?: string; /** * The custom small icon used to display on the notification. If not set, uses the default * application icon defined in the AndroidManifest file. */ smallIcon?: string; /** * The custom image was provided and displayed in the notification body. */ imageUrl?: string; /** * Deep link URL provided to the notification. */ link?: string; /** * The current unread notification count for this application, managed by the device. */ count?: number; /** * Name of the click action set on the notification. */ clickAction?: string; /** * The notification priority. * * Note; on devices which have channel support (Android 8.0 (API level 26) +), * this value will be ignored. Instead, the channel "importance" level is used. */ priority?: NotificationAndroidPriority; /** * Ticker text set on the notification. * * Ticker text is used for devices with accessibility enabled (e.g. to read out the message). */ ticker?: string; /** * The visibility of a notification. This value determines how the notification is shown on the users * devices (e.g. on the lock-screen). */ visibility?: NotificationAndroidVisibility; }; } /** * Represents a critical sound configuration that can be included in the * `aps` dictionary of an APNs payload. */ export interface NotificationIOSCriticalSound { /** * The critical alert flag. Set to `true` to enable the critical alert. */ critical?: boolean; /** * The name of a sound file in the app's main bundle or in the `Library/Sounds` * folder of the app's container directory. Specify the string "default" to play * the system sound. */ name: string; /** * The volume for the critical alert's sound. Must be a value between 0.0 * (silent) and 1.0 (full volume). */ volume?: number; } /** * The type representing a notification priority. * * Note; on devices which have channel support (Android 8.0 (API level 26) +), * this value will be ignored. Instead, the channel "importance" level is used. */ export type NotificationAndroidPriority = -2 | -1 | 0 | 1 | 2; /** * The type representing the visibility of a notification. */ export type NotificationAndroidVisibility = -1 | 0 | 1; /** * An interface representing all the available permissions that can be requested by your app via * the `requestPermission` API. * * @deprecated Use {@link https://github.com/zoontek/react-native-permissions react-native-permissions} or * {@link https://docs.expo.dev/versions/latest/sdk/notifications/ expo-notifications} for notification permission * requests instead. These APIs will be removed in a future major release. * See {@link https://github.com/invertase/react-native-firebase/issues/6283 #6283}. */ export interface IOSPermissions { /** * Request permission to display alerts. * * Defaults to true. */ alert?: boolean; /** * On iOS >= 13, request permission for Siri to automatically read out notification messages over AirPods. * * Defaults to false. */ announcement?: boolean; /** * Request permission to update the application badge. * * Defaults to true. */ badge?: boolean; /** * Request permission for critical alerts. * * Defaults to false. */ criticalAlert?: boolean; /** * Request permission to display notifications in a CarPlay environment. * * Defaults to true. */ carPlay?: boolean; /** * On iOS, equest permission to provisionally create non-interrupting notifications. * * Defaults to false. */ provisional?: boolean; /** * Request permission to play sounds. * * Defaults to true. */ sound?: boolean; /** * On iOS, request permission to display a button for in-app notification settings. * * Default to false */ providesAppNotificationSettings?: boolean; } /** * A type representing the notification authorization status for this app on the device. * * Value is truthy if authorized, compare against an exact status (e.g. iOS PROVISIONAL) for a more * granular status. * * @deprecated Use {@link https://github.com/zoontek/react-native-permissions react-native-permissions} or * {@link https://docs.expo.dev/versions/latest/sdk/notifications/ expo-notifications} for notification permission * requests instead. These APIs will be removed in a future major release. * See {@link https://github.com/invertase/react-native-firebase/issues/6283 #6283}. */ export type AuthorizationStatus = -1 | 0 | 1 | 2 | 3; /** * An event that is received when a message fails to send. */ export interface SendErrorEvent { /** * The id of the message that failed to send */ messageId: string; /** * A native firebase error that indicates the failure reason. */ error: ReactNativeFirebase.NativeFirebaseError; } /** * The Firebase Messaging service interface. * * > This module is available for the default app only. */ export interface Messaging extends ReactNativeFirebase.FirebaseModule { /** The FirebaseApp this module is associated with */ app: ReactNativeFirebase.FirebaseApp; /** * Returns whether messaging auto initialization is enabled or disabled for the device. */ isAutoInitEnabled: boolean; /** * Sets whether auto initialization for messaging is enabled or disabled. * * @param enabled A boolean value to enable or disable auto initialization. */ setAutoInitEnabled(enabled: boolean): Promise<void>; /** * When a notification from FCM has triggered the application to open from a quit state, * this method will return a `RemoteMessage` containing the notification data, or `null` if * the app was opened via another method. */ getInitialNotification(): Promise<RemoteMessage | null>; /** * When the app is opened from iOS notifications settings from a quit state, * this method will return `true` or `false` if the app was opened via another method. */ getDidOpenSettingsForNotification(): Promise<boolean>; /** * iOS only - Returns whether the root view is headless or not * i.e true if the app was launched in the background (for example, by data-only cloud message) */ getIsHeadless(): Promise<boolean>; /** * Returns an FCM token for this device. Optionally you can specify a custom options to your own use-case. * * @param options Options composite type with all members of `GetTokenOptions` and `NativeTokenOptions` */ getToken(options?: GetTokenOptions & NativeTokenOptions): Promise<string>; /** * Removes access to an FCM token previously authorized by it's scope. Messages sent by the server * to this token will fail. * * @param options Options to override senderId (iOS) and appName (android) */ deleteToken(options?: NativeTokenOptions): Promise<void>; /** * When any FCM payload is received, the listener callback is called with a `RemoteMessage`. * * Returns an unsubscribe function to stop listening for new messages. * * > This subscriber method is only called when the app is active (in the foreground). * * @param listener Called with a `RemoteMessage` when a new FCM payload is received from the server. */ onMessage(listener: (message: RemoteMessage) => any): () => void; /** * When the user presses a notification displayed via FCM, this listener will be called if the app * has opened from a background state. * * @param listener Called with a `RemoteMessage` when a notification press opens the application. */ onNotificationOpenedApp(listener: (message: RemoteMessage) => any): () => void; /** * Called when a new registration token is generated for the device. For example, this event can happen when a * token expires or when the server invalidates the token. * * Returns an unsubscribe function to stop listening for token refresh events. * * > This subscriber method is only called when the app is active (in the foreground). * * @param listener Called with a FCM token when the token is refreshed. */ onTokenRefresh(listener: (token: string) => any): () => void; /** * On iOS, messaging permission must be requested by the current application before messages can * be received or sent. * * > You can safely call this method on Android without platform checks. It's a no-op on Android and will promise resolve `AuthorizationStatus.AUTHORIZED`. * * @deprecated Use {@link https://github.com/zoontek/react-native-permissions react-native-permissions} or * {@link https://docs.expo.dev/versions/latest/sdk/notifications/ expo-notifications} for notification permission * requests instead. These APIs will be removed in a future major release. * See {@link https://github.com/invertase/react-native-firebase/issues/6283 #6283}. */ requestPermission(permissions?: IOSPermissions): Promise<AuthorizationStatus>; /** * On iOS, if your app wants to receive remote messages from FCM (via APNs), you must explicitly register * with APNs if auto-registration has been disabled. * * On **ARM64 iOS Simulator**, UIKit registration is skipped intentionally so the main thread * cannot wedge; the call may reject with `messaging/registration-timeout` after ~10s and will * not produce a real APNs token. Use a physical device for end-to-end push. Overlapping calls * reject the earlier attempt with `messaging/registration-superseded`. * `requestPermission`'s APNs side-effect is also skipped on ARM64 Simulator but that Promise * still resolves with `AuthorizationStatus` (it does not reject with these codes). * * > You can safely call this method on Android without platform checks. It's a no-op on Android and will promise resolve `void`. */ registerDeviceForRemoteMessages(): Promise<void>; /** * On iOS, returns a boolean value whether the user has registered for remote notifications via * `registerDeviceForRemoteMessages()`. * * > You can safely access this property on Android without platform checks. Android returns `true` only. */ isDeviceRegisteredForRemoteMessages: boolean; /** * Returns whether remote notification delegation to Google Play Services is enabled or disabled. * * > You can safely access this property on iOS without platform checks. iOS returns `false` only. */ isNotificationDelegationEnabled: boolean; /** * Returns whether message delivery metrics are exported to BigQuery. */ isDeliveryMetricsExportToBigQueryEnabled: boolean; /** * On iOS, unregisters the app from receiving remote notifications. * * > You can safely call this method on Android without platform checks. It's a no-op on Android and will promise resolve `void`. */ unregisterDeviceForRemoteMessages(): Promise<void>; /** * On iOS, it is possible to get the users APNs token. This may be required if you want to send messages to your * iOS devices without using the FCM service. * * > You can safely call this method on Android without platform checks. It's a no-op on Android and will promise resolve `null`. */ getAPNSToken(): Promise<string | null>; /** * On iOS, This method is used to set the APNs Token received by the application delegate. * * > You can safely call this method on Android without platform checks. It's a no-op on Android and will promise resolve `null`. * * @param token a hexadecimal string representing your APNS token * @param type optional string specifying 'prod', 'sandbox' or 'unknown' token type */ setAPNSToken(token: string, type?: string): Promise<void>; /** * Returns a `AuthorizationStatus` as to whether the user has messaging permission for this app. * * @deprecated Use {@link https://github.com/zoontek/react-native-permissions react-native-permissions} or * {@link https://docs.expo.dev/versions/latest/sdk/notifications/ expo-notifications} for notification permission * requests instead. These APIs will be removed in a future major release. * See {@link https://github.com/invertase/react-native-firebase/issues/6283 #6283}. */ hasPermission(): Promise<AuthorizationStatus>; /** * On Android, called when the FCM server deletes pending messages. * * Returns an unsubscribe function to stop listening for deleted messages. * * @param listener Called when the FCM deletes pending messages. */ onDeletedMessages(listener: () => void): () => void; /** * On Android, when sending a `RemoteMessage`, this listener is called when the message has been sent to FCM. * * Returns an unsubscribe function to stop listening for sent messages. * * @param listener Called when the FCM sends the remote message to FCM. */ onMessageSent(listener: (messageId: string) => any): () => void; /** * When sending a `RemoteMessage`, this listener is called when an error is thrown and the * message could not be sent. * * Returns an unsubscribe function to stop listening for sent errors. * * NOTE: Android only * * @param listener */ onSendError(listener: (evt: SendErrorEvent) => any): () => void; /** * Set a message handler function which is called when the app is in the background * or terminated. * * @param handler called with an argument of type messaging.RemoteMessage that must be async and return a Promise */ setBackgroundMessageHandler(handler: (message: RemoteMessage) => Promise<any>): void; /** * On iOS, set a handler function which is called when the `${App Name} notifications settings` * link in iOS settings is clicked. */ setOpenSettingsForNotificationsHandler(handler: (message: RemoteMessage) => any): void; /** * On Android, send a new `RemoteMessage` to the FCM server. * * @param message A `RemoteMessage` interface. */ sendMessage(message: RemoteMessage): Promise<void>; /** * Apps can subscribe to a topic, which allows the FCM server to send targeted messages to only those * devices subscribed to that topic. * * @param topic The topic name. */ subscribeToTopic(topic: string): Promise<void>; /** * Unsubscribe the device from a topic. * * @param topic The topic name. */ unsubscribeFromTopic(topic: string): Promise<void>; /** * Sets whether message delivery metrics are exported to BigQuery is enabled or disabled. * * @param enabled A boolean value to enable or disable exporting of message delivery metrics to BigQuery. */ setDeliveryMetricsExportToBigQuery(enabled: boolean): Promise<void>; /** * Sets whether remote notification delegation to Google Play Services is enabled or disabled. * * @param enabled A boolean value to enable or disable remote notification delegation to Google Play Services. */ setNotificationDelegationEnabled(enabled: boolean): Promise<void>; /** * On Web, checks if all required APIs exist in the browser. */ isSupported(): Promise<boolean>; } export {}; //# sourceMappingURL=messaging.d.ts.map