Skip to content

Commit 3bba7d4

Browse files
Merge pull request #14 from linhvovan29546/fix/prevent-open-app-action-endcall
Fix/prevent open app action end call
2 parents 309e113 + 4727de5 commit 3bba7d4

File tree

5 files changed

+87
-22
lines changed

5 files changed

+87
-22
lines changed

android/src/main/java/com/reactnativefullscreennotificationincomingcall/IncomingCallService.java

+47-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import android.os.Handler;
1818
import android.os.IBinder;
1919
import android.util.Log;
20-
20+
import android.content.BroadcastReceiver;
21+
import android.content.IntentFilter;
2122

2223
import androidx.annotation.Nullable;
2324
import androidx.core.app.NotificationCompat;
@@ -41,6 +42,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
4142
if (action != null) {
4243
if (action.equals(Constants.ACTION_SHOW_INCOMING_CALL)) {
4344
NotificationReceiverHandler.updateCanClick(true);
45+
registerBroadcastPressEvent();
4446
Bundle bundle = intent.getExtras();
4547
uuid= bundle.getString("uuid");
4648
if(bundle.containsKey("timeout")){
@@ -75,6 +77,11 @@ public void onTaskRemoved(Intent rootIntent) {
7577

7678

7779
private PendingIntent onButtonNotificationClick(int id, String action,String eventName) {
80+
if(action == Constants.ACTION_PRESS_DECLINE_CALL){
81+
Intent buttonIntent= new Intent();
82+
buttonIntent.setAction(action);
83+
return PendingIntent.getBroadcast(this,id , buttonIntent, PendingIntent.FLAG_IMMUTABLE);
84+
}
7885
Intent emptyScreenIntent = new Intent(this, NotificationReceiverActivity.class);
7986
emptyScreenIntent.setAction(action);
8087
emptyScreenIntent.putExtras(bundleData);
@@ -161,6 +168,20 @@ public void onDestroy() {
161168
stopForeground(true);
162169
}
163170

171+
public void registerBroadcastPressEvent() {
172+
if (isRegistered) return;
173+
IntentFilter filter = new IntentFilter();
174+
filter.addAction(Constants.ACTION_PRESS_DECLINE_CALL);
175+
getApplicationContext().registerReceiver(mReceiver, filter);
176+
isRegistered = true;
177+
}
178+
179+
public void unregisterBroadcastPressEvent() {
180+
if (!isRegistered) return;
181+
getApplicationContext().unregisterReceiver(mReceiver);
182+
isRegistered = false;
183+
}
184+
164185
public void setTimeOutEndCall(String uuid) {
165186
callhandle=new Handler();
166187
handleTimeout=new Runnable() {
@@ -201,6 +222,31 @@ private int getColorForResourceName(Context context, String colorPath){
201222

202223
return desiredColor;
203224
}
225+
226+
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
227+
@Override
228+
public void onReceive(Context context, Intent intent) {
229+
String action = intent.getAction();
230+
if (action != null) {
231+
if(action.equals(Constants.ACTION_PRESS_DECLINE_CALL)){
232+
boolean canClick=NotificationReceiverHandler.getStatusClick();
233+
if(!canClick)return;
234+
NotificationReceiverHandler.disableClick();
235+
cancelTimer();
236+
if (IncomingCallActivity.active) {
237+
IncomingCallActivity.getInstance().destroyActivity(false);
238+
}
239+
WritableMap params = Arguments.createMap();
240+
params.putString("callUUID", uuid);
241+
params.putString("endAction", Constants.ACTION_REJECTED_CALL);
242+
FullScreenNotificationIncomingCallModule.sendEventToJs(Constants.RNNotificationEndCallAction,params);
243+
stopForeground(true);
244+
}
245+
}
246+
}
247+
};
248+
249+
204250
}
205251

206252

android/src/main/java/com/reactnativefullscreennotificationincomingcall/NotificationReceiverHandler.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ static void updateCanClick(Boolean status){
2323
openedInComing=status;
2424
}
2525

26+
static void disableClick(){
27+
canClick=false;
28+
}
29+
30+
static Boolean getStatusClick(){
31+
return canClick;
32+
}
33+
2634
private static void handleNotificationIntent(Context context, Intent intent) {
2735
if(!canClick) return;
2836
String action= intent.getAction();
@@ -32,7 +40,6 @@ private static void handleNotificationIntent(Context context, Intent intent) {
3240
canClick=false;
3341
handleNotificationPressIntent(context, intent);
3442
break;
35-
case Constants.ACTION_PRESS_DECLINE_CALL:
3643
case Constants.ACTION_PRESS_ANSWER_CALL:
3744
canClick=false;
3845
handleNotificationActionIntent(context,intent);

example/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"react": "17.0.2",
1313
"react-native": "0.65.1",
14+
"react-native-background-timer": "^2.4.1",
1415
"uuid-random": "^1.3.2"
1516
},
1617
"devDependencies": {
@@ -19,4 +20,4 @@
1920
"babel-plugin-module-resolver": "^4.0.0",
2021
"metro-react-native-babel-preset": "^0.66.0"
2122
}
22-
}
23+
}

example/src/App.tsx

+29-18
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import * as React from 'react';
22
import RNNotificationCall from '../../src/index'
33
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native';
44
import ramdomUuid from 'uuid-random';
5+
import BackgroundTimer from 'react-native-background-timer';
56

67
export default function App() {
78
React.useEffect(() => {
89

910
RNNotificationCall.addEventListener("answer", (payload) => {
1011
console.log('press answer', payload.callUUID)
11-
1212
})
1313
RNNotificationCall.addEventListener("endCall", (payload) => {
1414
console.log('press endCall', payload.callUUID)
1515
})
16+
1617
return () => {
1718
RNNotificationCall.removeEventListener("answer")
1819
RNNotificationCall.removeEventListener("endCall")
@@ -24,23 +25,33 @@ export default function App() {
2425
return ramdomUuid().toLowerCase();
2526
};
2627
const display = () => {
27-
const uid = getCurrentCallId()
28-
console.log('uid', uid)
29-
RNNotificationCall.displayNotification(
30-
uid,
31-
null,
32-
30000,
33-
{
34-
channelId: "com.abc.incomingcall",
35-
channelName: "Incoming video call",
36-
notificationIcon: "ic_launcher",//mipmap
37-
notificationTitle: "Linh Vo",
38-
notificationBody: "Incoming video call",
39-
answerText: "Answer",
40-
declineText: "Decline",
41-
notificationColor: 'colorAccent'//path color in android
42-
}
43-
)
28+
// Start a timer that runs once after X milliseconds
29+
const timeoutId = BackgroundTimer.setTimeout(() => {
30+
// this will be executed once after 10 seconds
31+
// even when app is the the background
32+
const uid = getCurrentCallId()
33+
console.log('uid', uid)
34+
RNNotificationCall.displayNotification(
35+
uid,
36+
null,
37+
30000,
38+
{
39+
channelId: "com.abc.incomingcall",
40+
channelName: "Incoming video call",
41+
notificationIcon: "ic_launcher",//mipmap
42+
notificationTitle: "Linh Vo",
43+
notificationBody: "Incoming video call",
44+
answerText: "Answer",
45+
declineText: "Decline",
46+
notificationColor: 'colorAccent'//path color in android
47+
}
48+
)
49+
// Cancel the timeout if necessary
50+
BackgroundTimer.clearTimeout(timeoutId);
51+
}, 1000);
52+
53+
//rest of code will be performing for iOS on background too
54+
4455
}
4556
const onHide = () => {
4657
RNNotificationCall.hideNotification()

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-full-screen-notification-incoming-call",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "Android full screen notification incoming call for React Native",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

0 commit comments

Comments
 (0)