Reputation: 5980
I am making a VOIP call app. When a call comes in, it should open the app, even if the app is closed or never opened. We also need the call notifications to come in from a server.
I know apps like WhatsApp and Facebook Messenger do this, but how?
We are using a design similar to follows: Build a Calling App
We have tried using Firebase Cloud Messaging as recommended by Android documentation, and while there is moderate success, it does not work when the app is closed.
We are considering using a Sync Adapter or WorkManager next, but it takes quite a bit of time to prototype, and I'd prefer to ask if anyone has any success or if there are existing plugins for this.
As I'm aware, there are also going to be restrictions on Android 10. It says to use time-sensitive notifications, but these will still need to be triggered from a server somehow.
Upvotes: 2
Views: 2104
Reputation: 1521
In your calling activity add this code. It will turn your screen on.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
this.setTurnScreenOn(true);
} else {
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
and in your activity manifest, define intent-filter as CALL
<activity
android:name=".activities.CallActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden">
<intent-filter>
<action android:name="android.intent.action.CALL" />
</intent-filter>
</activity>
When you receive notification from Firebase in onMessageReceived(RemoteMessage remoteMessage), open your activity
openActivity(CallActivity.class);
public void openActivity(Class<?> tClass) {
Intent i = new Intent(this, tClass);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
Upvotes: 1