How to show the incoming call screen when the screen is locked?
Asked Answered
A

1

13

I'm developing my custom calling app, like skype and I need to show "incoming call" screen to user, when I receive fcm message. I use full screen intent notification for this purpose. My code now is like this:

        val intent = Intent(Intent.ACTION_MAIN, null)
        val fakeIntent = Intent()
        intent.flags = Intent.FLAG_ACTIVITY_NO_USER_ACTION or Intent.FLAG_ACTIVITY_NEW_TASK
        intent.setClass(ctx, IncomingCallActivity::class.java!!)
        val pendingIntent = PendingIntent.getActivity(ctx, 1, intent, 0)
        val pendingIntent2 = PendingIntent.getActivity(ctx, 1, fakeIntent, PendingIntent.FLAG_ONE_SHOT)
        val builder = Notification.Builder(ctx)
        builder.setOngoing(true)
        builder.setPriority(Notification.PRIORITY_HIGH)

        // Set notification content intent to take user to fullscreen UI if user taps on the
        // notification body.
        builder.setContentIntent(pendingIntent)
        // Set full screen intent to trigger display of the fullscreen UI when the notification
        // manager deems it appropriate.
        builder.setFullScreenIntent(pendingIntent, true)

        // Setup notification content.
        builder.setSmallIcon(R.mipmap.ic_launcher)
        builder.setContentTitle("Call from Vitalii")
        builder.setContentText("Your notification content.")
        builder.setAutoCancel(true)


        // Use builder.addAction(..) to add buttons to answer or reject the call.
        val acceptAction = Notification.Action.Builder(Icon.createWithResource(ctx, R.drawable.ic_launch), "Accept", pendingIntent).build()
        val declineAction = Notification.Action.Builder(Icon.createWithResource(ctx, R.drawable.ic_launch), "Decline", pendingIntent2).build()
        builder.addAction(acceptAction)
        builder.addAction(declineAction)
        val notificationManager = ctx.getSystemService(
                NotificationManager::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("callnotification", "Incoming Calls", NotificationManager.IMPORTANCE_MAX)
            val ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            channel.setSound(ringtoneUri, AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build())
            notificationManager.createNotificationChannel(channel)
            builder.setChannelId("callnotification")
        }
        val notification = builder.build()
        notification.flags = Notification.FLAG_INSISTENT
        currentNotificationId = Random().nextInt(500)
        intent.putExtra("notifid", currentNotificationId)
        notificationManager.notify("callnotification", currentNotificationId, notification)

And when screen is unlocked I receive calling notification with buttons to accept the call and decline. But when screen is locked I receive only sound and vibration, but not the custom screen with buttons like in telegram, whatsup and viber. How can I show such custom screen when device is locked?

Antipode answered 7/11, 2018 at 14:52 Comment(0)
W
21

Add following code in your CallActivity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
Wooten answered 7/11, 2018 at 14:59 Comment(4)
Aolphn, can I add some active buttons or start new activity then from my CallActivity? How to add accept and reject button there?Antipode
Add button?just like normal activity it will be worked,try it,please tell me if you got any question .Wooten
Yes, the problem was I can't load webview in this activity. It seems to be too heavy. But all buttons are working. Thanks.Antipode
guys I don't understand where to put this code? In receiver activity or another place ? I have some issueVacant

© 2022 - 2024 — McMap. All rights reserved.