Code Monkey home page Code Monkey logo

react-native-invoke-app's Introduction

React Native Invoke App

npm version

Headless JS 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.

  • App is in foreground
  • App is in background
  • App is not running

Installation

$ npm install --save react-native-invoke-app
# if your react-native version is <0.60
$ react-native link react-native-invoke-app

Usage

import invokeApp from 'react-native-invoke-app';

// Within your headless function
invokeApp();

Advanced usage

You can pass an object to invokeApp method to pick it from DeviceEventEmitter by listening to appInvoked event.

Example:

const yourObject = { route: 'Dashboard' };

invokeApp({
    data: yourObject,
})

Use case

Let's say you want to navigate to dashboard screen of the app after a specific task is completed. You can acheive it like,

import React, { Component } from 'react';
import { AppRegistry, DeviceEventEmitter, Text, View } from 'react-native';
import { createStackNavigator } from 'react-navigation';
import invokeApp from 'react-native-invoke-app';

import Dashboard from './dashboard';

class App extends Component {
    componentWillMount() {
        DeviceEventEmitter.addListener('appInvoked', (data) => {
	    const { route } = data;

	    // Using react-navigation library for navigation.
	    this.props.navigation.navigate(route);
	});
    }

    render() {
        return (
	    <View>
		<Text>
		    This is Home screen.
		</Text>
	    </View>
	)
    }
}

const appStack = () => {
    const Stack = createStackNavigator({
        App,
        Dashboard,
    });

    return <Stack />
}

const notificationActionHandler = async (data) => {
    // Your background task
    const yourObject = { route: 'Dashboard' };

    invokeApp({
	data: yourObject,
    })
}

AppRegistry.registerHeadlessTask(
    'RNPushNotificationActionHandlerTask', () => notificationActionHandler,
);

AppRegistry.registerComponent('testProject', () => appStack);

Extra step needed when app is not running

Event 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,

package com.yourpackage;

+import android.os.Bundle;
import com.facebook.react.ReactActivity;
+import com.codegulp.invokeapp.RNInvokeApp;

public class MainActivity extends ReactActivity {
    /**
    * Returns the name of the main component registered from JavaScript.
    * This is used to schedule rendering of the component.
    */
    @Override
    protected String getMainComponentName() {
    	return "testProject";
    }

+   @Override
+   protected void onCreate(Bundle savedInstanceState) {
+       super.onCreate(savedInstanceState);
+	RNInvokeApp.sendEvent();
+   }
}

License

MIT

react-native-invoke-app's People

Contributors

janakact avatar saeedzhiany avatar vicke4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-native-invoke-app's Issues

Android 9 Issues

Hi

First of all, thank you for writing this plugin and sharing it with everyone.

The plugin does not seem to work with Android 9, I have tested with Android 8 and the plugin works without any issues but with Android 9, nothing happens. Any ideas as to what's going on?

listener not work's

hi, i used this plugin in react-native :^0.57.1, i want to after invoke route to specific ecene like you said but listener not fire:
app.js:
componentWillMount(){
DeviceEventEmitter.addListener('appInvoked', (data) => {
const { route } = data;
console.log("data IS:");
console.log(data);
});
}
and i added to mainActivity.java part of code you said, but my log not appears,
can you help me what's wrong?

invokeApp is not called when app is in background, DeviceEventEmitter is only called upon opening app when app is killed

I am receiving data only Firebase Cloud Messages when my Android React Native app is in the background or is killed. Here is my headless task:

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => backgroundMessaging);

Inside my backgroundMessaging function I have this code:

const backgroundMessaging = async (message) => {
  // handle your message
  console.log(message);
  const log = { logMessage: 'this is the event emitter log' }
  invokeApp({ data: log });
  return Promise.resolve();
}
export default backgroundMessaging;

When the app is in the background the above function logs the message, but invokeApp does not seem to be called because the DeviceEventEmitter does not log anything:

    DeviceEventEmitter.addListener('appInvoked', (data) => {
      const { logMessage } = data;
      console.log(logMessage)
    });

However, when the app is killed (not in background), once I open the app the DeviceEventEmitter function logs the logMessage.

At no point does the app come to the foreground.

ERROR WHILE BUILDING APP

I fixed it already. change 'compile' to 'implementation' in node_modules/react-native-invoke-app/android/build.gradle line 44

Android 5

E/RNInvokeApp( 8137): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
E/RNInvokeApp( 8137):   at android.app.ContextImpl.startActivity(ContextImpl.java:1377)
E/RNInvokeApp( 8137):   at android.app.ContextImpl.startActivity(ContextImpl.java:1357)
E/RNInvokeApp( 8137):   at android.content.ContextWrapper.startActivity(ContextWrapper.java:324)
E/RNInvokeApp( 8137):   at android.content.ContextWrapper.startActivity(ContextWrapper.java:324)
E/RNInvokeApp( 8137):   at com.codegulp.invokeapp.RNInvokeApp.invokeApp(RNInvokeApp.java:55)
E/RNInvokeApp( 8137):   at java.lang.reflect.Method.invoke(Native Method)
E/RNInvokeApp( 8137):   at java.lang.reflect.Method.invoke(Method.java:372)
E/RNInvokeApp( 8137):   at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
E/RNInvokeApp( 8137):   at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:158)
E/RNInvokeApp( 8137):   at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
E/RNInvokeApp( 8137):   at android.os.Handler.handleCallback(Handler.java:815)
E/RNInvokeApp( 8137):   at android.os.Handler.dispatchMessage(Handler.java:104)

Expectation:
Application to open

Reality:
Nothing happens and above error can be seen in the adb logcat.

Android Version: 5
React Native Version: 0.59

This error is showing up on React-Native 0.70.1.

A problem occurred evaluating project ':react-native-invoke-app'. Could not find method compile() for arguments [com.facebook.react:react-native:+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Fixed by changing

compile 'com.facebook.react:react-native:+'

to

implementation 'com.facebook.react:react-native:+'

on /android/build.gradle on line 44

Crashing when invoking

the app crashes when invocking appInvoked method, currently using react-native version 0.42
Error
Cannot read property invokeApp of undifined

import invokeApp from 'react-native-invoke-app';

invokeApp({
      data: 'myroute',
    });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.