flutter_callkit_incoming.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. import 'entities/entities.dart';
  4. /// Instance to use library functions.
  5. /// * showCallkitIncoming(dynamic)
  6. /// * startCall(dynamic)
  7. /// * endCall(dynamic)
  8. /// * endAllCalls()
  9. /// * callConnected(dynamic)
  10. class FlutterCallkitIncoming {
  11. static const MethodChannel _channel =
  12. MethodChannel('flutter_callkit_incoming');
  13. static const EventChannel _eventChannel =
  14. EventChannel('flutter_callkit_incoming_events');
  15. /// Listen to event callback from [FlutterCallkitIncoming].
  16. ///
  17. /// FlutterCallkitIncoming.onEvent.listen((event) {
  18. /// Event.ACTION_CALL_INCOMING - Received an incoming call
  19. /// Event.ACTION_CALL_START - Started an outgoing call
  20. /// Event.ACTION_CALL_ACCEPT - Accepted an incoming call
  21. /// Event.ACTION_CALL_DECLINE - Declined an incoming call
  22. /// Event.ACTION_CALL_ENDED - Ended an incoming/outgoing call
  23. /// Event.ACTION_CALL_TIMEOUT - Missed an incoming call
  24. /// Event.ACTION_CALL_CALLBACK - only Android (click action `Call back` from missed call notification)
  25. /// Event.ACTION_CALL_TOGGLE_HOLD - only iOS
  26. /// Event.ACTION_CALL_TOGGLE_MUTE - only iOS
  27. /// Event.ACTION_CALL_TOGGLE_DMTF - only iOS
  28. /// Event.ACTION_CALL_TOGGLE_GROUP - only iOS
  29. /// Event.ACTION_CALL_TOGGLE_AUDIO_SESSION - only iOS
  30. /// Event.DID_UPDATE_DEVICE_PUSH_TOKEN_VOIP - only iOS
  31. /// }
  32. static Stream<CallEvent?> get onEvent =>
  33. _eventChannel.receiveBroadcastStream().map(_receiveCallEvent);
  34. /// Show Callkit Incoming.
  35. /// On iOS, using Callkit. On Android, using a custom UI.
  36. static Future showCallkitIncoming(CallKitParams params) async {
  37. await _channel.invokeMethod("showCallkitIncoming", params.toJson());
  38. }
  39. /// Show Miss Call Notification.
  40. /// Only Android
  41. static Future showMissCallNotification(CallKitParams params) async {
  42. await _channel.invokeMethod("showMissCallNotification", params.toJson());
  43. }
  44. /// Hide notification call for Android.
  45. /// Only Android
  46. static Future hideCallkitIncoming(CallKitParams params) async {
  47. await _channel.invokeMethod("hideCallkitIncoming", params.toJson());
  48. }
  49. /// Start an Outgoing call.
  50. /// On iOS, using Callkit(create a history into the Phone app).
  51. /// On Android, Nothing(only callback event listener).
  52. static Future startCall(CallKitParams params) async {
  53. await _channel.invokeMethod("startCall", params.toJson());
  54. }
  55. /// Muting an Ongoing call.
  56. /// On iOS, using Callkit(update the ongoing call ui).
  57. /// On Android, Nothing(only callback event listener).
  58. static Future muteCall(String id, {bool isMuted = true}) async {
  59. await _channel.invokeMethod("muteCall", {'id': id, 'isMuted': isMuted});
  60. }
  61. /// Get Callkit Mic Status (muted/unmuted).
  62. /// On iOS, using Callkit(update call ui).
  63. /// On Android, Nothing(only callback event listener).
  64. static Future<bool> isMuted(String id) async {
  65. return (await _channel.invokeMethod("isMuted", {'id': id})) as bool? ??
  66. false;
  67. }
  68. /// Hold an Ongoing call.
  69. /// On iOS, using Callkit(update the ongoing call ui).
  70. /// On Android, Nothing(only callback event listener).
  71. static Future holdCall(String id, {bool isOnHold = true}) async {
  72. await _channel.invokeMethod("holdCall", {'id': id, 'isOnHold': isOnHold});
  73. }
  74. /// End an Incoming/Outgoing call.
  75. /// On iOS, using Callkit(update a history into the Phone app).
  76. /// On Android, Nothing(only callback event listener).
  77. static Future endCall(String id) async {
  78. await _channel.invokeMethod("endCall", {'id': id});
  79. }
  80. /// Set call has been connected successfully.
  81. /// On iOS, using Callkit(update a history into the Phone app).
  82. /// On Android, Nothing(only callback event listener).
  83. static Future setCallConnected(String id) async {
  84. await _channel.invokeMethod("callConnected", {'id': id});
  85. }
  86. /// End all calls.
  87. static Future endAllCalls() async {
  88. await _channel.invokeMethod("endAllCalls");
  89. }
  90. /// Get active calls.
  91. /// On iOS: return active calls from Callkit.
  92. /// On Android: only return last call
  93. static Future<dynamic> activeCalls() async {
  94. return await _channel.invokeMethod("activeCalls");
  95. }
  96. /// Get device push token VoIP.
  97. /// On iOS: return deviceToken for VoIP.
  98. /// On Android: return Empty
  99. static Future getDevicePushTokenVoIP() async {
  100. return await _channel.invokeMethod("getDevicePushTokenVoIP");
  101. }
  102. /// Silence CallKit events
  103. static Future silenceEvents() async {
  104. return await _channel.invokeMethod("silenceEvents", true);
  105. }
  106. /// Unsilence CallKit events
  107. static Future unsilenceEvents() async {
  108. return await _channel.invokeMethod("silenceEvents", false);
  109. }
  110. /// Request permisstion show notification for Android(13)
  111. /// Only Android: show request permission post notification for Android 13+
  112. static Future requestNotificationPermission(dynamic data) async {
  113. return await _channel.invokeMethod("requestNotificationPermission", data);
  114. }
  115. /// Request permisstion show notification for Android(14)+
  116. /// Only Android: show request permission for ACTION_MANAGE_APP_USE_FULL_SCREEN_INTENT
  117. static Future requestFullIntentPermission() async {
  118. return await _channel.invokeMethod("requestFullIntentPermission");
  119. }
  120. static CallEvent? _receiveCallEvent(dynamic data) {
  121. Event? event;
  122. Map<String, dynamic> body = {};
  123. if (data is Map) {
  124. event = Event.values.firstWhere((e) => e.name == data['event']);
  125. body = Map<String, dynamic>.from(data['body']);
  126. return CallEvent(body, event);
  127. }
  128. return null;
  129. }
  130. }