flutter_callkit_incoming 1.0.1+4
flutter_callkit_incoming: ^1.0.1+4 copied to clipboard
Flutter Callkit Incoming to show callkit screen in your Flutter app.
example/lib/main.dart
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:uuid/uuid.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart';
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("Handling a background message: ${message.messageId}");
listenerEvent(null);
showCallkitIncoming('5c3ab1f0-8750-42eb-8d29-ad28e3d7f336');
}
Future<void> listenerEvent(Function? callback) async {
try {
FlutterCallkitIncoming.onEvent.listen((event) {
print(event);
switch (event!.name) {
case CallEvent.ACTION_CALL_INCOMING:
// TODO: received an incoming call
break;
case CallEvent.ACTION_CALL_START:
// TODO: started an outgoing call
// TODO: show screen calling in Flutter
break;
case CallEvent.ACTION_CALL_ACCEPT:
// TODO: accepted an incoming call
// TODO: show screen calling in Flutter
break;
case CallEvent.ACTION_CALL_DECLINE:
// TODO: declined an incoming call
break;
case CallEvent.ACTION_CALL_ENDED:
// TODO: ended an incoming/outgoing call
break;
case CallEvent.ACTION_CALL_TIMEOUT:
// TODO: missed an incoming call
break;
case CallEvent.ACTION_CALL_CALLBACK:
// TODO: only Android - click action `Call back` from missed call notification
break;
case CallEvent.ACTION_CALL_TOGGLE_HOLD:
// TODO: only iOS
break;
case CallEvent.ACTION_CALL_TOGGLE_MUTE:
// TODO: only iOS
break;
case CallEvent.ACTION_CALL_TOGGLE_DMTF:
// TODO: only iOS
break;
case CallEvent.ACTION_CALL_TOGGLE_GROUP:
// TODO: only iOS
break;
case CallEvent.ACTION_CALL_TOGGLE_AUDIO_SESSION:
// TODO: only iOS
break;
case CallEvent.ACTION_DID_UPDATE_DEVICE_PUSH_TOKEN_VOIP:
// TODO: only iOS
break;
}
if (callback != null) {
callback(event.toString());
}
});
} on Exception {}
}
Future<void> showCallkitIncoming(String uuid) async {
var params = <String, dynamic>{
'id': uuid,
'nameCaller': 'Hien Nguyen',
'appName': 'Callkit',
'avatar': 'https://i.pravatar.cc/100',
'handle': '0123456789',
'type': 0,
'duration': 30000,
'extra': <String, dynamic>{'userId': '1a2b3c4d'},
'headers': <String, dynamic>{'apiKey': 'Abc@123!', 'platform': 'flutter'},
'android': <String, dynamic>{
'isCustomNotification': true,
'isShowLogo': false,
'isShowCallback': false,
'ringtonePath': 'system_ringtone_default',
'backgroundColor': '#0955fa',
'background': 'https://i.pravatar.cc/500',
'actionColor': '#4CAF50'
},
'ios': <String, dynamic>{
'iconName': 'CallKitLogo',
'handleType': '',
'supportsVideo': true,
'maximumCallGroups': 2,
'maximumCallsPerCallGroup': 1,
'audioSessionMode': 'default',
'audioSessionActive': true,
'audioSessionPreferredSampleRate': 44100.0,
'audioSessionPreferredIOBufferDuration': 0.005,
'supportsDTMF': true,
'supportsHolding': true,
'supportsGrouping': false,
'supportsUngrouping': false,
'ringtonePath': 'system_ringtone_default'
}
};
await FlutterCallkitIncoming.showCallkitIncoming(params);
}
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _uuid = Uuid();
var _currentUuid;
var textEvents = "";
late final FirebaseMessaging _firebaseMessaging;
@override
void initState() {
super.initState();
listenerEvent(onEvent);
initCurrentCall();
initFirebase();
}
onEvent(event) {
if (!mounted) return;
setState(() {
textEvents += "${event.toString()}\n";
});
}
initCurrentCall() async {
//check current call from pushkit if possible
var calls = await FlutterCallkitIncoming.activeCalls();
if (calls is List && calls.isNotEmpty) {
this._currentUuid = calls[0]['id'];
}
}
initFirebase() async {
await Firebase.initializeApp();
_firebaseMessaging = FirebaseMessaging.instance;
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print(
'Message title: ${message.notification?.title}, body: ${message.notification?.body}, data: ${message.data}');
this._currentUuid = _uuid.v4();
showCallkitIncoming(this._currentUuid);
});
_firebaseMessaging.getToken().then((token) {
print('Device Token FCM: $token');
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.call,
color: Colors.white,
),
onPressed: () async {
this.makeFakeCallInComing();
},
),
IconButton(
icon: Icon(
Icons.call_end,
color: Colors.white,
),
onPressed: () async {
this.endCurrentCall();
},
),
IconButton(
icon: Icon(
Icons.call_made,
color: Colors.white,
),
onPressed: () async {
this.startOutGoingCall();
},
),
IconButton(
icon: Icon(
Icons.call_merge,
color: Colors.white,
),
onPressed: () async {
this.activeCalls();
},
),
IconButton(
icon: Icon(
Icons.clear_all_sharp,
color: Colors.white,
),
onPressed: () async {
this.endAllCalls();
},
)
],
),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints viewportConstraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: viewportConstraints.maxHeight,
),
child: Text('$textEvents'),
),
);
},
),
),
);
}
Future<void> makeFakeCallInComing() async {
await Future.delayed(const Duration(seconds: 10), () async {
this._currentUuid = _uuid.v4();
var params = <String, dynamic>{
'id': _currentUuid,
'nameCaller': 'Hien Nguyen',
'appName': 'Callkit',
'avatar': 'https://i.pravatar.cc/100',
'handle': '0123456789',
'type': 0,
'duration': 30000,
'extra': <String, dynamic>{'userId': '1a2b3c4d'},
'headers': <String, dynamic>{
'apiKey': 'Abc@123!',
'platform': 'flutter'
},
'android': <String, dynamic>{
'isCustomNotification': true,
'isShowLogo': false,
'isShowCallback': false,
'ringtonePath': 'system_ringtone_default',
'backgroundColor': '#0955fa',
'background': 'https://i.pravatar.cc/500',
'actionColor': '#4CAF50'
},
'ios': <String, dynamic>{
'iconName': 'CallKitLogo',
'handleType': '',
'supportsVideo': true,
'maximumCallGroups': 2,
'maximumCallsPerCallGroup': 1,
'audioSessionMode': 'default',
'audioSessionActive': true,
'audioSessionPreferredSampleRate': 44100.0,
'audioSessionPreferredIOBufferDuration': 0.005,
'supportsDTMF': true,
'supportsHolding': true,
'supportsGrouping': false,
'supportsUngrouping': false,
'ringtonePath': 'system_ringtone_default'
}
};
await FlutterCallkitIncoming.showCallkitIncoming(params);
});
}
Future<void> showMissCallNotification() async {
this._currentUuid = _uuid.v4();
var params = <String, dynamic>{
'id': this._currentUuid,
'nameCaller': 'Hien Nguyen',
'handle': '0123456789',
'type': 1,
'extra': <String, dynamic>{'userId': '1a2b3c4d'},
};
await FlutterCallkitIncoming.showMissCallNotification(params);
}
Future<void> endCurrentCall() async {
initCurrentCall();
var params = <String, dynamic>{'id': this._currentUuid};
await FlutterCallkitIncoming.endCall(params);
}
Future<void> startOutGoingCall() async {
this._currentUuid = _uuid.v4();
var params = <String, dynamic>{
'id': this._currentUuid,
'nameCaller': 'Hien Nguyen',
'handle': '0123456789',
'type': 1,
'extra': <String, dynamic>{'userId': '1a2b3c4d'},
'ios': <String, dynamic>{'handleType': 'number'}
}; //number/email
await FlutterCallkitIncoming.startCall(params);
}
Future<void> activeCalls() async {
var calls = await FlutterCallkitIncoming.activeCalls();
print(calls);
}
Future<void> endAllCalls() async {
await FlutterCallkitIncoming.endAllCalls();
}
Future<void> getDevicePushTokenVoIP() async {
var devicePushTokenVoIP =
await FlutterCallkitIncoming.getDevicePushTokenVoIP();
print(devicePushTokenVoIP);
}
}