|
1 | | -# react-native-incoming-call |
| 1 | +# RN Incoming Call Version 2 |
2 | 2 |
|
3 | 3 | > React Native module to display custom incoming call activity, best result when using with firebase background messaging. Only for Android since iOS we have VoIP. |
4 | 4 |
|
5 | 5 | Yes I heard you could use **self managed ConnectionService** thing. But since I'm not an Android expert, this is a solution I found acceptable. |
6 | 6 |
|
7 | 7 | You could also wait for [this feature request](https://github.com/react-native-webrtc/react-native-callkeep/issues/43) from `react-native-callkeep` to be resolved and have an easier life. |
8 | 8 |
|
| 9 | +## Version 2 Breaking Changes |
| 10 | + |
| 11 | +Hello there! It's been a while since I first public version 1 of this library, which contains some bugs that I don't have much time to fix. |
| 12 | + |
| 13 | +Luckily I got a client project which needs this feature again and now I have time to improve it and make sure all major features work. So here is most of it I guess: |
| 14 | + |
| 15 | +[x] More generic incoming call UI. |
| 16 | + |
| 17 | +[x] Work nicely with all application state (foreground, background, killed, locked). |
| 18 | + |
| 19 | +[x] More flexible APIs. |
| 20 | + |
| 21 | +*Thanks to [jpudysz](https://github.com/jpudysz/react-native-callkeep)'s folk of react-native-callkeep, version 2 is heavily depended on it.* |
| 22 | + |
| 23 | +### Migrate from v1 |
| 24 | + |
| 25 | +- `getLaunchParameters()` & `clearLaunchParameters()` is now replaced by `openAppFromHeadlessMode()` & `getExtrasFromHeadlessMode()`. |
| 26 | + |
| 27 | +- Answer calls from background / killed state no longer open app and send launchParams, you need to listen to `answerCall` event from headless job and trigger `backToForeground` or `openAppFromHeadlessMode` manually. |
| 28 | + |
9 | 29 | ## Getting started |
10 | 30 |
|
11 | 31 | `$ npm install react-native-incoming-call --save` |
|
16 | 36 |
|
17 | 37 | ### Addition installation step |
18 | 38 |
|
19 | | -In `AndroidManifest.xml`, add `<activity android:name="com.incomingcall.UnlockScreenActivity" />` line between `<application>` tag. |
| 39 | +In `AndroidManifest.xml`: |
| 40 | + |
| 41 | +- Add `<activity android:name="com.incomingcall.UnlockScreenActivity" />` line between `<application>` tag. |
| 42 | + |
| 43 | +- Add `<uses-permission android:name="android.permission.VIBRATE" />` permission. |
| 44 | + |
| 45 | +- Also, it's recommend to put `android:launchMode="singleInstance"` in `<activity android:name=".MainActivity"...` tag to prevent duplicate activities. |
20 | 46 |
|
21 | 47 | For RN >= 0.60, it's done. Otherwise: |
22 | 48 |
|
23 | 49 | `$ react-native link react-native-incoming-call` |
24 | 50 |
|
25 | 51 | ## Usage |
| 52 | + |
| 53 | +In `App.js`: |
| 54 | + |
26 | 55 | ```javascript |
27 | 56 | import {useEffect} from 'react'; |
| 57 | +import {DeviceEventEmitter, Platform} from 'react-native'; |
28 | 58 | import IncomingCall from 'react-native-incoming-call'; |
29 | 59 |
|
30 | | -// Display incoming call activity. Should be called in backgroundHandler function of react-native-firebase. |
31 | | -IncomingCall.display(uuid, name, avatar); |
32 | | - |
33 | | -// Dismiss current activity. Should be called when call ended. |
34 | | -IncomingCall.dismiss(); |
35 | | - |
36 | 60 | // Listen to cancel and answer call events |
37 | 61 | useEffect(() => { |
38 | 62 | if (Platform.OS === "android") { |
39 | 63 | /** |
40 | | - * App in background or killed state, if user press answer button |
41 | | - * IncomingCall open app and put payload to getLaunchParameters |
42 | | - * You could start the call action here. |
43 | | - * End call action in this case not supported yet. |
| 64 | + * App open from killed state (headless mode) |
44 | 65 | */ |
45 | | - const payload = await IncomingCall.getLaunchParameters(); |
| 66 | + const payload = await IncomingCall.getExtrasFromHeadlessMode(); |
46 | 67 | console.log('launchParameters', payload); |
47 | | - IncomingCall.clearLaunchParameters(); |
48 | 68 | if (payload) { |
49 | | - // Start call here |
| 69 | + // Start call action here. You probably want to navigate to some CallRoom screen with the payload.uuid. |
50 | 70 | } |
51 | 71 |
|
52 | 72 | /** |
53 | | - * App in foreground: listen to call events and determine what to do next |
| 73 | + * App in foreground / background: listen to call events and determine what to do next |
54 | 74 | */ |
55 | 75 | DeviceEventEmitter.addListener("endCall", payload => { |
56 | 76 | // End call action here |
57 | 77 | }); |
58 | 78 | DeviceEventEmitter.addListener("answerCall", payload => { |
59 | | - // Start call action here |
| 79 | + // Start call action here. You probably want to navigate to some CallRoom screen with the payload.uuid. |
60 | 80 | }); |
61 | 81 | } |
62 | 82 | }, []); |
63 | 83 | ``` |
| 84 | + |
| 85 | +In `index.js` or anywhere firebase background handler lies: |
| 86 | + |
| 87 | +```javascript |
| 88 | +import messaging from '@react-native-firebase/messaging'; |
| 89 | +import {DeviceEventEmitter} from 'react-native'; |
| 90 | +import IncomingCall from 'react-native-incoming-call'; |
| 91 | + |
| 92 | +messaging().setBackgroundMessageHandler(async remoteMessage => { |
| 93 | + // Receive remote message |
| 94 | + if (remoteMessage?.notification?.title === 'Incoming Call') { |
| 95 | + // Display incoming call activity. |
| 96 | + IncomingCall.display( |
| 97 | + 'callUUIDv4', // Call UUID v4 |
| 98 | + 'Quocs', // Username |
| 99 | + 'https://avatars3.githubusercontent.com/u/16166195', // Avatar URL |
| 100 | + 'Incomming Call' // Info text |
| 101 | + ); |
| 102 | + } else if (remoteMessage?.notification?.title === 'Missed Call') { |
| 103 | + // Terminate incoming activity. Should be called when call expired. |
| 104 | + IncomingCall.dismiss(); |
| 105 | + } |
| 106 | + |
| 107 | + // Listen to headless action events |
| 108 | + DeviceEventEmitter.addListener("endCall", payload => { |
| 109 | + // End call action here |
| 110 | + }); |
| 111 | + DeviceEventEmitter.addListener("answerCall", (payload) => { |
| 112 | + console.log('answerCall', payload); |
| 113 | + if (payload.isHeadless) { |
| 114 | + // Called from killed state |
| 115 | + IncomingCall.openAppFromHeadlessMode(payload.uuid); |
| 116 | + } else { |
| 117 | + // Called from background state |
| 118 | + IncomingCall.backToForeground(); |
| 119 | + } |
| 120 | + }); |
| 121 | +}); |
| 122 | +``` |
| 123 | +
|
| 124 | +## Well-known issues |
| 125 | +
|
| 126 | +### Incoming screen not show on android > 9: |
| 127 | +
|
| 128 | +You need to turn on autostart and display pop-up windows permissions manually. I'm searching for a better solution. |
| 129 | +
|
| 130 | +### No vibration when screen locked: |
| 131 | +
|
| 132 | +PR is welcomed! 😂 |
| 133 | +
|
| 134 | +## License |
| 135 | +
|
| 136 | +This project is licensed under the MIT License. |
| 137 | +
|
| 138 | + |
0 commit comments