|
| 1 | +import React, {useEffect, useState} from 'react'; |
| 2 | +import { |
| 3 | + View, |
| 4 | + Text, |
| 5 | + ToastAndroid, |
| 6 | + StyleSheet, |
| 7 | + TouchableHighlight, |
| 8 | + DeviceEventEmitter, |
| 9 | + Image, |
| 10 | +} from 'react-native'; |
| 11 | +import RNBootSplash from 'react-native-bootsplash'; |
| 12 | +import messaging from '@react-native-firebase/messaging'; |
| 13 | +import Clipboard from '@react-native-community/clipboard'; |
| 14 | +import IncomingCall from 'react-native-incoming-call'; |
| 15 | + |
| 16 | +export function handleRemoteMessage(remoteMessage) { |
| 17 | + if (remoteMessage?.notification?.title === 'Incoming call') { |
| 18 | + IncomingCall.display( |
| 19 | + '123', |
| 20 | + 'Quocs', |
| 21 | + 'https://avatars3.githubusercontent.com/u/16166195', |
| 22 | + ); |
| 23 | + // Could also persist data here for later uses |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const App = () => { |
| 28 | + const [deviceToken, setDeviceToken] = useState(null); |
| 29 | + const [callPayload, setCallPayload] = useState(null); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + RNBootSplash.hide({duration: 250}); |
| 33 | + |
| 34 | + // Get the device token |
| 35 | + messaging() |
| 36 | + .getToken() |
| 37 | + .then(token => { |
| 38 | + return setDeviceToken(token); |
| 39 | + }); |
| 40 | + |
| 41 | + // Listen to whether the token changes |
| 42 | + return messaging().onTokenRefresh(token => { |
| 43 | + setDeviceToken(token); |
| 44 | + }); |
| 45 | + }, []); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + const unsubscribe = messaging().onMessage(async remoteMessage => { |
| 49 | + handleRemoteMessage(remoteMessage); |
| 50 | + }); |
| 51 | + |
| 52 | + return unsubscribe; |
| 53 | + }, []); |
| 54 | + |
| 55 | + async function handleIncomingCall() { |
| 56 | + const launchPayload = await IncomingCall.getLaunchParameters(); |
| 57 | + console.log('launchParameters', launchPayload); |
| 58 | + IncomingCall.clearLaunchParameters(); |
| 59 | + if (launchPayload) { |
| 60 | + // Start call here |
| 61 | + setCallPayload(launchPayload); |
| 62 | + } |
| 63 | + |
| 64 | + DeviceEventEmitter.addListener('endCall', payload => { |
| 65 | + // End call action here |
| 66 | + setCallPayload(payload); |
| 67 | + }); |
| 68 | + DeviceEventEmitter.addListener('answerCall', payload => { |
| 69 | + // Start call action here |
| 70 | + setCallPayload(payload); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + handleIncomingCall(); |
| 76 | + }, []); |
| 77 | + |
| 78 | + function handleCopyToken() { |
| 79 | + Clipboard.setString(deviceToken); |
| 80 | + ToastAndroid.show('Device token copied!', 2000); |
| 81 | + } |
| 82 | + |
| 83 | + function handleClearPayload() { |
| 84 | + setCallPayload(null); |
| 85 | + } |
| 86 | + |
| 87 | + return ( |
| 88 | + <View style={styles.wrapper}> |
| 89 | + <Text style={styles.appTitle}>RNCall</Text> |
| 90 | + <Text style={styles.appDesc}>example for react-native-incoming-call</Text> |
| 91 | + <View style={styles.container}> |
| 92 | + {callPayload ? ( |
| 93 | + <> |
| 94 | + <Image |
| 95 | + style={styles.image} |
| 96 | + source={require('./images/incoming-call.jpg')} |
| 97 | + /> |
| 98 | + <Text style={styles.header}>Incoming Call Payload</Text> |
| 99 | + <Text style={styles.text}>{JSON.stringify(callPayload)}</Text> |
| 100 | + <TouchableHighlight |
| 101 | + onPress={handleClearPayload} |
| 102 | + style={styles.button} |
| 103 | + underlayColor="#C0B7FD"> |
| 104 | + <Text style={styles.btnLabel}>Clear</Text> |
| 105 | + </TouchableHighlight> |
| 106 | + </> |
| 107 | + ) : deviceToken ? ( |
| 108 | + <> |
| 109 | + <Image |
| 110 | + style={styles.image} |
| 111 | + source={require('./images/waiting-call.jpg')} |
| 112 | + /> |
| 113 | + <Text style={styles.header}>FCM Device Token</Text> |
| 114 | + <Text style={styles.text}>{deviceToken}</Text> |
| 115 | + <TouchableHighlight |
| 116 | + onPress={handleCopyToken} |
| 117 | + style={styles.button} |
| 118 | + underlayColor="#C0B7FD"> |
| 119 | + <Text style={styles.btnLabel}>Copy To Clipboard</Text> |
| 120 | + </TouchableHighlight> |
| 121 | + </> |
| 122 | + ) : ( |
| 123 | + <> |
| 124 | + <Image |
| 125 | + style={styles.image} |
| 126 | + source={require('./images/no-device-token.jpg')} |
| 127 | + /> |
| 128 | + <Text style={styles.text}>No Device Token Found!</Text> |
| 129 | + </> |
| 130 | + )} |
| 131 | + </View> |
| 132 | + </View> |
| 133 | + ); |
| 134 | +}; |
| 135 | + |
| 136 | +const styles = StyleSheet.create({ |
| 137 | + wrapper: { |
| 138 | + flex: 1, |
| 139 | + backgroundColor: '#8271FC', |
| 140 | + }, |
| 141 | + appTitle: { |
| 142 | + textAlign: 'center', |
| 143 | + marginTop: 40, |
| 144 | + fontSize: 24, |
| 145 | + color: '#FFFFFF', |
| 146 | + fontFamily: 'monospace', |
| 147 | + fontWeight: '700', |
| 148 | + }, |
| 149 | + appDesc: { |
| 150 | + textAlign: 'center', |
| 151 | + marginTop: 20, |
| 152 | + fontSize: 10, |
| 153 | + marginHorizontal: 20, |
| 154 | + color: '#FFFFFF', |
| 155 | + fontFamily: 'monospace', |
| 156 | + textTransform: 'uppercase', |
| 157 | + letterSpacing: 2, |
| 158 | + }, |
| 159 | + container: { |
| 160 | + marginTop: 40, |
| 161 | + paddingTop: 20, |
| 162 | + paddingHorizontal: 20, |
| 163 | + paddingBottom: 50, |
| 164 | + marginHorizontal: 20, |
| 165 | + borderRadius: 10, |
| 166 | + backgroundColor: '#FFFFFF', |
| 167 | + elevation: 5, |
| 168 | + }, |
| 169 | + header: { |
| 170 | + fontSize: 20, |
| 171 | + color: '#21346E', |
| 172 | + marginBottom: 10, |
| 173 | + fontFamily: 'monospace', |
| 174 | + fontWeight: '700', |
| 175 | + }, |
| 176 | + text: { |
| 177 | + fontSize: 14, |
| 178 | + color: '#7984A7', |
| 179 | + fontFamily: 'monospace', |
| 180 | + }, |
| 181 | + button: { |
| 182 | + position: 'absolute', |
| 183 | + height: 50, |
| 184 | + borderRadius: 10, |
| 185 | + paddingHorizontal: 20, |
| 186 | + bottom: -25, |
| 187 | + backgroundColor: '#FDDA97', |
| 188 | + justifyContent: 'center', |
| 189 | + alignItems: 'center', |
| 190 | + alignSelf: 'center', |
| 191 | + elevation: 5, |
| 192 | + }, |
| 193 | + btnLabel: { |
| 194 | + fontSize: 14, |
| 195 | + color: '#21346E', |
| 196 | + fontFamily: 'monospace', |
| 197 | + fontWeight: '700', |
| 198 | + }, |
| 199 | + image: { |
| 200 | + alignSelf: 'center', |
| 201 | + width: 300, |
| 202 | + height: 300, |
| 203 | + }, |
| 204 | +}); |
| 205 | + |
| 206 | +export default App; |
0 commit comments