|
| 1 | +import { PermissionsAndroid, Platform } from 'react-native'; |
| 2 | +import RNNotificationCall, { |
| 3 | + answerPayload, |
| 4 | + declinePayload, |
| 5 | +} from '../../../src/index'; |
| 6 | +import RNCallKeep from 'react-native-callkeep'; |
| 7 | +import { request, check, PERMISSIONS, RESULTS } from 'react-native-permissions'; |
| 8 | + |
| 9 | +const appName = 'Incoming-Test'; |
| 10 | +const isAndroid = Platform.OS === 'android'; |
| 11 | +const answerOption = { |
| 12 | + channelId: 'com.abc.incomingcall', |
| 13 | + channelName: 'Incoming video call', |
| 14 | + notificationIcon: 'ic_launcher', //mipmap |
| 15 | + notificationTitle: 'Linh Vo', |
| 16 | + answerText: 'Answer', |
| 17 | + declineText: 'Decline', |
| 18 | + notificationColor: 'colorAccent', //path color in android |
| 19 | + notificationSound: undefined, //raw |
| 20 | +}; |
| 21 | +// this service only focus for Android |
| 22 | + |
| 23 | +export class CallKeepService { |
| 24 | + private static _instance?: CallKeepService; |
| 25 | + static navigation: any; |
| 26 | + constructor() { |
| 27 | + //setup callkeep |
| 28 | + // this.setupCallKeep(); |
| 29 | + } |
| 30 | + static instance(): CallKeepService { |
| 31 | + if (!CallKeepService._instance) { |
| 32 | + CallKeepService._instance = new CallKeepService(); |
| 33 | + } |
| 34 | + return CallKeepService._instance; |
| 35 | + } |
| 36 | + |
| 37 | + async setupCallKeep() { |
| 38 | + await new Promise((resolve) => { |
| 39 | + console.log('setup call keep done in promise'); |
| 40 | + this.setupCallKeepFunc().then(resolve); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + async setupCallKeepFunc() { |
| 45 | + const granted = await request(PERMISSIONS.ANDROID.READ_PHONE_NUMBERS); |
| 46 | + if (granted !== RESULTS.GRANTED) return; |
| 47 | + //only setup when granted permission |
| 48 | + await this.setup(); |
| 49 | + //setup done |
| 50 | + if (isAndroid) { |
| 51 | + RNCallKeep.setAvailable(true); |
| 52 | + } |
| 53 | + this.registerEvent(); |
| 54 | + } |
| 55 | + async setup() { |
| 56 | + try { |
| 57 | + await RNCallKeep.setup({ |
| 58 | + ios: { |
| 59 | + appName: appName, |
| 60 | + maximumCallGroups: '1', |
| 61 | + maximumCallsPerCallGroup: '1', |
| 62 | + includesCallsInRecents: false, |
| 63 | + imageName: 'callkitIcon', //image name from ios |
| 64 | + }, |
| 65 | + android: { |
| 66 | + alertTitle: 'Permissions required', |
| 67 | + alertDescription: |
| 68 | + 'This application needs to access your phone accounts', |
| 69 | + cancelButton: 'Cancel', |
| 70 | + okButton: 'ok', |
| 71 | + selfManaged: true, |
| 72 | + additionalPermissions: [], |
| 73 | + foregroundService: { |
| 74 | + channelId: 'com.test.app.callkeep', |
| 75 | + channelName: 'Incoming Call', |
| 76 | + notificationTitle: 'Incoming Call', |
| 77 | + notificationIcon: 'ic_launcher_round', |
| 78 | + }, |
| 79 | + }, |
| 80 | + }); |
| 81 | + return { |
| 82 | + result: 'setupDone', |
| 83 | + }; |
| 84 | + } catch (error) { |
| 85 | + console.log('error setup callkeep', error); |
| 86 | + return error; |
| 87 | + } |
| 88 | + } |
| 89 | + registerEvent() { |
| 90 | + isAndroid && |
| 91 | + RNCallKeep.addEventListener( |
| 92 | + 'createIncomingConnectionFailed', |
| 93 | + this.onFailCallAction |
| 94 | + ); |
| 95 | + RNCallKeep.addEventListener('answerCall', this.onCallKeepAnswerCallAction); |
| 96 | + RNCallKeep.addEventListener('endCall', this.onCallKeepEndCallAction); |
| 97 | + if (isAndroid) { |
| 98 | + //event only on android |
| 99 | + RNCallKeep.addEventListener( |
| 100 | + 'showIncomingCallUi', |
| 101 | + ({ handle, callUUID, name }) => { |
| 102 | + RNNotificationCall.displayNotification(callUUID, null, 30000, { |
| 103 | + ...answerOption, |
| 104 | + channelId: 'com.abc.incomingcall', |
| 105 | + channelName: 'Incoming video call', |
| 106 | + notificationTitle: 'Linh Vo', |
| 107 | + notificationBody: 'Incoming video call', |
| 108 | + payload: { |
| 109 | + kiokas: 'ádada', |
| 110 | + ssskis: 'awq', |
| 111 | + }, |
| 112 | + }); |
| 113 | + } |
| 114 | + ); |
| 115 | + // Listen to headless action events |
| 116 | + RNNotificationCall.addEventListener('endCall', (data: declinePayload) => { |
| 117 | + const { callUUID } = data; |
| 118 | + // End call action here |
| 119 | + console.log('endCall', callUUID); |
| 120 | + RNCallKeep.endCall(callUUID); |
| 121 | + }); |
| 122 | + RNNotificationCall.addEventListener('answer', (data: answerPayload) => { |
| 123 | + const { callUUID } = data; |
| 124 | + //open app from quit state |
| 125 | + RNNotificationCall.backToApp(); |
| 126 | + //call api answer |
| 127 | + console.log('answer', callUUID); |
| 128 | + RNCallKeep.answerIncomingCall(callUUID); |
| 129 | + }); |
| 130 | + // You can listener firebase message event here |
| 131 | + } |
| 132 | + } |
| 133 | + onFailCallAction() { |
| 134 | + RNCallKeep.endAllCalls(); |
| 135 | + } |
| 136 | + |
| 137 | + //handle event |
| 138 | + onCallKeepAnswerCallAction(answerData: any) { |
| 139 | + const { callUUID } = answerData; |
| 140 | + // called when the user answer the incoming call |
| 141 | + //navigate to another screen |
| 142 | + //some project need to rehandle with redux state or other state manager refer https://github.com/linhvovan29546/react-native-full-screen-notification-incoming-call/issues/17#issuecomment-1318225574 |
| 143 | + CallKeepService.navigation.navigate('Detail'); |
| 144 | + } |
| 145 | + onCallKeepEndCallAction(answerData: any) { |
| 146 | + const { callUUID } = answerData; |
| 147 | + //end call action of callkit |
| 148 | + //action destroy screen |
| 149 | + //You need to call RNCallKeep.endCall(callUUID) to end call |
| 150 | + } |
| 151 | + |
| 152 | + async displayCall(uuid: string) { |
| 153 | + const granted = await check(PERMISSIONS.ANDROID.READ_PHONE_NUMBERS); |
| 154 | + //only display call when permission granted |
| 155 | + if (granted !== RESULTS.GRANTED) return; |
| 156 | + console.log('display call', uuid); |
| 157 | + RNCallKeep.displayIncomingCall( |
| 158 | + uuid, |
| 159 | + 'Linh Vo', |
| 160 | + 'Linh Vo', |
| 161 | + 'number', |
| 162 | + true, |
| 163 | + undefined |
| 164 | + ); |
| 165 | + } |
| 166 | +} |
0 commit comments