I am trying to provide a native video calling experience with Twilio Video Call. Here is the scenario:
So we had figured out this solution (when a notification arrives, bring the app to foreground) and I'm posting it even though it's been a while:
The FCM notification (firebase cloud messaging notification) needs to only send "data" in the notification. So no Notification object in the JSON structure of the notification, only data. This way the notification is handled by your app's FirebaseMessagingService.java class. Please read the below in detail to understand how 2 FCM notification types are handled. https://firebase.google.com/docs/cloud-messaging/android/receive https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
In the FirebaseMessagingService.java class, launch a VideoCall activity with an Intent. Don't forget to add this service to the Manifest.xml
Intent intent = new Intent(this, VideoCallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
getApplicationContext().startActivity(intent);
In the VideoCall activity, make sure you have the below code in the beginning of onCreate() :
// These flags ensure that the activity can be launched when the screen is locked.
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// to wake up screen
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();
// to release screen lock
KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
Add the VideoCallActivity to the Manifest.xml with the appropriate intent-filter:
Optional: To make the phone ring and vibrate:
// For Incoming Call
// 1. declare
private MediaPlayer incomingCallMediaPlayer;
// .2 in onCreate, if I'm the person that's calling, ring the phone
incomingCallMediaPlayer = MediaPlayer.create(this, R.raw.incoming);
incomingCallMediaPlayer.setLooping(true);
incomingCallMediaPlayer.start();
// 3. when I pick up, stop the player
incomingCallMediaPlayer.stop();
// I play R.raw.incoming if I'm being called.
// I play R.raw.outgoing when I'm calling.
// I understand if I'm the one calling from the number of participants in the "room" (this is a video call terminology) and by passing in a variable through the intent
// For Outgoing Call
// 1. declare
private MediaPlayer callingMediaPlayer;
// 2. in onCreate, if I'm being called, ring the phone
callingMediaPlayer = MediaPlayer.create(this, R.raw.outgoing);
callingMediaPlayer.setLooping(true);
callingMediaPlayer.start();
// 3. when another "participant" (this is a video call terminology) joins the "room" I stop playing the sound
callingMediaPlayer.stop();
// to Vibrate, add the code with the media players and stop vibrate with media players
//https://stackoverflow.com/questions/13950338/how-to-make-an-android-device-vibrate