Search code examples
androidforeground-serviceandroid-push-notification

Android push dismises after few seconds


I'm trying to create the incoming call push notification. When a call event occurs the foreground service with notification starts. I create a channel for it and notification. Here is the code:

The channel settings:

    private fun createCallChannelChannel(): NotificationChannel {
    val attributes = AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
        .build()
    val importance = NotificationManager.IMPORTANCE_HIGH
    return NotificationChannel(CALL_CHANNEL_ID, CALL_CHANNEL_NAME, importance).apply {
        description = CALL_CHANNEL_DESCRIPTION
        setSound(
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE),
            attributes
        )
        enableLights(true)
        enableVibration(true)
    }
}

The notification settings:

    private fun buildIncomingCallNotification(payload: VoIpCallResponse): Notification {

    val remoteView = RemoteViews(packageName, R.layout.notification_call_view)
    remoteView.setOnClickPendingIntent(R.id.declineBtn, getDeclinePendingIntent())
    remoteView.setOnClickPendingIntent(R.id.acceptBtn, getAcceptPendingIntent(payload))
    return NotificationCompat.Builder(this, CALL_CHANNEL_ID)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .setCategory(NotificationCompat.CATEGORY_CALL)
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(remoteView)
        .setAutoCancel(false)
        .build()
}

It works. The notification is being shown. But the problem is notification minimizes to the notifications bar after a few seconds. The goal is to prevent the notification minimization until user decline/ accept the call or the end call event occurs. For example WhatsApp. The incoming call notification stays at the top of the screen for an infinite time. How can I make the same? The importance of my channel is NotificationManager.IMPORTANCE_HIGH and the notification priority is NotificationCompat.PRIORITY_MAX


Solution

  • I have found this page and it has worked

    https://developer.android.com/training/notify-user/time-sensitive

        val fullScreenIntent = Intent(this, CallActivity::class.java)
    val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
        fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
    
    val notificationBuilder =
            NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Incoming call")
        .setContentText("(919) 555-1234")
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setCategory(NotificationCompat.CATEGORY_CALL)
    
    
    
    // Use a full-screen intent only for the highest-priority alerts where you
        // have an associated activity that you would like to launch after the user
        // interacts with the notification. Also, if your app targets Android 10
        // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
        // order for the platform to invoke this notification.
        .setFullScreenIntent(fullScreenPendingIntent, true)
    

    and of course use with foregroundService

    val incomingCallNotification = notificationBuilder.build()