{"id":4396,"url":"https://github.com/vicke4/react-native-invoke-app","last_synced_at":"2025-08-04T01:32:21.341Z","repository":{"id":43730742,"uuid":"136394208","full_name":"vicke4/react-native-invoke-app","owner":"vicke4","description":"Bring React Native App to foreground from Headless JS","archived":false,"fork":false,"pushed_at":"2021-06-15T21:34:28.000Z","size":21,"stargazers_count":58,"open_issues_count":11,"forks_count":50,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-25T12:02:08.585Z","etag":null,"topics":["android","headless-js","react-native"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vicke4.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-06T22:49:41.000Z","updated_at":"2024-01-22T22:38:34.000Z","dependencies_parsed_at":"2022-09-11T05:41:20.017Z","dependency_job_id":null,"html_url":"https://github.com/vicke4/react-native-invoke-app","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicke4%2Freact-native-invoke-app","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicke4%2Freact-native-invoke-app/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicke4%2Freact-native-invoke-app/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicke4%2Freact-native-invoke-app/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vicke4","download_url":"https://codeload.github.com/vicke4/react-native-invoke-app/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228389191,"owners_count":17912188,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","headless-js","react-native"],"created_at":"2024-01-05T20:17:10.602Z","updated_at":"2024-12-07T08:30:37.747Z","avatar_url":"https://github.com/vicke4.png","language":"Java","readme":"\n# React Native Invoke App\n\n[![npm version](https://badge.fury.io/js/react-native-invoke-app.svg)](https://badge.fury.io/js/react-native-invoke-app)\n\n[Headless JS](https://facebook.github.io/react-native/docs/headless-js-android.html) is a way to run background tasks in a React Native app. Sometimes we may want to open the app from background task (Headless JS). You can use this module to bring your app to foreground in all the following three cases.\n\n- App is in foreground\n- App is in background\n- App is not running\n\n## Installation\n\n```\n$ npm install --save react-native-invoke-app\n# if your react-native version is \u003c0.60\n$ react-native link react-native-invoke-app\n```\n\n## Usage\n```javascript\nimport invokeApp from 'react-native-invoke-app';\n\n// Within your headless function\ninvokeApp();\n```\n\n## Advanced usage\n\nYou can pass an object to `invokeApp` method to pick it from [DeviceEventEmitter](https://facebook.github.io/react-native/docs/native-modules-android.html#sending-events-to-javascript) by listening to `appInvoked` event.\n\nExample:\n\n```javascript\nconst yourObject = { route: 'Dashboard' };\n\ninvokeApp({\n    data: yourObject,\n})\n```\n\n### Use case\n\nLet's say you want to navigate to dashboard screen of the app after a specific task is completed. You can acheive it like,\n\n```javascript\nimport React, { Component } from 'react';\nimport { AppRegistry, DeviceEventEmitter, Text, View } from 'react-native';\nimport { createStackNavigator } from 'react-navigation';\nimport invokeApp from 'react-native-invoke-app';\n\nimport Dashboard from './dashboard';\n\nclass App extends Component {\n    componentWillMount() {\n        DeviceEventEmitter.addListener('appInvoked', (data) =\u003e {\n\t    const { route } = data;\n\n\t    // Using react-navigation library for navigation.\n\t    this.props.navigation.navigate(route);\n\t});\n    }\n\n    render() {\n        return (\n\t    \u003cView\u003e\n\t\t\u003cText\u003e\n\t\t    This is Home screen.\n\t\t\u003c/Text\u003e\n\t    \u003c/View\u003e\n\t)\n    }\n}\n\nconst appStack = () =\u003e {\n    const Stack = createStackNavigator({\n        App,\n        Dashboard,\n    });\n\n    return \u003cStack /\u003e\n}\n\nconst notificationActionHandler = async (data) =\u003e {\n    // Your background task\n    const yourObject = { route: 'Dashboard' };\n\n    invokeApp({\n\tdata: yourObject,\n    })\n}\n\nAppRegistry.registerHeadlessTask(\n    'RNPushNotificationActionHandlerTask', () =\u003e notificationActionHandler,\n);\n\nAppRegistry.registerComponent('testProject', () =\u003e appStack);\n\n```\n\n## Extra step needed when app is not running\n\nEvent listener will work fine when your app is in background or foreground. If it is not running, to capture the first event we need to do some extra work. Make the following changes in your `MainActivity.java` file of React Native app,\n\n```diff\npackage com.yourpackage;\n\n+import android.os.Bundle;\nimport com.facebook.react.ReactActivity;\n+import com.codegulp.invokeapp.RNInvokeApp;\n\npublic class MainActivity extends ReactActivity {\n    /**\n    * Returns the name of the main component registered from JavaScript.\n    * This is used to schedule rendering of the component.\n    */\n    @Override\n    protected String getMainComponentName() {\n    \treturn \"testProject\";\n    }\n\n+   @Override\n+   protected void onCreate(Bundle savedInstanceState) {\n+       super.onCreate(savedInstanceState);\n+\tRNInvokeApp.sendEvent();\n+   }\n}\n```\n\n### License\n\nMIT\n","funding_links":[],"categories":["Components"],"sub_categories":["Navigation"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicke4%2Freact-native-invoke-app","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvicke4%2Freact-native-invoke-app","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicke4%2Freact-native-invoke-app/lists"}