react-native-callkeep icon indicating copy to clipboard operation
react-native-callkeep copied to clipboard

backToForeground not working

Open azhararmar opened this issue 3 years ago • 17 comments

Bug report

  • [ ✓] I've checked the example to reproduce the issue.

  • Reproduced on:

  • [✓ ] Android

  • [ ] iOS

Description

backToForeground does not invoke the app when the app is in background or quit state

Steps to Reproduce

I have following code in index.js

const options = {
    ios: {
        appName: 'App Name',
        imageName: 'logo-trans'
    },
    android: {
        alertTitle: 'Permissions required',
        alertDescription: 'This application needs to access your phone accounts',
        cancelButton: 'Cancel',
        okButton: 'Ok',
    }
};
RNCallKeep.setup(options);

messaging().setBackgroundMessageHandler(async remoteMessage => {
    const { data } = remoteMessage;
    if ('voip' === data?.type) {
        RNCallKeep.backToForeground();
        RNCallKeep.displayIncomingCall(data.uuid, data.handle, data.callerName);
        return;
    }
});

displayIncomingCall is invoked but RNCallKeep.backToForeground() does not do anything

Versions

- Callkeep: 4.2.0
- React Native: 0.61
- Android: 10

Logs

Paste here

azhararmar avatar Oct 09 '21 07:10 azhararmar

Any pointer here is really appreciated. No solution is working for me until now.

azhararmar avatar Oct 09 '21 16:10 azhararmar

I'm also facing the same issue. If anyone managed to resolve this, Please share!

AvibhavChoudhary avatar Oct 13 '21 13:10 AvibhavChoudhary

I made it to work. After tons of debugging and wasting a lot of time, I cross checked the android source from node modules and latest copy available and found that the copy from node modules is from previous version. I then forced updated the library and made some changes and it worked.

I also moved the RNSetup and answer/end call event to index.js this is what my index.js looks like now.

import 'react-native-gesture-handler';
import 'react-native-get-random-values';
import { AppRegistry, Platform } from 'react-native';
import messaging from '@react-native-firebase/messaging';
import RNUnlockDevice from 'react-native-unlock-device';
import RNCallKeep from 'react-native-callkeep';
import App from './App';
import { name as appName } from './app.json';
import { gestureHandlerRootHOC } from 'react-native-gesture-handler';

RNCallKeep.setup({
    ios: {
        appName: 'App Name',
        imageName: 'app-logo-trans'
    },
    android: {
        alertTitle: 'Permissions required',
        alertDescription: 'This application needs to access your phone accounts',
        cancelButton: 'Cancel',
        okButton: 'Ok',
    }
});


messaging().setBackgroundMessageHandler(async remoteMessage => {
    const { data } = remoteMessage;
    if ('voip' === data?.type) {
        RNCallKeep.displayIncomingCall(data.uuid, data.handle, data.callerName);
    }
});

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

RNCallKeep.addEventListener('answerCall', (data) => {
    if ('android' === Platform.OS) {
        RNUnlockDevice.unlock();
        RNCallKeep.endAllCalls();
        RNCallKeep.backToForeground();
    }
});

RNCallKeep.addEventListener('endCall', (data) => {
    RNCallKeep.endAllCalls();
});


I use event emitter https://www.npmjs.com/package/eventemitter3 to communicate from index.js to my root component where I handle all the redirection.

azhararmar avatar Oct 13 '21 18:10 azhararmar

I suppose we cannot call backToForeground before displayIncomingCall which I tried to do. Where I changed my approach mainly is to call backToForeground in answerCall event

azhararmar avatar Oct 13 '21 18:10 azhararmar

I was already calling it in the answer call listener but didn't work out.

AvibhavChoudhary avatar Oct 19 '21 12:10 AvibhavChoudhary

For me this is also not working. My steps on android are:

  1. it receives the call, I invoke displayIncomingCall()
  2. Default android UI popup shows up with options to answer call or decline
  3. When that popup shows and I tap answer, my app goes to background, and any of RN code is not executing anymore
  4. I must manually go back to my app with task switcher, then RN code is continuing an the call starts end everything works as normal

Wherever I put RNCallKeep.backToForeground() it actually doesn't work, because the moment I show incoming call, my app becomes totally inactive.

Is there way around this?

dackom avatar Nov 26 '21 10:11 dackom

@dackom I had to struggle to make RNCallKeep.backToForeground() work for android as well. Finally I noticed my app was not using the latest version of code, although I had installed the latest one. Perhaps, it got installed from my cache.

I suggest you to check yarn.lock (if you are using yarn) and make sure it is using 4.3.1

azhararmar avatar Nov 27 '21 12:11 azhararmar

@azhararmar can you please share how did you use https://www.npmjs.com/package/eventemitter3 in react native. if you share your code snippet it will great. Thanks

AliSaeed090 avatar Dec 14 '21 21:12 AliSaeed090

There seems to be a race condition if the app is already in the foreground when you answer the call.

Here is my understanding of what happens:

  1. The app is in the foreground
  2. It receives a call
  3. The call is accepted
  4. backToForeground() gets called -- while the app is still in the foreground.
  5. The OS switches to the phone UI

This solutions is not elegant at all but worked for me:

RNCallKeep.addEventListener("answerCall", async ({ handle, callUUID, name }) => {
  for (var i = 0; i < 10; i++) {
    RNCallKeep.backToForeground()
  }
})

glesperance avatar Feb 15 '22 03:02 glesperance

Any update on this?

ch3tan03 avatar Apr 29 '22 11:04 ch3tan03

same isssue

minhka195 avatar Nov 17 '22 09:11 minhka195

same issue on Galaxy Z Flip only(latest version of android). Any update?

taekeunn avatar Nov 30 '22 08:11 taekeunn

Facing same issue and @glesperance's workaround works.

ShelbyIB avatar Feb 08 '23 07:02 ShelbyIB

try to enable this permission

Settings app > Other permissions > Display pop-up windows while running in the background

this fixed in my case, Xiaomi 11 phone

Dat-Mobile avatar Feb 15 '23 16:02 Dat-Mobile

There seems to be a race condition if the app is already in the foreground when you answer the call.

Here is my understanding of what happens:

  1. The app is in the foreground
  2. It receives a call
  3. The call is accepted
  4. backToForeground() gets called -- while the app is still in the foreground.
  5. The OS switches to the phone UI

This solutions is not elegant at all but worked for me:

RNCallKeep.addEventListener("answerCall", async ({ handle, callUUID, name }) => {
  for (var i = 0; i < 10; i++) {
    RNCallKeep.backToForeground()
  }
})

This worked for me

vamsimudadla avatar Apr 26 '23 06:04 vamsimudadla