6

I read

But these aren't the solution for react-native-firebase.

I'm using react-native-firebase to handle notifications. In foreground, everything works well. I stuck with background state.

I'm working on a chat project and making 1-to-1 call feature. I push data-notification to clients when they have a call. In iOS, I used CallKit when client received notification, and it worked fine. But in Android I have a problem: I can show ConnectionService UI for a call, but can't connect to my web socket server because it runs on Headless JS service. So I tried to auto open the app. I want to show a call screen as Skype, and accept/deny the call. (Skype can launch the app when I have a call)

Summary, successful steps:

  • Receive call notification in foreground state and show calling screen.
  • Receive call notification in background/closed state and show calling screen on iOS.
  • Receive call notification in background/closed state on Android.

Stuck here:

  • Show calling screen in background/closed on Android ???

TL;DR - How to launch/run/open Android app from Headless JS Service?

I dig into react-native-firebase, it catches the notification on onMessageReceived and send notification message to Headless JS Service.

// If the app is in the background we send it to the Headless JS Service Intent headlessIntent = new Intent( this.getApplicationContext(), RNFirebaseBackgroundMessagingService.class ); headlessIntent.putExtra("message", message); this .getApplicationContext() .startService(headlessIntent); HeadlessJsTaskService.acquireWakeLockNow(this.getApplicationContext());

Any help would be appreciated

tuledev
  • 10,177
  • 4
  • 29
  • 49

1 Answers1

0

This library is helpful to open app from background state. React Native Launch Application

If your app is quit from background state

you've to register registerHeadlessTask in index.js like this

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

and in notificationActionHandler function call libraries function to open app

If your app is in background you can subscribe this

messaging().setBackgroundMessageHandler(remoteMessage => {
      console.log('setBackgroundMessageHandler', remoteMessage.data);
    })
Zuhair Naqi
  • 1,162
  • 11
  • 21