[Android] displayIncomingCall either doesn't called or called with a long delay in when the app is launched from high priority FCM arrives and device in idle mode (Doze)
I'm trying to solve the following problem: receiving an incoming call and displaying an incoming call window in the application, even when the application is killed.
I assume this is solved as follows:
- High priority FCM message is sent to the Android 10 device (Pixel 2).
- The App start the HeadlessJS task and call the RNCallKeep.displayIncomingCall function.
My FCM messages looks like this:
{
"data":
{
"message":"New call",
"sound":"default",
"title":"New INVITE",
"to":"[email protected]",
"type":2
},
"priority":"high",
"registration_ids": ["xxx"],
}
Implementation of FCM messages processing and display of an incoming call:
index.js
...
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);
...
bgMessaging.js
...
export default async (message: RemoteMessage) => {
const options = {
ios: {
appName: 'MyApp',
imageName: '../img/TabBar/contacts.imageset/contacts.png',
ringtoneSound: 'my_ringtone_sound_filename_in_bundle',
maximumCallGroups: '1',
maximumCallsPerCallGroup: '1'
},
android: {
alertTitle: 'Permissions Required',
alertDescription: 'This application needs to access your phone calling accounts to make calls',
cancelButton: 'Cancel',
okButton: 'ok',
imageName: 'sim_icon',
additionalPermissions: [PermissionsAndroid.PERMISSIONS.READ_CONTACTS]
}
};
RNCallKeep.setup(options);
RNCallKeep.setAvailable(true);
if(message.data.type==='2'){
let _uuid = uuid.v4();
RNCallKeep.displayIncomingCall(_uuid, "123", "123");
}
return Promise.resolve();
}
This works fine and the incoming call screen is displayed:
- If the App has just been killed and the device display is still active.
- If the App has just been killed and the device’s display has been blocked for a while.
This doesn't work or works with a long delay (about 60 sec after FCM received I see incoming call display) if the App was killed and the device’s screen was blocked and some time passed, for example 5 minutes. I think device in idle mode (Doze) in this moment .
What's wrong? How to make guaranteed to open the window of an incoming call after the arrival of a high-priority FCM-message in idle-mode (and inactive-mode) device?
Hi @Prussich, We do use FCM to wakeup our application and it works perfectly.
If you have some console.log in bgMessaging.js, did you see your application starting in zombie mode ? At the end of the call you have to disconnect from all your services (websocket, ...).
Did you send a push for each call ? You have to wake up your application for each call you receive (it will be faster after the first wake up).
Hi @manuquentin ,
I added some code to onMessageReceived(RemoteMessage message) in RNFirebaseMessagingService.js:
PowerManager pm = (PowerManager) this.getApplicationContext().getSystemService(POWER_SERVICE);
int flags=PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE;
PowerManager.WakeLock sCpuWakeLock = pm.newWakeLock(flags, "com.test:PushWakeUp");
sCpuWakeLock.acquire();
...
HeadlessJsTaskService.acquireWakeLockNow(this.getApplicationContext());
and now it's work fine! But a slight inconvenience - every time before displaying an incoming call, the display lights up. If I change SCREEN_BRIGHT_WAKE_LOCK to PARTIAL_WAKE_LOCK, RNCallKeep.displayIncomingCall calls with the long delay 10-15 sec. I conclude that only the start of the device display takes the device out of idle and only in this case I can call RNCallKeep.displayIncomingCall.
Did you send a push for each call ?
Yes.
it will be faster after the first wake up
Yes, I know.
and it works perfectly
Are you sure? Did you test your app when the power cable is disconnected with a deep idle on the Android 10 device?
I conclude that only the start of the device display takes the device out of idle. The receiving of a push with high priority wakes up your phone and then you have to call
displayIncomingCall.
Maybe it's an issue with react-native-firebase.
Did you test your app when the power cable is disconnected with a deep idle on the Android 10 device? Yes of course, but we have tested with Android 9, not 10 yet.
@manuquentin I left the phone for a long time (all night). I woke up and started calling the application - no reaction. Apparently besides idle, the device and the app goes into a state (inactive) when network access is completely unavailable and SCREEN_BRIGHT_WAKE_LOCK not wake up the application completely. Calling to a Viber app brought no reaction either. Only Whatsup showing incoming call immediately.
Maybe it's an issue with react-native-firebase.
Maybe, but then there is no tool guaranteeing an incoming call, using react-native-callkeep on Android device.
We do use FCM to wakeup our application and it works perfectly.
Is it public app? Can I test your app in my device?
Hi, I have one brief question as I am trying to achieve the same:
RNCallKeep.setup(options);
RNCallKeep.setAvailable(true);
Does this code work for you? Doesn't it throws an exception?
In my case I'm getting:
Unhandled promise rejection Error: Activity doesn't exist
at createErrorFromErrorData (NativeModules.js:155)
at NativeModules.js:104
at MessageQueue.__invokeCallback (MessageQueue.js:414)
at MessageQueue.js:127
at MessageQueue.__guard (MessageQueue.js:314)
at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:126)
at e (RNDebuggerWorker.js:1)
Hi @renefs Because one of the alerts of some other permissions prevents it from opening CallKeep's alert (activity doesn't exist). Just check permissions that you call and do the RNCallKeep.setup(options) after you get the rest of the permissions. Otherwise, a conflict arises. For example:
async requestContactsPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
{
title: 'Contacts',
message: 'Contacts permissions needed.',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Contacts permission granted');
this.loadContacts();
RNCallKeep.setup(options);
RNCallKeep.setAvailable(true);
} else {
console.log('Contacts permission denied');
}
} catch (err) {
console.warn(err);
}
}
This is really an unpleasant feature and took me the some time. I think that many more will stumble upon this problem. Most likely this is true for issue #89
Hi @Prussich,
But do you have that code in the bgMessaging.js file? I get the exception when the push notification arrives:
export default async (store, message) => {
console.log('Receiving a background PUSH Notification');
const options = {
ios: {
appName: 'Phone App'
},
android: {
alertTitle: 'Permissions required',
alertDescription: 'This application needs to access your phone accounts',
cancelButton: 'Cancel',
okButton: 'ok'
}
};
const devMode = false;
const toneAPI = new Dial(devMode);
RNCallKeep.setup(options);
RNCallKeep.setAvailable(true);
RNCallKeep.addEventListener('answerCall', () => {
console.log('Received answerCall event');
toneAPI.answer();
});
const authToken = store.getState().auth.toneToken;
if (authToken) {
const firstRegister = false;
const number = '12345';
const tempToken = '12345;
toneAPI.authenticate(number, tempToken);
} else {
console.log('authToken is not defined');
}
}
return Promise.resolve();
};
I have no problem with the RNCallkeep when I setup the permissions when the main application is working, only here on the headless file. Thank you for your help!
EDIT: Fix typo
@renefs
This code worked for me. But I think that in your case, the app is trying to show alert and there is no activity.
@Prussich yes, I think that's happening. What I don't understand is why. I already gave the application permissions at this point
@renefs did you managed to solve the problem ? I am facing the same issue as well. When app is in killed state, i send "silent notification" which wakes the app and calls headless task file 'bgMessaging.js'. Inside this file i have done things as follow,
- create a new endpoint and store data (accounts, endpoint instance in redux store) (success)
- register SIP account so that i can receive the incoming call. (success)
- Initialise callkeep and add listeners related to callkeep (this step fails.)
I am getting error on this line :-
RNCallKeep.setup(options);
and error says Error: "Activity doesn't exist"
@Prussich @manuquentin Can you please suggest what can be done ? PS :- I have gone through ticket #97 #89 but not able to solve the issue.
@rohitvc I'm afraid I'm still facing this problem, but it seems it doesn't block the rest of the code to work. May I ask you how do you access the redux store from the headless js?
@renefs I am not accessing redux store from headless js but from main App.js file by calling INIT function from outside of the App component which loads when silent notification wakes the app.
Right now i am successfully able to register SIP account on receiving the silent notification and i can see that call is also received inside the app from logs but Callkeep is not getting registered properly because of above mentioned error and i am not able to receive the call in native dialer in background.
If there is any other way to handle this please suggest ?
Hi @rohitvc,
When doing a setup I receive the Activity doesn't exist message but in a yellow box, meaning that it's just a warning and the callkeep UI is well displayed.
Does your application stop right after calling setup ?
Do your register to Callkeep events too ? What device are you using ?
Hey, @manuquentin sorry for late reply, i was able to make it work even after getting the error Activity doesn't exist
Does your application stop right after calling setup ?
No as i checked, the error did not stop the application and rest of the app code runs fine. In my case, the problem was setTimeout which was stopping the execution of the entire code and causing the problem. JS setTimeout does not work inside headless task as expected and also according to this answer on stackoverflow we should use native timer which react-native-background-timer provides. In my case setTimeout was not required, so i removed it. After which i was able to register to Callkeep events and call was properly received in native dialer. Thanks for the help.
@manuquentin I am calling RNCallKeep.setup in a try/catch, and when being triggered by a background push notification (when the app is closed) it throws the Activity doesn't exist exception which stops the execution of my application. If I ignore this exception and register the listeners as usual and call RNCallKeep.setAvailable then it appears to function correctly.
I don't think Activity doesn't exist should be an exception since it is possible to register listeners and use RNCallKeep when the app is closed in response to a background push notification.
@grit96 Thank you so much! Your instruction works perfect!
I moved RNCallKeep.setup inside try/catch and start working (but with alert Activity doesn't exist).
Ive managed to solve our problem which was caused by "setTimeout" like @rohitvc referenced.
@NickMendes How did you manage to launch the app from headlessJS ?
I prefer to launch the app entirely(UI, bypass the locked screen..and bring it to foreground) when app is closed or in background
any luck here?