使用 Agora 的 Flutter 来电 video/audio 来电通知

Flutter incoming video/audio call notification using Agora

我一直在开发一个应用程序,我需要在我的应用程序中实现音频和视频通话,我已经使用 Agora.io 但问题是我必须显示来电通知并不重要应用程序是在前台还是在后台。我已经尝试了很多东西,但我仍然无法配置它。我正在使用 agora_rtc_engine 套餐来拨打电话。

如有任何帮助,我们将不胜感激。

谢谢

我目前使用的代码:

调用方法

class CallMethods {
  final callRef = FirebaseFirestore.instance.collection('Calls');

  Stream<DocumentSnapshot> callstream({@required String id}) =>
      callRef.doc(id).snapshots();

  Future<bool> makeCall({@required Call call}) async {
    try {
      log('Making call');
      call.hasdialed = true;
      Map<String, dynamic> hasDialedMap = call.toMap(call);

      call.hasdialed = false;
      Map<String, dynamic> hasNotDialedMap = call.toMap(call);

      await callRef.doc(call.senderid).set(hasDialedMap);
      await callRef.doc(call.receiverid).set(hasNotDialedMap);

      return true;
    } catch (e) {
      print(e);
      return false;
    }
  }

  Future<bool> endCall({@required Call call}) async {
    try {
      log('ending call');
      await callRef.doc(call.senderid).delete();
      await callRef.doc(call.receiverid).delete();

      return true;
    } catch (e) {
      print(e);
      return false;
    }
  }
}

Call Utils: 用来打电话

class CallUtils {
  static final CallMethods callmethods = CallMethods();

  static dial(
    BuildContext context, {
    @required User from,
    @required var to,
  }) async {
    Call call = Call(
      senderid: from.id,
      // senderpic: from.avatar.url,
      callername: from.name,
      receiverid: to.id,
      // receiverpic: to.avatar.url,
      receivername: to.name,
      channelid: Random().nextInt(999999).toString(),
    );

    bool callmade = await callmethods.makeCall(call: call);
    call.hasdialed = true;

    if (callmade) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => VideoCallScreen(call: call),
        ),
      );
    }
  }
}

之后我有一个拾取布局,用于包裹所有屏幕以显示来电通知。

代答呼叫布局:

(user.value.id != null)
          ? StreamBuilder<DocumentSnapshot>(
              stream: callmethods.callstream(id: user.value.id),
              builder: (context, snapshot) {
                if (snapshot.hasData && snapshot.data.data() != null) {
                    Call call = Call.fromMap(snapshot.data.data());
                  if (!call.hasdialed) {
                    return PickupScreen(call: call);
                  } else {
                    return widget.scaffold;
                  }

                } else {
                  return widget.scaffold;
                }
              },
            )
          : widget.scaffold,

可以通过 firebase 推送通知和后端 API 服务完成。

发送方:

一旦拨打电话,您将 post 您的后端 api 服务与呼叫者和接收者 ID,并且您的后端服务进一步负责发送带有有效载荷的推送通知到接收器。

接收方:

当接收者收到推送通知时,您可以将其配置为自动打开您的应用程序并显示包含所有负载信息的屏幕。也许你可以给他看一个带有接受和拒绝按钮的屏幕,如果他接受,你可以把他连接到 Agora。

检查 负载配置。