Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receiving Events in the Background on Android #21

Closed
hsandhu opened this issue Feb 3, 2021 · 2 comments
Closed

Receiving Events in the Background on Android #21

hsandhu opened this issue Feb 3, 2021 · 2 comments

Comments

@hsandhu
Copy link

hsandhu commented Feb 3, 2021

Thanks so much for your great library @MrHertal!

Running on Android 11 with Firebase Messaging configured, I can receive FCM notifications in the background from an incoming call and answer this call as well. I see the following message on logcat as the call is received:
D/TwilioPhone: Call did connect

However, this event is not received in the event listener in my App.tsx file (I've defined the listeners as you have in your example project). The events are received as expected in App.tsx if the incoming call is initialed while the app is in the foreground. Any thoughts as to why this event is not sent back through the React context and to the JS listener while the app is in the background?

@MrHertal
Copy link
Owner

MrHertal commented Feb 3, 2021

Hi @hsandhu, thanks for your message!

This is normal behavior because when app is in background, code is executed independently of React context.

When you are calling handleBackgroundState, messaging().setBackgroundMessageHandler is registering a headless JS task. The task code is independent and has no access to React context (this context does not even exist since app is in background).

But you can still listen to CallConnected event within that function. You could copy handleBackgroundState and write something like:

import messaging from '@react-native-firebase/messaging';
import { AppRegistry, Platform } from 'react-native';
import RNCallKeep from 'react-native-callkeep';
import {
  RNTwilioPhone,
  TwilioPhone,
  twilioPhoneEmitter,
} from 'react-native-twilio-phone';
import { name as appName } from './app.json';
import { App } from './src/App';

function handleBackgroundState() {
  if (Platform.OS !== 'android') {
    return;
  }

  messaging().setBackgroundMessageHandler(async (remoteMessage) => {
    if (!remoteMessage.data) {
      return;
    }

    RNCallKeep.registerPhoneAccount();
    RNCallKeep.registerAndroidEvents();
    RNCallKeep.setAvailable(true);

    RNTwilioPhone.listenTwilioPhone(); // Listen to events after this line because listeners are removed here
    RNTwilioPhone.listenCallKeep();

    // Here you can listen to events and do what you want
    twilioPhoneEmitter.addListener('CallConnected', (data) => {
      console.log(data);
    })

    TwilioPhone.handleMessage(remoteMessage.data);
  });
}

handleBackgroundState();

AppRegistry.registerComponent(appName, () => App);

@hsandhu
Copy link
Author

hsandhu commented Feb 3, 2021

@MrHertal this is really helpful and makes sense, thank you!

@MrHertal MrHertal closed this as completed Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants