Skip to content

Commit

Permalink
Merge pull request #14 from linhvovan29546/fix/prevent-open-app-actio…
Browse files Browse the repository at this point in the history
…n-endcall

Fix/prevent open app action end call
  • Loading branch information
linhvovan29546 authored Nov 12, 2022
2 parents 309e113 + 4727de5 commit 3bba7d4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import android.content.BroadcastReceiver;
import android.content.IntentFilter;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
Expand All @@ -41,6 +42,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if (action != null) {
if (action.equals(Constants.ACTION_SHOW_INCOMING_CALL)) {
NotificationReceiverHandler.updateCanClick(true);
registerBroadcastPressEvent();
Bundle bundle = intent.getExtras();
uuid= bundle.getString("uuid");
if(bundle.containsKey("timeout")){
Expand Down Expand Up @@ -75,6 +77,11 @@ public void onTaskRemoved(Intent rootIntent) {


private PendingIntent onButtonNotificationClick(int id, String action,String eventName) {
if(action == Constants.ACTION_PRESS_DECLINE_CALL){
Intent buttonIntent= new Intent();
buttonIntent.setAction(action);
return PendingIntent.getBroadcast(this,id , buttonIntent, PendingIntent.FLAG_IMMUTABLE);
}
Intent emptyScreenIntent = new Intent(this, NotificationReceiverActivity.class);
emptyScreenIntent.setAction(action);
emptyScreenIntent.putExtras(bundleData);
Expand Down Expand Up @@ -161,6 +168,20 @@ public void onDestroy() {
stopForeground(true);
}

public void registerBroadcastPressEvent() {
if (isRegistered) return;
IntentFilter filter = new IntentFilter();
filter.addAction(Constants.ACTION_PRESS_DECLINE_CALL);
getApplicationContext().registerReceiver(mReceiver, filter);
isRegistered = true;
}

public void unregisterBroadcastPressEvent() {
if (!isRegistered) return;
getApplicationContext().unregisterReceiver(mReceiver);
isRegistered = false;
}

public void setTimeOutEndCall(String uuid) {
callhandle=new Handler();
handleTimeout=new Runnable() {
Expand Down Expand Up @@ -201,6 +222,31 @@ private int getColorForResourceName(Context context, String colorPath){

return desiredColor;
}

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
if(action.equals(Constants.ACTION_PRESS_DECLINE_CALL)){
boolean canClick=NotificationReceiverHandler.getStatusClick();
if(!canClick)return;
NotificationReceiverHandler.disableClick();
cancelTimer();
if (IncomingCallActivity.active) {
IncomingCallActivity.getInstance().destroyActivity(false);
}
WritableMap params = Arguments.createMap();
params.putString("callUUID", uuid);
params.putString("endAction", Constants.ACTION_REJECTED_CALL);
FullScreenNotificationIncomingCallModule.sendEventToJs(Constants.RNNotificationEndCallAction,params);
stopForeground(true);
}
}
}
};


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ static void updateCanClick(Boolean status){
openedInComing=status;
}

static void disableClick(){
canClick=false;
}

static Boolean getStatusClick(){
return canClick;
}

private static void handleNotificationIntent(Context context, Intent intent) {
if(!canClick) return;
String action= intent.getAction();
Expand All @@ -32,7 +40,6 @@ private static void handleNotificationIntent(Context context, Intent intent) {
canClick=false;
handleNotificationPressIntent(context, intent);
break;
case Constants.ACTION_PRESS_DECLINE_CALL:
case Constants.ACTION_PRESS_ANSWER_CALL:
canClick=false;
handleNotificationActionIntent(context,intent);
Expand Down
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"react": "17.0.2",
"react-native": "0.65.1",
"react-native-background-timer": "^2.4.1",
"uuid-random": "^1.3.2"
},
"devDependencies": {
Expand All @@ -19,4 +20,4 @@
"babel-plugin-module-resolver": "^4.0.0",
"metro-react-native-babel-preset": "^0.66.0"
}
}
}
47 changes: 29 additions & 18 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import * as React from 'react';
import RNNotificationCall from '../../src/index'
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native';
import ramdomUuid from 'uuid-random';
import BackgroundTimer from 'react-native-background-timer';

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

RNNotificationCall.addEventListener("answer", (payload) => {
console.log('press answer', payload.callUUID)

})
RNNotificationCall.addEventListener("endCall", (payload) => {
console.log('press endCall', payload.callUUID)
})

return () => {
RNNotificationCall.removeEventListener("answer")
RNNotificationCall.removeEventListener("endCall")
Expand All @@ -24,23 +25,33 @@ export default function App() {
return ramdomUuid().toLowerCase();
};
const display = () => {
const uid = getCurrentCallId()
console.log('uid', uid)
RNNotificationCall.displayNotification(
uid,
null,
30000,
{
channelId: "com.abc.incomingcall",
channelName: "Incoming video call",
notificationIcon: "ic_launcher",//mipmap
notificationTitle: "Linh Vo",
notificationBody: "Incoming video call",
answerText: "Answer",
declineText: "Decline",
notificationColor: 'colorAccent'//path color in android
}
)
// Start a timer that runs once after X milliseconds
const timeoutId = BackgroundTimer.setTimeout(() => {
// this will be executed once after 10 seconds
// even when app is the the background
const uid = getCurrentCallId()
console.log('uid', uid)
RNNotificationCall.displayNotification(
uid,
null,
30000,
{
channelId: "com.abc.incomingcall",
channelName: "Incoming video call",
notificationIcon: "ic_launcher",//mipmap
notificationTitle: "Linh Vo",
notificationBody: "Incoming video call",
answerText: "Answer",
declineText: "Decline",
notificationColor: 'colorAccent'//path color in android
}
)
// Cancel the timeout if necessary
BackgroundTimer.clearTimeout(timeoutId);
}, 1000);

//rest of code will be performing for iOS on background too

}
const onHide = () => {
RNNotificationCall.hideNotification()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-full-screen-notification-incoming-call",
"version": "0.1.4",
"version": "0.1.5",
"description": "Android full screen notification incoming call for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down

0 comments on commit 3bba7d4

Please sign in to comment.