Created
November 12, 2024 11:19
-
-
Save adityagokula2210/adade788dfb4af178e81e387a49e1ef7 to your computer and use it in GitHub Desktop.
APNS Service for handling notifications in iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:convert'; | |
| import 'package:cometchat_calls_uikit/cometchat_calls_uikit.dart'; | |
| import 'package:cometchat_chat_uikit/cometchat_chat_uikit.dart'; | |
| import 'package:firebase_messaging/firebase_messaging.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:flutter/services.dart'; | |
| import 'package:flutter_apns_x/flutter_apns/src/apns_connector.dart'; | |
| import 'package:flutter_callkit_incoming/entities/entities.dart'; | |
| import 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart'; | |
| import 'package:voip_flutter/payload_data.dart'; | |
| import 'app_constants.dart'; | |
| import 'call_action.dart'; | |
| import 'call_type.dart'; | |
| class APNSService with CometChatCallsEventsListener { | |
| String? id; | |
| @override | |
| void onCallEndButtonPressed() async { | |
| debugPrint("onCallEndButtonPressed Clicked"); | |
| await FlutterCallkitIncoming.endCall(id!); | |
| } | |
| Future<void> displayIncomingCall(rMessage) async { | |
| PayloadData payloadData = PayloadData.fromJson(rMessage.data); | |
| String messageCategory = payloadData.type ?? ""; | |
| if (messageCategory == 'call') { | |
| CallAction callAction = payloadData.callAction ?? CallAction.none; | |
| String uuid = payloadData.sessionId ?? ""; | |
| final callUUID = uuid; | |
| String callerName = payloadData.senderName ?? ""; | |
| CallType callType = payloadData.callType ?? CallType.none; | |
| if (callAction == CallAction.initiated) { | |
| CallKitParams callKitParams = CallKitParams( | |
| id: callUUID, | |
| nameCaller: callerName, | |
| appName: 'notification_new', | |
| type: (callType == CallType.audio) ? 0 : 1, | |
| textAccept: 'Accept', | |
| textDecline: 'Decline', | |
| duration: 55000, | |
| ios: const IOSParams( | |
| supportsVideo: true, | |
| audioSessionMode: 'default', | |
| audioSessionActive: true, | |
| audioSessionPreferredSampleRate: 44100.0, | |
| audioSessionPreferredIOBufferDuration: 0.005, | |
| ringtonePath: 'system_ringtone_default', | |
| ), | |
| ); | |
| await FlutterCallkitIncoming.showCallkitIncoming(callKitParams); | |
| } | |
| } | |
| } | |
| init(BuildContext context) { | |
| final connector = ApnsPushConnector(); | |
| connector.shouldPresent = (x) => Future.value(false); | |
| connector.configure( | |
| onLaunch: (message) async { | |
| debugPrint('onLaunch: ${message.toString()}'); | |
| openNotification(message, context); | |
| }, | |
| onResume: (message) async { | |
| openNotification(message, context); | |
| }, | |
| ); | |
| connector.requestNotificationPermissions(); | |
| connector.token.addListener(() { | |
| if (connector.token.value != null || connector.token.value != '') { | |
| CometChatNotifications.registerPushToken( | |
| PushPlatforms.APNS_FLUTTER_DEVICE, | |
| providerId: AppConstants.apnsProviderId, | |
| deviceToken: connector.token.value, | |
| onSuccess: (response) { | |
| debugPrint( | |
| "${connector.token.value} DEVICE TOKEN - registerPushToken:success ${response.toString()}"); | |
| }, | |
| onError: (e) { | |
| debugPrint( | |
| "DEVICE TOKEN - registerPushToken:error ${e.toString()}"); | |
| }, | |
| ); | |
| } | |
| }); | |
| // Push Token VoIP | |
| FlutterCallkitIncoming.getDevicePushTokenVoIP().then( | |
| (voipToken) { | |
| if (voipToken != null || voipToken.toString().isNotEmpty) { | |
| CometChatNotifications.registerPushToken( | |
| PushPlatforms.APNS_FLUTTER_VOIP, | |
| providerId: AppConstants.apnsProviderId, | |
| voipToken: voipToken, | |
| onSuccess: (response) { | |
| debugPrint( | |
| "VOIP TOKEN - registerPushToken:success${response.toString()}"); | |
| }, | |
| onError: (e) { | |
| debugPrint( | |
| "VOIP TOKEN - registerPushToken:error ${e.toString()}"); | |
| }, | |
| ); | |
| } | |
| }, | |
| ); | |
| // Call event listeners | |
| FlutterCallkitIncoming.onEvent.listen( | |
| (CallEvent? callEvent) async { | |
| final Map<String, dynamic> body = callEvent?.body; | |
| PayloadData payloadData = PayloadData(); | |
| if (body['extra']['message'] != null) { | |
| payloadData = | |
| PayloadData.fromJson(jsonDecode(body['extra']['message'])); | |
| } | |
| String sessionId = payloadData.sessionId ?? ''; | |
| id = sessionId; | |
| switch (callEvent?.event) { | |
| case Event.actionCallIncoming: | |
| break; | |
| case Event.actionCallAccept: | |
| UIKitSettings uiKitSettings = (UIKitSettingsBuilder() | |
| ..subscriptionType = CometChatSubscriptionType.allUsers | |
| ..region = AppConstants.region | |
| ..autoEstablishSocketConnection = true | |
| ..appId = AppConstants.appId | |
| ..authKey = AppConstants.authKey | |
| ..extensions = | |
| CometChatUIKitChatExtensions.getDefaultExtensions() | |
| ..callingExtension = CometChatCallingExtension()) | |
| .build(); | |
| CometChatUIKit.init( | |
| uiKitSettings: uiKitSettings, | |
| onSuccess: (String successMessage) async { | |
| debugPrint("Cometchat ui kit Initialization success"); | |
| final user = await CometChat.getLoggedInUser(); | |
| if (user != null) { | |
| CallSettingsBuilder callSettingsBuilder = (CallSettingsBuilder() | |
| ..enableDefaultLayout = true | |
| ..setAudioOnlyCall = payloadData.callType == CallType.audio | |
| ); | |
| CometChatUIKitCalls.acceptCall(sessionId, onSuccess: (Call call) async { | |
| call.category = MessageCategoryConstants.call; | |
| CometChatCallEvents.ccCallAccepted(call); | |
| await FlutterCallkitIncoming.setCallConnected(sessionId); | |
| if (context.mounted) { | |
| Navigator.push( | |
| context, | |
| MaterialPageRoute( | |
| builder: (context) => CometChatOngoingCall( | |
| callSettingsBuilder: callSettingsBuilder, | |
| sessionId: sessionId, | |
| callWorkFlow: CallWorkFlow.defaultCalling, | |
| ), | |
| ), | |
| ).then((_) async { | |
| await const MethodChannel('com.cometchat.flutter_pn').invokeMethod('endCall'); | |
| }); | |
| } | |
| }, onError: (CometChatException e) { | |
| debugPrint("Error: acceptCall: ${e.message}"); | |
| }); | |
| } | |
| }, | |
| onError: (CometChatException e) { | |
| debugPrint("Initialization failed with exception: ${e.message}"); | |
| }); | |
| break; | |
| case Event.actionCallDecline: | |
| CometChatUIKitCalls.rejectCall(sessionId, CallStatusConstants.rejected, onSuccess: (Call call) async { | |
| call.category = MessageCategoryConstants.call; | |
| CometChatCallEvents.ccCallRejected(call); | |
| await FlutterCallkitIncoming.endCall(sessionId); | |
| }, onError: (e) { | |
| debugPrint("Unable to end call from incoming call screen${e.message}"); | |
| }); | |
| break; | |
| case Event.actionCallEnded: | |
| CometChat.endCall( | |
| sessionId, | |
| onSuccess: (call) { | |
| CometChat.clearActiveCall(); | |
| CometChatCalls.endSession(onSuccess: (onSuccess) { | |
| CometChatCallEvents.ccCallEnded(call); | |
| debugPrint("END CALl"); | |
| }, | |
| onError: (e) { | |
| debugPrint("CALl NOT ENDED $e"); | |
| }, | |
| ); | |
| }, | |
| onError: (e) { | |
| debugPrint("$e"); | |
| }, | |
| ); | |
| break; | |
| default: | |
| break; | |
| } | |
| }, | |
| cancelOnError: false, | |
| onDone: () { | |
| debugPrint('FlutterCallkitIncoming.onEvent: done'); | |
| }, | |
| onError: (e) { | |
| debugPrint('FlutterCallkitIncoming.onEvent:error ${e.toString()}'); | |
| }, | |
| ); | |
| } | |
| Future<void> openNotification(RemoteMessage? message, BuildContext context) async { | |
| if (message != null) { | |
| PayloadData payloadData = PayloadData.fromJson(message.data); | |
| if (payloadData.type == "call") { | |
| displayIncomingCall(message); | |
| } else { | |
| final receiverType = payloadData.receiverType ?? ""; | |
| User? sendUser; | |
| Group? sendGroup; | |
| String messageCategory = payloadData.type ?? ""; | |
| if (receiverType == CometChatReceiverType.user) { | |
| final uid = payloadData.sender ?? ""; | |
| await CometChat.getUser( | |
| uid, | |
| onSuccess: (user) { | |
| debugPrint("Got User App Background $user"); | |
| sendUser = user; | |
| }, | |
| onError: (excep) { | |
| debugPrint(excep.message); | |
| }, | |
| ); | |
| } else if (receiverType == CometChatReceiverType.group) { | |
| final guid = payloadData.receiver ?? ""; | |
| await CometChat.getGroup( | |
| guid, | |
| onSuccess: (group) { | |
| sendGroup = group; | |
| }, | |
| onError: (e) { | |
| debugPrint(e.message); | |
| }, | |
| ); | |
| } | |
| if (messageCategory == "chat" && (receiverType == CometChatReceiverType.user && sendUser != null) || (receiverType == CometChatReceiverType.group && sendGroup != null)) { | |
| if (context.mounted) { | |
| Future.delayed(const Duration(seconds: 2), () { | |
| Navigator.of(context).push( | |
| MaterialPageRoute( | |
| builder: (context) => CometChatMessages( | |
| user: sendUser, | |
| group: sendGroup, | |
| ), | |
| ), | |
| ); | |
| }); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment