My app has a VoIP feature for users, controlled via Firebase (FCM & rtdb).
My problem:
I'm unable to launch the app when a e.g. VoIP incoming call fcm message (notification) arrives. This is crucial as showing this only as a popup push notification isn't sufficient. I need to be able to launch the app from FirebaseMessaging onBackgroundMessage Isolate function. (see example mcve code below)
Requirements of Isolate (Android specific):
When received, an isolate is spawned (Android only, iOS/macOS does not require a separate isolate) allowing you to handle messages even when your application is not running.
There are a few things to keep in mind about your background message handler:
- It must not be an anonymous function.
- It must be a top-level function (e.g. not a class method which requires initialization).
Sample (MCVE) flutter setup:
Thus, my code is setup to call this Isolate function as required, a simple example is shown below:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
// if message.payload.type == "INCOMING_CALL"
// LAUNCH APP with INCOMING CALL screen here
}
void main() {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
The problem is I'm unable to launch the app from the line LAUNCH APP with INCOMING CALL screen here
mentioned above. I've tried runApp(MaterialApp(...))
which doesn't launch the app, infact nothing happens.
Possible solutions?
In my research, I've come across this SO post - the only post that offers a solution of some sort solution can be found here (which offers an Android specific solution only).
Solution 1:
Use native code to override the onMessageRecieved in Android and call startActivity()
from this service. See here for more info.
Solution 2:
Using flutter_local_notifications, create a notification which when clicked/activated opens the app. The problem with this solution is it remains a notification.
I've already got a custom call screen which specific requirements, but in essence I need to show something like the above when a FCM message comes through informing the user of a call - I'm unable to do this from an Isolate function (as discussed above).
How can I achieve this?