### Install react-native-background-actions with Yarn or npm Source: https://github.com/huddle01/react-native-background-actions/blob/main/INSTALL.md Installs the background actions library for React Native using either Yarn or npm package managers. ```shell yarn add @huddle01/react-native-background-actions ``` ```shell npm install --save @huddle01/react-native-background-actions ``` -------------------------------- ### Start, Update, and Stop Background Service (JavaScript) Source: https://github.com/huddle01/react-native-background-actions/blob/main/README.md Demonstrates how to initiate, update the notification for, and stop a background service using the library. Note that notification updates are only applicable to Android. Calling stop() prevents future task starts, and calling start() twice restarts tasks. ```javascript import BackgroundService from '@huddle01/react-native-background-actions'; const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(), time)); // You can do anything in your task such as network requests, timers and so on, // as long as it doesn't touch UI. Once your task completes (i.e. the promise is resolved), // React Native will go into "paused" mode (unless there are other tasks running, // or there is a foreground app). const veryIntensiveTask = async (taskDataArguments) => { // Example of an infinite loop task const { delay } = taskDataArguments; await new Promise( async (resolve) => { for (let i = 0; BackgroundService.isRunning(); i++) { console.log(i); await sleep(delay); } }); }; const options = { taskName: 'Example', taskTitle: 'ExampleTask title', taskDesc: 'ExampleTask description', taskIcon: { name: 'ic_launcher', type: 'mipmap', }, color: '#ff00ff', linkingURI: 'yourSchemeHere://chat/jane', // See Deep Linking for more info parameters: { delay: 1000, }, }; await BackgroundService.start(veryIntensiveTask, options); await BackgroundService.updateNotification({taskDesc: 'New ExampleTask description'}); // Only Android, iOS will ignore this call // iOS will also run everything here in the background until .stop() is called await BackgroundService.stop(); ``` -------------------------------- ### Android App Killed Event Listener Source: https://github.com/huddle01/react-native-background-actions/blob/main/README.md Adds a listener for the 'appKilled' event, which is specific to Android. This event is triggered when the Android app is terminated by the user, for example, by swiping it away from recent tasks. ```javascript DeviceEventEmitter.addListener('appKilled', () => { console.log('App is killed :('); }); ``` -------------------------------- ### Manually Link react-native-background-actions on iOS (React Native < 0.60) Source: https://github.com/huddle01/react-native-background-actions/blob/main/INSTALL.md Provides step-by-step instructions for manually linking the library in Xcode for older React Native versions, including adding the project and library files. ```shell 1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]` 2. Go to `node_modules` ➜ `react-native-background-actions` and add `RNBackgroundActions.xcodeproj` 3. In XCode, in the project navigator, select your project. Add `libRNBackgroundActions.a` to your project's `Build Phases` ➜ `Link Binary With Libraries` 4. Run your project (`Cmd+R`) ``` -------------------------------- ### iOS Info.plist Configuration for Background Tasks Source: https://github.com/huddle01/react-native-background-actions/blob/main/INSTALL.md Adds necessary keys to the Info.plist file on iOS to permit background task scheduling, essential for the background actions library. ```xml BGTaskSchedulerPermittedIdentifiers $(PRODUCT_BUNDLE_IDENTIFIER) ``` -------------------------------- ### Manually Link react-native-background-actions on Android (React Native < 0.60) Source: https://github.com/huddle01/react-native-background-actions/blob/main/INSTALL.md Details the manual linking process for Android for React Native versions below 0.60, involving modifications to MainApplication.java, settings.gradle, and build.gradle. ```java 1. Open up `android/app/src/main/java/[...]/MainApplication.java` - Add `import com.asterinet.react.bgactions.BackgroundActionsPackage;` to the imports at the top of the file - Add `new BackgroundActionsPackage()` to the list returned by the `getPackages()` method 2. Append the following lines to `android/settings.gradle`: ``` include ':react-native-background-actions' project(':react-native-background-actions').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-actions/android') ``` 3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: ``` compile project(':react-native-background-actions') ``` ``` -------------------------------- ### Android Manifest Permissions and Service Configuration Source: https://github.com/huddle01/react-native-background-actions/blob/main/INSTALL.md Configures the AndroidManifest.xml file by adding foreground service permissions and specifying the foregroundServiceType for Android 14+ compatibility. ```xml ... ... ``` ```xml ... ... ... ... ``` -------------------------------- ### Configure BackgroundService with Linking URI Source: https://github.com/huddle01/react-native-background-actions/blob/main/README.md Sets up the BackgroundService options with a linkingURI that matches the scheme defined in the AndroidManifest.xml. This enables deep linking functionality by specifying the URI the service should listen for. ```javascript const options = { taskName: 'Example', taskTitle: 'ExampleTask title', taskDesc: 'ExampleTask description', taskIcon: { name: 'ic_launcher', type: 'mipmap', }, color: '#ff00ff', linkingURI: 'yourSchemeHere://chat/jane', // Add this parameters: { delay: 1000, }, }; await BackgroundService.start(veryIntensiveTask, options); ``` -------------------------------- ### Background Task Options Source: https://github.com/huddle01/react-native-background-actions/blob/main/README.md Configuration options for setting up and customizing background tasks, primarily for Android notifications. ```APIDOC ## Background Task Configuration ### Description This section outlines the available options for configuring background tasks and their associated notifications on Android. ### Method N/A (Configuration Object) ### Endpoint N/A ### Parameters #### Request Body (Options Object) - **taskName** (string) - Required - Task name for identification. - **taskTitle** (string) - Required (Android) - Notification title. - **taskDesc** (string) - Required (Android) - Notification description. - **taskIcon** (object) - Required (Android) - Notification icon configuration. See `taskIconOptions` below. - **color** (string) - Optional - Notification color. Default: `"#ffffff"`. - **linkingURI** (string) - Optional - Deep link URI to be called when the notification is clicked. Default: `undefined`. - **priority** (string) - Optional (Android) - Priority of the notification. Values: `MIN`, `LOW`, `DEFAULT`, `HIGH`, `MAX`. - **importance** (string) - Optional (Android) - Sets the level of interruption of the notification channel. Values: `NONE`, `MIN`, `LOW`, `DEFAULT`, `HIGH`, `MAX`. - **ongoing** (boolean) - Optional (Android) - Set whether this is an on-going notification. Default: `true`. - **autoCancel** (boolean) - Optional (Android) - Set whether the notification is automatically canceled when the user presses it. Default: `false`. - **stopOnTerminate** (boolean) - Optional (Android) - Set whether background tasks should be terminated when the app is killed. Default: `false`. - **progressBar** (object) - Optional - Notification progress bar configuration. - **parameters** (any) - Optional - Parameters to pass to the task. ### taskIconOptions #### Parameters - **name** (string) - Required - Icon name in the `res/` folder (e.g., `ic_launcher`). - **type** (string) - Required - Icon type in the `res/` folder (e.g., `mipmap`). - **package** (string) - Optional - Icon package to search within. Defaults to the app's package. ### Request Example ```json { "taskName": "my_background_task", "taskTitle": "Processing Data", "taskDesc": "Your data is being processed in the background.", "taskIcon": { "name": "ic_launcher", "type": "mipmap" }, "color": "#000000", "linkingURI": "app://my.app/task-status", "priority": "HIGH", "importance": "HIGH", "ongoing": true, "autoCancel": false, "stopOnTerminate": false, "progressBar": { "max": 100, "progress": 50, "indeterminate": false }, "parameters": { "userId": "user123" } } ``` ### Response N/A (This is a configuration object, not an API endpoint that returns a response). ``` -------------------------------- ### Listen for Incoming Deep Links with React Native Linking Source: https://github.com/huddle01/react-native-background-actions/blob/main/README.md Utilizes React Native's Linking class to listen for incoming URLs and handle them when a notification is clicked. This involves adding an event listener for the 'url' event. ```javascript import { Linking } from 'react-native'; Linking.addEventListener('url', handleOpenURL); function handleOpenURL(evt) { // Will be called when the notification is pressed console.log(evt.url); // do something } ``` -------------------------------- ### Android Manifest Deep Linking Configuration Source: https://github.com/huddle01/react-native-background-actions/blob/main/README.md Configures the AndroidManifest.xml to handle incoming deep links by adding an intent filter to the main activity. This allows the application to respond to specific URL schemes when a notification is clicked. ```xml ... ... ``` -------------------------------- ### iOS Expiration Event Listener Source: https://github.com/huddle01/react-native-background-actions/blob/main/README.md Implements an event listener for the 'expiration' event, which is specific to iOS. This allows developers to perform cleanup operations shortly before the app's background time expires, as detailed in the iOS documentation. ```javascript BackgroundService.on('expiration', () => { console.log('I am being closed :('); }); await BackgroundService.start(veryIntensiveTask, options); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.