1.0.57 • Published 5 years ago

react-native-foreground-vpnservice v1.0.57

Weekly downloads
297
License
-
Repository
-
Last release
5 years ago

react-native-foreground-service

A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a notification. Foreground services continue running even when the user isn't interacting with the app.

See the Android official documentation for details on the concept.

Getting started

$ npm install @voximplant/react-native-foreground-service --save

Automatic installation (Android only)

  • React Native 0.60+

    CLI autolink feature links the module while building the app.

    1. Add the FOREGROUND_SERVICE permission to the application's AndroidManifest.xml:
      <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    2. Add VIForegroundService as a service to the application's AndroidManifest.xml:
      <service android:name="com.voximplant.foregroundservice.VIForegroundService"> </service>
  • React Native <= 0.59

    $ react-native link @voximplant/react-native-foreground-service

    1. Add the FOREGROUND_SERVICE permission to the application's AndroidManifest.xml:
      <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    2. Add VIForegroundService as a service to the application's AndroidManifest.xml:
      <service android:name="com.voximplant.foregroundservice.VIForegroundService"> </service>

Manual installation (Android only, React Native <= 0.59)

  1. Open up android/app/src/main/java/[...]/MainActivity.java
    • Add import com.voximplant.foregroundservice.VIForegroundServicePackage; to the imports at the top of the file
    • Add new VIForegroundServicePackage() to the list returned by the getPackages() method
  2. Append the following lines to android/settings.gradle:
    include ':@voximplant_react-native-foreground-service'
    project(':@voximplant_react-native-foreground-service').projectDir = new File(rootProject.projectDir, '../node_modules/@voximplant/react-native-foreground-service/android')
  3. Insert the following lines inside the dependencies block in android/app/build.gradle:
    implementation project(':@voximplant_react-native-foreground-service')
  4. Add the FOREGROUND_SERVICE permission to the application's AndroidManifest.xml:
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
  5. Add VIForegroundService as a service to the application's AndroidManifest.xml:

    <service android:name="com.voximplant.foregroundservice.VIForegroundService"> </service>

Demo project

Demo application: react-native-foreground-service-demo

Usage

Import module

import VIForegroundService from '@voximplant/react-native-foreground-service';

Create notification channel (Android 8+)

Since the foreground service must display a notification, for Android 8+ it is required to create a notification channel first:

const channelConfig = {
    id: 'channelId',
    name: 'Channel name',
    description: 'Channel description',
    enableVibration: false
};
VIForegroundService.createNotificationChannel(channelConfig);

Start foreground service

async startForegroundService() {
    const notificationConfig = {
        channelId: 'channelId',
        id: 3456,
        title: 'Title',
        text: 'Some text',
        icon: 'ic_icon'
    };
    try {
        await VIForegroundService.startService(notificationConfig);
    } catch (e) {
        console.error(e);
    }
}

Stop foreground service

VIForegroundService.stopService();

Reference

Methods

static async startService(notificationConfig)

Starts the foreground service and displays a notification with the defined configuration


static async stopService()

Stops the foreground service


static async createNotificationChannel(channelConfig)

Creates a notification channel for the foreground service. For Android 8+ the notification channel should be created before starting the foreground service

Configs

NotificationChannelConfig
Property nameDescriptionRequired
idUnique channel idyes
nameNotification channel nameyes
descriptionNotification channel descriptionno
importanceNotification channel importance. One of:1 – 'min' 2 – 'low' (by default)3 – 'default'4 – 'high'5 – 'max'no
enableVibrationSets whether notification posted to this channel should vibrate. False by default.no
NotificationConfig
Property nameDescriptionRequired
channelIdNotification channel id to display the notificationyes (Android 8+ only)
idUnique notification idyes
titleNotification titleyes
textNotification textyes
iconIcon nameyes
priorityPriority of this notification. One of:  0 – PRIORITY_DEFAULT (by default)-1 – PRIORITY_LOW-2 – PRIORITY_MIN 1 – PRIORITY_HIGH 2 – PRIORITY_MAXno
1.0.57

5 years ago

1.0.56

5 years ago

1.0.55

5 years ago

1.0.54

5 years ago

1.0.53

5 years ago

1.0.52

5 years ago

1.0.51

5 years ago

1.0.50

5 years ago

1.0.49

5 years ago

1.0.48

5 years ago

1.0.47

5 years ago

1.0.46

5 years ago

1.0.45

5 years ago

1.0.44

5 years ago

1.0.43

5 years ago

1.0.42

5 years ago

1.0.41

5 years ago

1.0.40

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.37

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

2.0.0

5 years ago