We plan to release an update for our Android application, which is based on the Twilio Voice SDK. Our customers want to get more relatives, for example, when they can see the screen to accept or reject a call (for example, Skype / Whatsapp / Viber / Line, etc.) Instead of clicking on the notification, and then on the dialog box. In addition, this should also work on the lock screen.
At the moment, I am successfully opening activity in my application and showing accept or reject buttons. It works when the application is in the foreground or in the background. Here is a piece of code that achieves this. I changed the notify () method in VoiceFirebaseMessagingService.java to show the action when onMessageRecived is called to notify you of an incoming call.
private void notify(CallInvite callInvite, int notificationId) {
String callSid = callInvite.getCallSid();
if (callInvite.getState() == CallInvite.State.PENDING) {
soundPoolManager.playRinging();
System.out.println("Disabling keyguard and accquiring wake lock");
Intent intent = new Intent(this, OnCallActivityNew.class);
intent.setAction(OnCallActivityNew.ACTION_INCOMING_CALL);
intent.putExtra(OnCallActivityNew.INCOMING_CALL_NOTIFICATION_ID, notificationId);
intent.putExtra(OnCallActivityNew.INCOMING_CALL_INVITE, callInvite);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, notificationId, intent, PendingIntent.FLAG_ONE_SHOT);
Bundle extras = new Bundle();
extras.putInt(NOTIFICATION_ID_KEY, notificationId);
extras.putString(CALL_SID_KEY, callSid);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_call_end_white_24px)
.setContentTitle(getString(R.string.app_name))
.setContentText(callInvite.getFrom() + " is calling.")
.setAutoCancel(true)
.setExtras(extras)
.setContentIntent(pendingIntent)
.setGroup("test_app_notification")
.setOngoing(true)
.setColor(Color.rgb(214, 10, 37));
notificationManager.notify(notificationId, notificationBuilder.build());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtras(extras);
globalintent = intent;
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
startActivity(globalintent);
}
},2000);
} else {
SoundPoolManager.getInstance(this).stopRinging();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications();
for (StatusBarNotification statusBarNotification : activeNotifications) {
Notification notification = statusBarNotification.getNotification();
Bundle extras = notification.extras;
String notificationCallSid = extras.getString(CALL_SID_KEY);
if (callSid.equals(notificationCallSid)) {
notificationManager.cancel(extras.getInt(NOTIFICATION_ID_KEY));
} else {
sendCallInviteToActivity(callInvite, notificationId);
}
}
} else {
notificationManager.cancelAll();
}
}
}
Also, in onCreate () of OnCallActivityNew.java, I mentioned the following code.
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("on create of activity is called for oncallactivitynew");
super.onCreate(savedInstanceState);
KeyguardManager kgm = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
KeyguardManager.KeyguardLock kgl = kgm.newKeyguardLock("OnCallActivityNew");
if(isKeyguardUp){
kgl.disableKeyguard();
isKeyguardUp = false;
}
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_on_call);
coordinatorLayout = (CoordinatorLayout) fin
.....
}
The only problem is that when the phone is locked, this action opens and is called onDestroy (), and I can not show the screen to accept and reject buttons.
The desired behavior that I want is to have a mechanism in which you can receive calls even on the lock screen in the same way as the applications I mentioned above.
, , , Android, , , . , .