npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rn-foreground-service-v

v2.1.20

Published

A Foreground Service for React Native

Downloads

132

Readme

rn-foreground-service-v

Table of Contents

API

Introduction

This project provides a foreground service that you can use to create an always-on background task in React Native. A foreground service in Android is a service that displays a notification that cannot be removed until the service is stopped. This allows the app to continue running tasks in the background, even if the app is closed.

A common use case for this service is tracking a user's location. With the help of Headless.js and a foreground service, you can continue tracking the user's location even if the app has been killed from the recent tasks list, provided that the necessary permissions have been granted.

Installation

npm i rn-foreground-service-v

The installation for the foreground service is straightforward and seamless. With the new v2 update, the service can auto-link itself with your application.

Automatic Linking

Simply run the following command once, and the service will automatically link with your application:

node node_modules/rn-foreground-service-v/postinstall.js

If the above command does not correctly link your application, you can try manually linking the service.

Manual Linking

Step 1:

In the MainActivity file, inside the <activity> tag, add the following metadata and services:

<meta-data
    android:name="com.tikotas.foregroundservice.notification_channel_name"
    android:value="Sticky Title"
/>
<meta-data
    android:name="com.tikotas.foregroundservice.notification_channel_description"
    android:value="Sticky Description."
/>
<meta-data
    android:name="com.tikotas.foregroundservice.notification_color"
    android:resource="@color/blue"
/>
<service android:name="com.tikotas.foregroundservice.ForegroundService"></service>
<service android:name="com.tikotas.foregroundservice.ForegroundServiceTask"></service>

Step 2:

Inside the res/values folder, create a file named colors.xml and add the following:

<resources>
    <item name="blue" type="color">#00C4D1</item>
    <integer-array name="androidcolors">
        <item>@color/blue</item>
    </integer-array>
</resources>

Important Notes

With Android 13, you need to explicty ask for a Post notification permission by the users, this repository does and will not contain code to request perission by the users.

Also you need to mention the permission in android manifest as well

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

For android 14 you need to add another permission as well.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

for tracking location add this alongside to the above permission.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

Getting Started

After Installing and linking the application, the rest of the procedure is pretty stragihtforward. You need register a headless task and and create a foreground service.

Step 1:

In your root, index.js file paste the following code.

import ReactNativeForegroundService from "rn-foreground-service-v";
ReactNativeForegroundService.register();

If you are using expo prebuild version, you should first create index.js file in the root directory of your project and then in your package.json file replace

"main": "node_modules/expo/AppEntry.js",

with

"main": "index.js",

The above code will register an initial headless task, which will be a first layer, over which, you can add as many task as you desire and they all will execute seamlessly.

Step 2:

Create a task and add it the execution queue anywhere in your application, you can create a task like this.

ReactNativeForegroundService.add_task(() => update(), {
  delay: 1000,
  onLoop: true,
  taskId: "taskid",
  onError: () => console.log("Error logging:"),
});

Step 3:

Creating a foreground service, so that your headless task stays active.

ReactNativeForegroundService.start({
  id: 1111,
  title: "Foreground Service",
  message: "Foregrund is active",
  icon: "ic_launcher",
  button: true,
  button2: true,
  buttonText: "Button",
  button2Text: "Second Button",
  buttonOnPress: "cray",
  setOnlyAlertOnce: true,
  color: "#000000",
  progress: {
    max: 100,
    curr: 50,
  },
});

Notification

Methods

| Prop | Type | Returns | Note | |-----------------|------------|--------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------| | start | Promise | A promise that resolves when the notification has been sent. | Sends a notification with the specified options. | | update | Promise | A promise that resolves when the notification has been sent. | Sends a notification with the specified options. | | stop | Promise | A promise that resolves when the service has been stopped. | Stops the foreground service. Note: Pending tasks might still complete. If startService has been called multiple times, this needs to be called as many times. | | stopAll | Promise | A promise that resolves when the service has been stopped. | Stops the foreground service. Note: Pending tasks might still complete. This will stop the service regardless of how many times startService was called. | | eventListener | function | A function that removes the event listener when called. | Adds an event listener that listens for a notificationClickHandle event. |

start \ update Params

| Prop | Type | Default | Note | |------------------|---------------------------------------------------------|-------------------|------------------------------------------------------------------------------------------------------------| | id | number | | Unique notification id. | | title | string | | Notification title. | | message | string | | Notification message. | | number | string | | Int specified as string > 0, for devices that support it, this might be used to set the badge counter. | | icon | string | ic_notification | Small icon name | | largeIcon | string | ic_launcher | Large icon name | | visibility | private, public,secret | | Visibility of the notification | | ongoing | boolean | | Whether the notification is ongoing. The notification the service was started with will always be ongoing. | | importance | min,low,default,high,max,none,unspecified | default | Importance (and priority for older devices) of this notification. This might affect notification sound | | button | boolean | | Whether to include a first button in the notification. | | buttonText | string | | Text for the first button. | | buttonOnPress | string | | Action to take when the first button is pressed. | | button2 | boolean | | Whether to include a second button in the notification. | | button2Text | string | | Text for the second button. | | button2OnPress | string | | Action to take when the second button is pressed. | | mainOnPress | string | | Action to take when the main area of the notification is pressed. | | setOnlyAlertOnce | bollean | false | Whether the notification should only alert the user once | | color | string | | Color of the notification (in hex format, e.g. #000000). | | progress | {max: number, curr: number} | | Object containing progress information for the notification |

eventListener Params

| Prop | Type | Default | Note | |----------|------------|---------|------------------------------------------------------------------------------| | callBack | function | | A function to be called when the notificationClickHandle event is emitted. |

Task

Methods

| Prop | Type | Returns | Note | |--------------------|------------|------------------------------------------------------------------------------------------|------------------------------------------------------------------------| | add_task | function | The ID of the added task. | Adds a new task to the task queue. | | update_task | function | The ID of the updated task. | Updates an existing task in the task queue. | | remove_task | function | void | Removes a task from the task queue. | | is_task_running | function | true if the task is in the queue, false otherwise. | Checks if a task with the specified ID is currently in the task queue. | | remove_all_tasks | function | void | Removes all tasks from the task queue. | | get_task | function | An object representing the task, or null if no task with the specified ID was found. | Gets the task with the specified ID from the task queue. | | get_all_tasks | function | An object containing all tasks in the queue, with task IDs as keys. | Gets all tasks currently in the task queue. |

add_task \ update_task Params

| Prop | Type | Default | Note | |---------|--------------------|---------|--------------------------------------------------------------------------| | task | function | | A function or Promise that represents the task to be added to the queue. | | options | object | | An object containing the following options. See below options |

Options

| Prop | Type | Default | Note | |-------------|------------|----------|------------------------------------------------------------------------| | delay | number | 5000ms | The delay in milliseconds between each execution of the task | | onLoop | boolean | true | Whether the task should be executed repeatedly after the initial delay | | taskId | string | | A unique ID for the task / The ID of the task to be updated | | onSuccess | function | | A function to be called when the task succeeds. | | onError | function | | A function to be called when the task fails. |

remove_task Params

| Prop | Type | Default | Note | |--------|----------|---------|-----------------------------------| | taskId | string | | The ID of the task to be removed. |

is_task_running Params

| Prop | Type | Default | Note | |--------|----------|---------|----------------------------------| | taskId | string | | The ID of the task to check for. |

get_task Params

| Prop | Type | Default | Note | |--------|----------|---------|---------------------------------| | taskId | string | | The ID of the task to retrieve. |