Skip to content

Commit

Permalink
Handle Late Notifications as missed calls
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacakakpo1 committed Nov 11, 2024
1 parent 8ed14b9 commit fbc084b
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .lh/README.md.json

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions .lh/lib/service/notification_service.dart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"sourceFile": "lib/service/notification_service.dart",
"activeCommit": 0,
"commits": [
{
"activePatchIndex": 0,
"patches": [
{
"date": 1731272573644,
"content": "Index: \n===================================================================\n--- \n+++ \n"
}
],
"date": 1731272573644,
"name": "Commit-0",
"content": "import 'dart:convert';\nimport 'package:firebase_messaging/firebase_messaging.dart';\nimport 'package:flutter/foundation.dart';\nimport 'package:flutter_callkit_incoming/entities/android_params.dart';\nimport 'package:flutter_callkit_incoming/entities/call_kit_params.dart';\nimport 'package:flutter_callkit_incoming/entities/ios_params.dart';\nimport 'package:flutter_callkit_incoming/entities/notification_params.dart';\nimport 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart';\nimport 'package:uuid/uuid.dart';\nimport 'package:logger/logger.dart';\nimport 'package:telnyx_webrtc/model/push_notification.dart';\n\n\nclass NotificationService {\n\n static Future showNotification(RemoteMessage message) async {\n var logger = Logger();\n print('Received Incoming NotificationService!');\n logger.i('Received Incoming NotificationService! from background');\n var metadata = PushMetaData.fromJson(jsonDecode(message.data[\"metadata\"]));\n var received = message.data[\"message\"];\n var currentUuid = const Uuid().v4();\n\n\n CallKitParams callKitParams = CallKitParams(\n id: currentUuid,\n nameCaller: metadata.caller_name,\n appName: 'Telnyx Flutter Voice',\n avatar: 'https://i.pravatar.cc/100',\n handle: metadata.caller_number,\n type: 0,\n textAccept: 'Accept',\n textDecline: 'Decline',\n missedCallNotification: const NotificationParams(\n showNotification: true,\n isShowCallback: true,\n subtitle: 'Missed call',\n callbackText: 'Call back',\n ),\n duration: 30000,\n extra: message.data,\n headers: <String, dynamic>{'platform': 'flutter'},\n android: const AndroidParams(\n isCustomNotification: true,\n isShowLogo: false,\n ringtonePath: 'system_ringtone_default',\n backgroundColor: '#0955fa',\n backgroundUrl: 'https://i.pravatar.cc/500',\n actionColor: '#4CAF50',\n textColor: '#ffffff',\n incomingCallNotificationChannelName: \"Incoming Call\",\n missedCallNotificationChannelName: \"Missed Call\"),\n ios: const IOSParams(\n iconName: 'CallKitLogo',\n handleType: 'generic',\n supportsVideo: true,\n maximumCallGroups: 2,\n maximumCallsPerCallGroup: 1,\n audioSessionMode: 'default',\n audioSessionActive: true,\n audioSessionPreferredSampleRate: 44100.0,\n audioSessionPreferredIOBufferDuration: 0.005,\n supportsDTMF: true,\n supportsHolding: true,\n supportsGrouping: false,\n supportsUngrouping: false,\n ringtonePath: 'system_ringtone_default',\n ),\n );\n\n\n await FlutterCallkitIncoming.showCallkitIncoming(callKitParams);\n }\n\n static Future showMissedCallNotification(RemoteMessage message) async {\n var logger = Logger();\n print('Received Incoming NotificationService!');\n logger.i('Received Incoming NotificationService! from background');\n var metadata = PushMetaData.fromJson(jsonDecode(message.data[\"metadata\"]));\n var received = message.data[\"message\"];\n var currentUuid = const Uuid().v4();\n\n\n CallKitParams callKitParams = CallKitParams(\n id: currentUuid,\n nameCaller: metadata.caller_name,\n appName: 'Telnyx Flutter Voice',\n avatar: 'https://i.pravatar.cc/100',\n handle: metadata.caller_number,\n type: 0,\n textAccept: 'Accept',\n textDecline: 'Decline',\n missedCallNotification: const NotificationParams(\n showNotification: true,\n isShowCallback: true,\n subtitle: 'Missed call',\n callbackText: 'Call back',\n ),\n duration: 30000,\n extra: message.data,\n headers: <String, dynamic>{'platform': 'flutter'},\n android: const AndroidParams(\n isCustomNotification: true,\n isShowLogo: false,\n ringtonePath: 'system_ringtone_default',\n backgroundColor: '#0955fa',\n backgroundUrl: 'https://i.pravatar.cc/500',\n actionColor: '#4CAF50',\n textColor: '#ffffff',\n incomingCallNotificationChannelName: \"Incoming Call\",\n missedCallNotificationChannelName: \"Missed Call\"),\n ios: const IOSParams(\n iconName: 'CallKitLogo',\n handleType: 'generic',\n supportsVideo: true,\n maximumCallGroups: 2,\n maximumCallsPerCallGroup: 1,\n audioSessionMode: 'default',\n audioSessionActive: true,\n audioSessionPreferredSampleRate: 44100.0,\n audioSessionPreferredIOBufferDuration: 0.005,\n supportsDTMF: true,\n supportsHolding: true,\n supportsGrouping: false,\n supportsUngrouping: false,\n ringtonePath: 'system_ringtone_default',\n ),\n );\n\n\n await FlutterCallkitIncoming.showMissCallNotification(callKitParams);\n }\n}\n"
}
]
}
6 changes: 5 additions & 1 deletion .lh/lib/view/screen/home_screen.dart.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"activeCommit": 0,
"commits": [
{
"activePatchIndex": 1,
"activePatchIndex": 2,
"patches": [
{
"date": 1730906592729,
Expand All @@ -12,6 +12,10 @@
{
"date": 1730980825909,
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -22,9 +22,9 @@\n \n @override\n void initState() {\n super.initState();\n- destinationController.text = 'isaac33882';\n+ destinationController.text = 'isaac33882destination_number_here';\n }\n \n void _observeResponses() {\n Provider.of<MainViewModel>(context, listen: true).observeResponses();\n"
},
{
"date": 1731267138541,
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -22,9 +22,9 @@\n \n @override\n void initState() {\n super.initState();\n- destinationController.text = 'isaac33882destination_number_here';\n+ destinationController.text = 'isaac33882';\n }\n \n void _observeResponses() {\n Provider.of<MainViewModel>(context, listen: true).observeResponses();\n"
}
],
"date": 1730906592729,
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,23 @@ In order to end a call, we can get a stored instance of Call and call the .endCa
}
```

### Handling Late Notifications
If notifcations arrive very late due to no internet connectivity, It is good to always flag it as a missed call. You can do that using the
code snippet below :

```dart
const CALL_MISSED_TIMEOUT = 60;
DateTime nowTime = DateTime.now();
Duration? difference = nowTime?.difference(message.sentTime!);
if (difference.inSeconds > CALL_MISSED_TIMEOUT) {
NotificationService.showMissedCallNotification(message);
return;
}
```


### DTMF (Dual Tone Multi Frequency)

In order to send a DTMF message while on a call you can call the .dtmf(callID, tone), method where tone is a String value of the character you would like pressed:
Expand Down
15 changes: 15 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ class _MyAppState extends State<MyApp> {
// Android Only - Push Notifications
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
logger.i('OnMessage :: Notification Message: ${message.data}');
DateTime nowTime = DateTime.now();
Duration? difference = nowTime?.difference(message.sentTime!);

if (difference != null) {
logger.i(
'OnMessage :: Notification difference: ${difference.inSeconds}');
if (difference.inSeconds > CALL_MISSED_TIMEOUT) {
logger.i('OnMessage :: Notification Message: Missed Call');
// You can simulate a missed call here
NotificationService.showMissedCallNotification(message);
return;
}
}

logger.i('OnMessage Time :: Notification Message: ${message.sentTime}');
TelnyxClient.setPushMetaData(message.data);
NotificationService.showNotification(message);
mainViewModel.callFromPush = true;
Expand Down
61 changes: 57 additions & 4 deletions lib/service/notification_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import 'package:uuid/uuid.dart';
import 'package:logger/logger.dart';
import 'package:telnyx_webrtc/model/push_notification.dart';


class NotificationService {

static Future showNotification(RemoteMessage message) async {
var logger = Logger();
print('Received Incoming NotificationService!');
Expand All @@ -21,7 +19,6 @@ class NotificationService {
var received = message.data["message"];
var currentUuid = const Uuid().v4();


CallKitParams callKitParams = CallKitParams(
id: currentUuid,
nameCaller: metadata.caller_name,
Expand Down Expand Up @@ -68,7 +65,63 @@ class NotificationService {
),
);


await FlutterCallkitIncoming.showCallkitIncoming(callKitParams);
}

static Future showMissedCallNotification(RemoteMessage message) async {
var logger = Logger();
print('Received Incoming NotificationService!');
logger.i('Received Incoming NotificationService! from background');
var metadata = PushMetaData.fromJson(jsonDecode(message.data["metadata"]));
var received = message.data["message"];
var currentUuid = const Uuid().v4();

CallKitParams callKitParams = CallKitParams(
id: currentUuid,
nameCaller: metadata.caller_name,
appName: 'Telnyx Flutter Voice',
avatar: 'https://i.pravatar.cc/100',
handle: metadata.caller_number,
type: 0,
textAccept: 'Accept',
textDecline: 'Decline',
missedCallNotification: const NotificationParams(
showNotification: true,
isShowCallback: true,
subtitle: 'Missed call',
callbackText: 'Call back',
),
duration: 30000,
extra: message.data,
headers: <String, dynamic>{'platform': 'flutter'},
android: const AndroidParams(
isCustomNotification: true,
isShowLogo: false,
ringtonePath: 'system_ringtone_default',
backgroundColor: '#0955fa',
backgroundUrl: 'https://i.pravatar.cc/500',
actionColor: '#4CAF50',
textColor: '#ffffff',
incomingCallNotificationChannelName: "Incoming Call",
missedCallNotificationChannelName: "Missed Call"),
ios: const IOSParams(
iconName: 'CallKitLogo',
handleType: 'generic',
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.showMissCallNotification(callKitParams);
}
}
2 changes: 1 addition & 1 deletion lib/view/screen/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class _HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
destinationController.text = 'isaac33882destination_number_here';
destinationController.text = 'isaac33882';
}

void _observeResponses() {
Expand Down

0 comments on commit fbc084b

Please sign in to comment.