Rapsssito / react-native-background-actions

React Native background service library for running background tasks forever in Android & iOS.
MIT License
818 stars 117 forks source link

How can i open app on click on the notification? #215

Open AruAram opened 8 months ago

AruAram commented 8 months ago

Hey guys, I need to open the app after I click on the notification. Now it doesn't do anything.

My xml code

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

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

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" />
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
        <intent-filter android:label="filter_react_native"> 
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="yourSchemeHere" />
        </intent-filter>
      </activity>
    </application>
</manifest>

my JSX code

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 */

import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import BackgroundService from 'react-native-background-actions';

const sleep = (time:number) => new Promise<void>((resolve) => setTimeout(() => resolve(), time));

const veryIntensiveTask = async (taskDataArguments:any) => {
    console.log("in intensive task");
    const { delay } = taskDataArguments;
    await new Promise( async (resolve) => {
        for (let i = 0; BackgroundService.isRunning(); i++) {
            const date = `${new Date().getHours()}:${new Date().getMinutes()}:${new Date().getSeconds()}`;
            console.log(`${i} => ${date}`); 
            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', 
    parameters: {
        delay: 1000,
    },
};

export default function Background(){
  const startBackgoundJob=async ()=>{
      await BackgroundService.start(veryIntensiveTask, options);
      console.log("background service started");
  };

  const updateBackgroundJob=async ()=>{
      await BackgroundService.updateNotification({taskDesc: 'New ExampleTask description'}); 
      console.log("background service updated");
  };
  const stopBackgroundJob=async ()=>{
      await BackgroundService.stop();
      console.log("background service stopped");
  };
  return(
      <View style={{flex:1,display:"flex",justifyContent:"center",alignItems:"center"}}>
          <Button title="start background job" onPress={startBackgoundJob}/>
          <Button title="update background job" onPress={updateBackgroundJob}/>
          <Button title="stop background job" onPress={stopBackgroundJob}/>
      </View>
  )
}

Please let me know if you know how i can do it.

dpkthapa commented 3 months ago

https://www.npmjs.com/package/react-native-background-actions#deep-linking

Please use this in you navigation setting

  const linking = {
      prefixes: ['yourSchemeHere://'],
      config: {
        initialRouteName: 'Login',
        screens: {
          Login: {
            path: 'login'
          },
          ChatScreen: {
            path: 'chat/jane' 
          }
        }
      }
  }

<NavigationContainer linking={linking} fallback={<></>}

<Stack.Navigator screenOptions={navigationOptions}

</Stack.Navigator>