0.1.45 • Published 4 months ago

foreground-ss v0.1.45

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

foreground-ss

React Native Expo foreground service

Install guide

npx expo install foreground-ss

add this script to the root of the expo project name withForegroundService.js

const { withAndroidManifest } = require('@expo/config-plugins');

const withForegroundService = (config) => {
  return withAndroidManifest(config, (config) => {
    const permissions = [
        'android.permission.POST_NOTIFICATIONS',
        'android.permission.FOREGROUND_SERVICE',
        'android.permission.FOREGROUND_SERVICE_DATA_SYNC',
      ];

      if (!config.modResults.manifest['uses-permission']) {
        config.modResults.manifest['uses-permission'] = [];
      }

      permissions.forEach((permission) => {
        const alreadyExists = config.modResults.manifest['uses-permission'].some(
          (item) => item.$['android:name'] === permission
        );

        if (!alreadyExists) {
          config.modResults.manifest['uses-permission'].push({
            $: { 'android:name': permission },
          });
        }
      });

    const service = {
      $: {
        'android:name': 'expo.modules.foreground.ForegroundService',
        'android:enabled': 'true',
        'android:exported': 'true',
        'android:foregroundServiceType': 'dataSync',
      },
    };

    // Ensure the <application> tag exists and add the service
    if (!config.modResults.manifest.application[0].service) {
      config.modResults.manifest.application[0].service = [];
    }

    const hasService = config.modResults.manifest.application[0].service.some(
      (existingService) =>
        existingService.$['android:name'] === 'expo.modules.foreground.ForegroundService'
    );

    if (!hasService) {
      config.modResults.manifest.application[0].service.push(service);
    }

    return config;
  });
};

module.exports = withForegroundService;

add config to app.json

"plugins": [..., "./withForegroundService.js"],

Useage

npm run android

In code to start a foreground service the app need

  • Allow Post Notification permission
  • Allow FOREGROUND permission
  • Allow FOREGROUND_DATA_SYNC permisiion

Example useage

import * as ForegroundModule from "foreground-ss";

const requestNotificationPermission = async () => {
  if (Platform.OS === "android") {
    try {
      PermissionsAndroid.check("android.permission.POST_NOTIFICATIONS")
        .then((response) => {
          if (!response) {
            PermissionsAndroid.request(
              "android.permission.POST_NOTIFICATIONS",
              {
                title: "Notification",
                message:
                  "App needs access to your notification " +
                  "so you can get Updates",
                buttonNeutral: "Ask Me Later",
                buttonNegative: "Cancel",
                buttonPositive: "OK",
              }
            );
          }
        })
        .catch((err) => {
          console.log("Notification Error=====>", err);
        });
    } catch (err) {
      console.log(err);
    }
  }
};

export default function Home() {
  // request for foreground service permission
  useEffect(() => {
    requestNotificationPermission();
  }, []);

  const handlePress = () => {
    ForegroundModule.default.startForegroundService(
      // API end point return the data
      "https://dc70-101-99-23-76.ngrok-free.app/data"
    );
  };
  const handleStop = () => {
    ForegroundModule.default.stopForegroundService();
  };
  return (
    <>
      <Stack.Screen options={{ title: "Home" }} />
      <Container>
        <ScreenContent path="app/index.tsx" title="Home" />
        <Button title="Show Details" onPress={handlePress} />
        <Button title="Stop Service" onPress={handleStop} />
      </Container>
    </>
  );
}
0.1.41

4 months ago

0.1.42

4 months ago

0.1.43

4 months ago

0.1.44

4 months ago

0.1.45

4 months ago

0.1.34

5 months ago

0.1.35

5 months ago

0.1.36

5 months ago

0.1.37

5 months ago

0.1.40

5 months ago

0.1.38

5 months ago

0.1.39

5 months ago

0.1.32

5 months ago

0.1.33

5 months ago

0.1.30

5 months ago

0.1.31

5 months ago

0.1.29

5 months ago

0.1.27

6 months ago

0.1.28

6 months ago

0.1.22

6 months ago

0.1.23

6 months ago

0.1.24

6 months ago

0.1.25

6 months ago

0.1.26

6 months ago

0.1.21

6 months ago

0.1.20

6 months ago

0.1.19

6 months ago

0.1.18

6 months ago

0.1.17

6 months ago

0.1.16

6 months ago

0.1.15

6 months ago

0.1.14

6 months ago

0.1.13

6 months ago

0.1.12

6 months ago

0.1.11

6 months ago

0.1.10

6 months ago

0.1.9

6 months ago

0.1.8

6 months ago

0.1.7

6 months ago

0.1.6

6 months ago

0.1.5

6 months ago

0.1.4

6 months ago

0.1.3

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago