Flutter open app after receiving fcm onBackgroundMessage
Asked Answered
H

2

6

I have integrated FCM with flutter and when I send data message from server its working fine. My question is how can I use onBackgroundMessage to open the app or specific screen. Actually I'm building video calling app using agora and I want to show incoming call screen like whatsapp using fcm if app is in background or closed. Right now I'm only printing the data. I have tried various methods but non of them are working. I have done this work on android because in android I can get the application context anywhere in-app. The issue is I'm not able to get the context in onBackgroundMessageHandler function.

Below is the code for showing the incoming call screen on foreground.

 _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print('on message $message');
        
        if(message['data']['purpose'].toString().contains("video")){
          Navigator.popAndPushNamed(context, IncomingCall.routeName, arguments: RouteIncoming(
            int.parse(message['data']['clientid']), message['data']['token'], message['data']['username'] 
          ));
        
        Provider.of<CallStatus>(context).setStatus(-1);
        Provider.of<CallStatus>(context).setIncoming(-1);
        
        }
        else if(message['data']['purpose'].toString().contains("reject")) {
          Provider.of<CallStatus>(context).setStatus(0);

        }
        else if(message['data']['purpose'].toString().contains("cancel")){
          Provider.of<CallStatus>(context).setIncoming(0);
        }
        //_showMyDialog(message);
        
      },
      onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler ,
      onResume: (Map<String, dynamic> message) async {
        print('on resume $message');
        print(message['data']['token']);
        Navigator.pushNamed(context, IncomingCall.routeName, arguments: RouteIncoming(
            int.parse(message['data']['clientid']), message['data']['token'], message['data']['username'] 
          ));
      },
      onLaunch: (Map<String, dynamic> message) async {
        print('on launch $message');
        Navigator.popAndPushNamed(context, IncomingCall.routeName, arguments: RouteIncoming(
            int.parse(message['data']['clientid']), message['data']['token'], message['data']['username'] 
          ));
      },
    );

Here is the code for myBackgroundMessageHandler

Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
  print("onBackgroundMessage: $message");
  
}

My PHP code for sending the FCM data message

    $clientid = $this->input->post('clientid');
        $userdata = $this->signin_model->send_token_m($clientid);
        $url = 'https://fcm.googleapis.com/fcm/send';

        $fields = array (
                    'registration_ids' => array (
                                    "$userdata->firebase_token"
                    ),
                    'data' => array (
                                    "purpose" => "video",
                                    "clientid" => $clientid,
                    )
    );
    $fields = json_encode ( $fields );

    $headers = array (
                    'Authorization: key=' . "......",
                    'Content-Type: application/json'
    );

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    echo $result;

I tried every link including this https://github.com/FirebaseExtended/flutterfire/issues/2387

Halfdan answered 28/12, 2020 at 8:22 Comment(1)
Did you find a solution? The video below didn't help. Thanks.Hamlet
G
2

Call the native code using flutter 'methodChannel' function inside onBackgroundMessage.

Gimcrack answered 15/3, 2021 at 12:11 Comment(1)
@CybeX have a look at time-sensitive notification concept of android. It might help youGimcrack
L
-1

The plugins available right now didn't work properly for me when I was trying to implement background and foreground notifications. So I ended up writing native code for that part. You can watch this for more reference : https://www.youtube.com/watch?v=NXuAzXY_KOo

Luciusluck answered 8/1, 2021 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.