Skip to content

Commit

Permalink
Merge pull request #7 from HarshaR1642/AVAT-1297
Browse files Browse the repository at this point in the history
bug/AVAT-1297 [Intercom] [Android] Call notification UI is not visible
  • Loading branch information
bhishakBitcanny authored Feb 19, 2024
2 parents 4bfd82a + bffc1af commit 8e534e5
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 9 deletions.
4 changes: 4 additions & 0 deletions android/src/main/java/com/incomingcall/AnswerCallActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AnswerCallActivity : ReactActivity() {
or WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
)

IncomingCallModule.sendIntercomBroadcast(this, "Call Answered")

if (CallingActivity.active) {
sendBroadcast(Intent(Constants.ACTION_END_CALL))
Expand Down Expand Up @@ -80,6 +81,9 @@ class AnswerCallActivity : ReactActivity() {
private val mBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
if (intent.action == Constants.ACTION_END_CALL) {
context?.let {
IncomingCallModule.sendIntercomBroadcast(it, "Call Hangup")
}
finishAndRemoveTask()
}
}
Expand Down
21 changes: 15 additions & 6 deletions android/src/main/java/com/incomingcall/CallService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.os.VibratorManager
import android.widget.RemoteViews
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import java.lang.Error

class CallService : Service() {
override fun onBind(intent: Intent?): IBinder? {
Expand All @@ -29,13 +30,19 @@ class CallService : Service() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

val bundle = intent?.extras
try {
val bundle = intent?.extras

val notification: Notification = buildNotification(bundle)
startForeground(Constants.FOREGROUND_SERVICE_ID, notification)
playRingtone()
startVibration()
startTimer(Constants.TIME_OUT)
val notification: Notification = buildNotification(bundle)
startForeground(Constants.FOREGROUND_SERVICE_ID, notification)
playRingtone()
startVibration()
startTimer(Constants.TIME_OUT)
IncomingCallModule.sendIntercomBroadcast(this, "Notification showed")

} catch (error: Error) {
IncomingCallModule.sendIntercomBroadcast(this, "Failed to show notification")
}

return START_NOT_STICKY
}
Expand Down Expand Up @@ -68,6 +75,8 @@ class CallService : Service() {
notificationChannel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC

notificationManager.createNotificationChannel(notificationChannel)
} else {
IncomingCallModule.sendIntercomBroadcast(this, "Android OS less than API 26")
}
val notification = NotificationCompat.Builder(this, Constants.CHANNEL)
notification.setContentTitle(Constants.INCOMING_CALL)
Expand Down
5 changes: 5 additions & 0 deletions android/src/main/java/com/incomingcall/CallingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,22 @@ class CallingActivity : ReactActivity() {
registerReceiver(mBroadcastReceiver, mIntentFilter)
}

IncomingCallModule.sendIntercomBroadcast(this, "Incoming call full screen showed")

acceptButton.setOnClickListener {
stopService(Intent(this, CallService::class.java))
IncomingCallModule.sendIntercomBroadcast(this, "Call accepted from full screen")
val answerIntent = Intent(this, AnswerCallActivity::class.java)
startActivity(answerIntent)
finishAndRemoveTask()
}

declineButton.setOnClickListener {
stopService(Intent(this, CallService::class.java))
IncomingCallModule.sendIntercomBroadcast(this, "Call declined from full screen")
finishAndRemoveTask()
}

}

private val mBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/com/incomingcall/HungUpBroadcast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ class HungUpBroadcast : BroadcastReceiver() {

val stopIntent = Intent(context, CallService::class.java)
context?.stopService(stopIntent)

IncomingCallModule.sendIntercomBroadcast(context!!, "Call Declined")

}
}
49 changes: 48 additions & 1 deletion android/src/main/java/com/incomingcall/IncomingCallModule.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.incomingcall

import android.annotation.SuppressLint
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationManagerCompat
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
Expand All @@ -21,10 +25,42 @@ class IncomingCallModule(reactContext: ReactApplicationContext) :
return Constants.INCOMING_CALL
}

private val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.extras?.getString("action")
if(!action.isNullOrEmpty()){
val params: WritableMap = Arguments.createMap()
params.putString("action", action)
sendEventToJs("intercom_broadcast", params)
}
}
}

@SuppressLint("UnspecifiedRegisterReceiverFlag")
@ReactMethod
fun registerReceiver() {
try {
unregisterReceiver()
val intentFilter = IntentFilter("android.intercom.broadcast")
reactApplicationContext.registerReceiver(broadcastReceiver, intentFilter)
}catch (error: Exception){
error.printStackTrace()
}
}

@ReactMethod
fun unregisterReceiver() {
try {
reactApplicationContext.unregisterReceiver(broadcastReceiver)
}catch (error: Exception){
error.printStackTrace()
}
}

@RequiresApi(Build.VERSION_CODES.O)
@ReactMethod
fun showIncomingCall(options: ReadableMap?) {
if(AnswerCallActivity.active){
if (AnswerCallActivity.active) {
return
}
reactApplicationContext.stopService(
Expand All @@ -36,6 +72,8 @@ class IncomingCallModule(reactContext: ReactApplicationContext) :
val intent = Intent(reactApplicationContext, CallService::class.java)

reactApplicationContext.startForegroundService(intent)

sendIntercomBroadcast(reactApplicationContext, "Invoked call notification")
}

@ReactMethod
Expand Down Expand Up @@ -73,4 +111,13 @@ class IncomingCallModule(reactContext: ReactApplicationContext) :
reactApplicationContext?.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
?.emit(eventName, params)
}


companion object {
fun sendIntercomBroadcast(context: Context, action: String){
val intent = Intent("android.intercom.broadcast")
intent.putExtra("action", action)
context.sendBroadcast(intent)
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@harsha1642/react-native-incomingcall",
"version": "0.1.0",
"version": "2.0.0",
"description": "test",
"main": "src/index",
"module": "lib/module/index",
Expand Down
20 changes: 19 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { NativeModules, Platform } from 'react-native';

const IncomingCall = NativeModules.IncomingCall;

const registerReceiver = (): void => {
if (Platform.OS === 'android') {
IncomingCall.registerReceiver();
}
};

const unregisterReceiver = (): void => {
if (Platform.OS === 'android') {
IncomingCall.unregisterReceiver();
}
};

const showIncomingCall = (options = {}): void => {
if (Platform.OS === 'android') {
IncomingCall.showIncomingCall(options);
Expand All @@ -19,4 +31,10 @@ const areNotificationsEnabled = async () => {
return granted;
};

export { showIncomingCall, endCall, areNotificationsEnabled };
export {
showIncomingCall,
endCall,
areNotificationsEnabled,
registerReceiver,
unregisterReceiver,
};

0 comments on commit 8e534e5

Please sign in to comment.