1import { UnavailabilityError } from 'expo-modules-core'; 2 3import BackgroundNotificationTasksModule from './BackgroundNotificationTasksModule.native'; 4 5/** 6 * When a notification is received while the app is backgrounded, using this function you can set a callback that will be run in response to that notification. 7 * Under the hood, this function is run using `expo-task-manager`. You **must** define the task first, with [`TaskManager.defineTask`](./task-manager#taskmanagerdefinetasktaskname-taskexecutor). 8 * Make sure you define it in the global scope. 9 * 10 * The callback function you define with `TaskManager.defineTask` will receive an object with the following fields: 11 * - `data`: The remote payload delivered by either FCM (Android) or APNs (iOS). See [`PushNotificationTrigger`](#pushnotificationtrigger) for details. 12 * - `error`: The error (if any) that occurred during execution of the task. 13 * - `executionInfo`: JSON object of additional info related to the task, including the `taskName`. 14 * @param taskName The string you passed to `TaskManager.defineTask` as the `taskName` parameter. 15 * 16 * @example 17 * ```ts 18 * import * as TaskManager from 'expo-task-manager'; 19 * import * as Notifications from 'expo-notifications'; 20 * 21 * const BACKGROUND_NOTIFICATION_TASK = 'BACKGROUND-NOTIFICATION-TASK'; 22 * 23 * TaskManager.defineTask(BACKGROUND_NOTIFICATION_TASK, ({ data, error, executionInfo }) => { 24 * console.log('Received a notification in the background!'); 25 * // Do something with the notification data 26 * }); 27 * 28 * Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK); 29 * ``` 30 * @header inBackground 31 */ 32export default async function registerTaskAsync(taskName: string): Promise<null> { 33 if (!BackgroundNotificationTasksModule.registerTaskAsync) { 34 throw new UnavailabilityError('Notifications', 'registerTaskAsync'); 35 } 36 37 return await BackgroundNotificationTasksModule.registerTaskAsync(taskName); 38} 39