收到 fcm onBackgroundMessage 后 Flutter 打开应用

时间:2020-12-28 08:22:51

标签: flutter firebase-cloud-messaging whatsapp agora.io

我已经将 FCM 与 Flutter 集成在一起,当我从服务器发送数据消息时,它工作正常。我的问题是如何使用 onBackgroundMessage 打开应用程序或特定屏幕。实际上,我正在使用 agora 构建视频通话应用程序,如果应用程序处于后台或关闭状态,我想使用 fcm 显示类似 whatsapp 的来电屏幕。现在我只打印数据。我尝试了各种方法,但都没有奏效。我已经在 android 上完成了这项工作,因为在 android 中我可以在应用程序中的任何地方获取应用程序上下文。问题是我无法在 onBackgroundMessageHandler 函数中获取上下文。

下面是在前台显示来电屏幕的代码。

 _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'] 
          ));
      },
    );

这是 myBackgroundMessageHandler 的代码

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

我的用于发送 FCM 数据消息的 PHP 代码

    $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;

我尝试了所有链接,包括这个https://github.com/FirebaseExtended/flutterfire/issues/2387

2 个答案:

答案 0 :(得分:0)

当我尝试实现后台和前台通知时,现在可用的插件无法正常工作。所以我最终为那部分编写了本机代码。您可以观看此内容以获取更多参考:https://www.youtube.com/watch?v=NXuAzXY_KOo

答案 1 :(得分:0)

在 onBackgroundMessage 中使用 flutter 'methodChannel' 函数调用本机代码。