Skip to content

Commit 3b46d50

Browse files
[AND-207] Simplify notification overriding for customization (#1265)
* 1. Expose the PendingIntent getters for different types of calls as public APIs. 2. Mark intentResolver with public visibility * change method visibility to open for enhanced notification customization Updated visibility of private and default methods to open to allow further extension and customization of notifications. This change provides greater flexibility for users to override and tailor notification behavior according to their specific needs. --------- Co-authored-by: Aleksandar Apostolov <apostolov.alexandar@gmail.com>
1 parent 43198f8 commit 3b46d50

File tree

5 files changed

+208
-86
lines changed

5 files changed

+208
-86
lines changed

stream-video-android-core/api/stream-video-android-core.api

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4335,16 +4335,29 @@ public class io/getstream/video/android/core/notifications/DefaultNotificationHa
43354335
public static final field Companion Lio/getstream/video/android/core/notifications/DefaultNotificationHandler$Companion;
43364336
public fun <init> (Landroid/app/Application;Lio/getstream/android/push/permissions/NotificationPermissionHandler;ZI)V
43374337
public synthetic fun <init> (Landroid/app/Application;Lio/getstream/android/push/permissions/NotificationPermissionHandler;ZIILkotlin/jvm/internal/DefaultConstructorMarker;)V
4338+
public fun addCallActions (Landroidx/core/app/NotificationCompat$Builder;Landroid/app/PendingIntent;Landroid/app/PendingIntent;Ljava/lang/String;)Landroidx/core/app/NotificationCompat$Builder;
4339+
public fun addHangUpAction (Landroidx/core/app/NotificationCompat$Builder;Landroid/app/PendingIntent;Ljava/lang/String;I)Landroidx/core/app/NotificationCompat$Builder;
4340+
public fun createIncomingCallChannel (Ljava/lang/String;Z)V
4341+
public fun createOnGoingChannel (Ljava/lang/String;)V
4342+
public fun getAcceptAction (Landroid/app/PendingIntent;)Landroidx/core/app/NotificationCompat$Action;
43384343
public fun getChannelDescription ()Ljava/lang/String;
43394344
public fun getChannelId ()Ljava/lang/String;
43404345
public fun getChannelName ()Ljava/lang/String;
43414346
public final fun getHideRingingNotificationInForeground ()Z
4347+
public fun getIncomingCallNotification (Landroid/app/PendingIntent;Landroid/app/PendingIntent;Landroid/app/PendingIntent;Ljava/lang/String;Z)Landroid/app/Notification;
4348+
public final fun getIntentResolver ()Lio/getstream/video/android/core/notifications/DefaultStreamIntentResolver;
4349+
public fun getLeaveAction (Landroid/app/PendingIntent;)Landroidx/core/app/NotificationCompat$Action;
4350+
public fun getNotification (Lkotlin/jvm/functions/Function1;)Landroid/app/Notification;
43424351
public final fun getNotificationIconRes ()I
43434352
protected final fun getNotificationManager ()Landroidx/core/app/NotificationManagerCompat;
43444353
public fun getNotificationUpdates (Lkotlinx/coroutines/CoroutineScope;Lio/getstream/video/android/core/Call;Lio/getstream/video/android/model/User;Lkotlin/jvm/functions/Function1;)V
43454354
public fun getOngoingCallNotification (Lio/getstream/video/android/model/StreamCallId;Ljava/lang/String;ZI)Landroid/app/Notification;
4355+
public fun getRejectAction (Landroid/app/PendingIntent;)Landroidx/core/app/NotificationCompat$Action;
43464356
public fun getRingingCallNotification (Lio/getstream/video/android/core/RingingState;Lio/getstream/video/android/model/StreamCallId;Ljava/lang/String;Z)Landroid/app/Notification;
43474357
public fun getSettingUpCallNotification ()Landroid/app/Notification;
4358+
public fun isInForeground ()Z
4359+
public fun maybeCreateChannel (Ljava/lang/String;Landroid/content/Context;Lkotlin/jvm/functions/Function1;)V
4360+
public static synthetic fun maybeCreateChannel$default (Lio/getstream/video/android/core/notifications/DefaultNotificationHandler;Ljava/lang/String;Landroid/content/Context;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
43484361
public fun onLiveCall (Lio/getstream/video/android/model/StreamCallId;Ljava/lang/String;)V
43494362
public fun onMissedCall (Lio/getstream/video/android/model/StreamCallId;Ljava/lang/String;)V
43504363
public fun onNotification (Lio/getstream/video/android/model/StreamCallId;Ljava/lang/String;)V
@@ -4353,11 +4366,32 @@ public class io/getstream/video/android/core/notifications/DefaultNotificationHa
43534366
public fun onPermissionRationale ()V
43544367
public fun onPermissionRequested ()V
43554368
public fun onRingingCall (Lio/getstream/video/android/model/StreamCallId;Ljava/lang/String;)V
4369+
public fun showLiveCallNotification (Landroid/app/PendingIntent;Ljava/lang/String;I)V
4370+
public fun showMissedCallNotification (Landroid/app/PendingIntent;Ljava/lang/String;I)V
4371+
public fun showNotificationCallNotification (Landroid/app/PendingIntent;Ljava/lang/String;I)V
43564372
}
43574373

43584374
public final class io/getstream/video/android/core/notifications/DefaultNotificationHandler$Companion {
43594375
}
43604376

4377+
public final class io/getstream/video/android/core/notifications/DefaultStreamIntentResolver {
4378+
public fun <init> (Landroid/content/Context;)V
4379+
public final fun getContext ()Landroid/content/Context;
4380+
public final fun getDefaultPendingIntent ()Landroid/app/PendingIntent;
4381+
public final fun searchAcceptCallPendingIntent (Lio/getstream/video/android/model/StreamCallId;I)Landroid/app/PendingIntent;
4382+
public static synthetic fun searchAcceptCallPendingIntent$default (Lio/getstream/video/android/core/notifications/DefaultStreamIntentResolver;Lio/getstream/video/android/model/StreamCallId;IILjava/lang/Object;)Landroid/app/PendingIntent;
4383+
public final fun searchEndCallPendingIntent (Lio/getstream/video/android/model/StreamCallId;)Landroid/app/PendingIntent;
4384+
public final fun searchIncomingCallPendingIntent (Lio/getstream/video/android/model/StreamCallId;I)Landroid/app/PendingIntent;
4385+
public static synthetic fun searchIncomingCallPendingIntent$default (Lio/getstream/video/android/core/notifications/DefaultStreamIntentResolver;Lio/getstream/video/android/model/StreamCallId;IILjava/lang/Object;)Landroid/app/PendingIntent;
4386+
public final fun searchLiveCallPendingIntent (Lio/getstream/video/android/model/StreamCallId;I)Landroid/app/PendingIntent;
4387+
public final fun searchMissedCallPendingIntent (Lio/getstream/video/android/model/StreamCallId;I)Landroid/app/PendingIntent;
4388+
public final fun searchNotificationCallPendingIntent (Lio/getstream/video/android/model/StreamCallId;I)Landroid/app/PendingIntent;
4389+
public final fun searchOngoingCallPendingIntent (Lio/getstream/video/android/model/StreamCallId;I)Landroid/app/PendingIntent;
4390+
public final fun searchOutgoingCallPendingIntent (Lio/getstream/video/android/model/StreamCallId;I)Landroid/app/PendingIntent;
4391+
public static synthetic fun searchOutgoingCallPendingIntent$default (Lio/getstream/video/android/core/notifications/DefaultStreamIntentResolver;Lio/getstream/video/android/model/StreamCallId;IILjava/lang/Object;)Landroid/app/PendingIntent;
4392+
public final fun searchRejectCallPendingIntent (Lio/getstream/video/android/model/StreamCallId;)Landroid/app/PendingIntent;
4393+
}
4394+
43614395
public final class io/getstream/video/android/core/notifications/NotificationConfig {
43624396
public fun <init> ()V
43634397
public fun <init> (Ljava/util/List;Lkotlin/jvm/functions/Function0;Lio/getstream/video/android/core/notifications/NotificationHandler;ZLkotlin/jvm/functions/Function0;Z)V
@@ -4396,6 +4430,7 @@ public abstract interface class io/getstream/video/android/core/notifications/No
43964430
public static final field INTENT_EXTRA_CALL_CID Ljava/lang/String;
43974431
public static final field INTENT_EXTRA_CALL_DISPLAY_NAME Ljava/lang/String;
43984432
public static final field INTENT_EXTRA_NOTIFICATION_ID Ljava/lang/String;
4433+
public abstract fun getIncomingCallNotification (Landroid/app/PendingIntent;Landroid/app/PendingIntent;Landroid/app/PendingIntent;Ljava/lang/String;Z)Landroid/app/Notification;
43994434
public abstract fun getNotificationUpdates (Lkotlinx/coroutines/CoroutineScope;Lio/getstream/video/android/core/Call;Lio/getstream/video/android/model/User;Lkotlin/jvm/functions/Function1;)V
44004435
public abstract fun getOngoingCallNotification (Lio/getstream/video/android/model/StreamCallId;Ljava/lang/String;ZI)Landroid/app/Notification;
44014436
public static synthetic fun getOngoingCallNotification$default (Lio/getstream/video/android/core/notifications/NotificationHandler;Lio/getstream/video/android/model/StreamCallId;Ljava/lang/String;ZIILjava/lang/Object;)Landroid/app/Notification;

stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/notifications/DefaultNotificationHandler.kt

Lines changed: 84 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import io.getstream.video.android.core.RingingState
4242
import io.getstream.video.android.core.notifications.NotificationHandler.Companion.ACTION_LIVE_CALL
4343
import io.getstream.video.android.core.notifications.NotificationHandler.Companion.ACTION_MISSED_CALL
4444
import io.getstream.video.android.core.notifications.NotificationHandler.Companion.ACTION_NOTIFICATION
45-
import io.getstream.video.android.core.notifications.internal.DefaultStreamIntentResolver
4645
import io.getstream.video.android.core.notifications.internal.service.CallService
4746
import io.getstream.video.android.model.StreamCallId
4847
import io.getstream.video.android.model.User
@@ -74,7 +73,7 @@ public open class DefaultNotificationHandler(
7473
NotificationPermissionHandler by notificationPermissionHandler {
7574

7675
private val logger by taggedLogger("Call:NotificationHandler")
77-
private val intentResolver = DefaultStreamIntentResolver(application)
76+
val intentResolver = DefaultStreamIntentResolver(application)
7877
protected val notificationManager: NotificationManagerCompat by lazy {
7978
NotificationManagerCompat.from(application).also {
8079
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -197,7 +196,7 @@ public open class DefaultNotificationHandler(
197196
}
198197
}
199198

200-
private fun getIncomingCallNotification(
199+
override fun getIncomingCallNotification(
201200
fullScreenPendingIntent: PendingIntent,
202201
acceptCallPendingIntent: PendingIntent,
203202
rejectCallPendingIntent: PendingIntent,
@@ -209,7 +208,6 @@ public open class DefaultNotificationHandler(
209208
// screen instead - but this needs to be handled by the application.
210209
// The default behaviour is that all notification are high priority
211210
val showAsHighPriority = !hideRingingNotificationInForeground || !isInForeground()
212-
213211
val channelId = application.getString(
214212
if (showAsHighPriority) {
215213
R.string.stream_video_incoming_call_notification_channel_id
@@ -218,6 +216,35 @@ public open class DefaultNotificationHandler(
218216
},
219217
)
220218

219+
createIncomingCallChannel(channelId, showAsHighPriority)
220+
221+
return getNotification {
222+
priority = NotificationCompat.PRIORITY_HIGH
223+
setContentTitle(callerName)
224+
setContentText(
225+
application.getString(R.string.stream_video_incoming_call_notification_description),
226+
)
227+
setChannelId(channelId)
228+
setOngoing(true)
229+
setCategory(NotificationCompat.CATEGORY_CALL)
230+
setFullScreenIntent(fullScreenPendingIntent, true)
231+
if (shouldHaveContentIntent) {
232+
setContentIntent(fullScreenPendingIntent)
233+
} else {
234+
val emptyIntent = PendingIntent.getActivity(
235+
application,
236+
0,
237+
Intent(),
238+
PendingIntent.FLAG_IMMUTABLE,
239+
)
240+
setContentIntent(emptyIntent)
241+
setAutoCancel(false)
242+
}
243+
addCallActions(acceptCallPendingIntent, rejectCallPendingIntent, callerName)
244+
}
245+
}
246+
247+
open fun createIncomingCallChannel(channelId: String, showAsHighPriority: Boolean) {
221248
maybeCreateChannel(
222249
channelId = channelId,
223250
context = application,
@@ -246,31 +273,6 @@ public open class DefaultNotificationHandler(
246273
}
247274
},
248275
)
249-
250-
return getNotification {
251-
priority = NotificationCompat.PRIORITY_HIGH
252-
setContentTitle(callerName)
253-
setContentText(
254-
application.getString(R.string.stream_video_incoming_call_notification_description),
255-
)
256-
setChannelId(channelId)
257-
setOngoing(true)
258-
setCategory(NotificationCompat.CATEGORY_CALL)
259-
setFullScreenIntent(fullScreenPendingIntent, true)
260-
if (shouldHaveContentIntent) {
261-
setContentIntent(fullScreenPendingIntent)
262-
} else {
263-
val emptyIntent = PendingIntent.getActivity(
264-
application,
265-
0,
266-
Intent(),
267-
PendingIntent.FLAG_IMMUTABLE,
268-
)
269-
setContentIntent(emptyIntent)
270-
setAutoCancel(false)
271-
}
272-
addCallActions(acceptCallPendingIntent, rejectCallPendingIntent, callerName)
273-
}
274276
}
275277

276278
override fun onNotification(callId: StreamCallId, callDisplayName: String) {
@@ -327,19 +329,8 @@ public open class DefaultNotificationHandler(
327329
val ongoingCallsChannelId = application.getString(
328330
R.string.stream_video_ongoing_call_notification_channel_id,
329331
)
330-
maybeCreateChannel(
331-
channelId = ongoingCallsChannelId,
332-
context = application,
333-
configure = {
334-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
335-
name = application.getString(
336-
R.string.stream_video_ongoing_call_notification_channel_title,
337-
)
338-
description =
339-
application.getString(R.string.stream_video_ongoing_call_notification_channel_description)
340-
}
341-
},
342-
)
332+
333+
createOnGoingChannel(ongoingCallsChannelId)
343334

344335
if (hangUpIntent == null) {
345336
logger.e { "End call intent is null, not showing notification!" }
@@ -379,6 +370,22 @@ public open class DefaultNotificationHandler(
379370
.build()
380371
}
381372

373+
open fun createOnGoingChannel(ongoingCallsChannelId: String) {
374+
maybeCreateChannel(
375+
channelId = ongoingCallsChannelId,
376+
context = application,
377+
configure = {
378+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
379+
name = application.getString(
380+
R.string.stream_video_ongoing_call_notification_channel_title,
381+
)
382+
description =
383+
application.getString(R.string.stream_video_ongoing_call_notification_channel_description)
384+
}
385+
},
386+
)
387+
}
388+
382389
override fun getNotificationUpdates(
383390
coroutineScope: CoroutineScope,
384391
call: Call,
@@ -460,7 +467,7 @@ public open class DefaultNotificationHandler(
460467
}
461468
}
462469

463-
private fun maybeCreateChannel(
470+
open fun maybeCreateChannel(
464471
channelId: String,
465472
context: Context,
466473
configure: NotificationChannel.() -> Unit = {
@@ -481,7 +488,7 @@ public open class DefaultNotificationHandler(
481488
}
482489
}
483490

484-
private fun showNotificationCallNotification(
491+
open fun showNotificationCallNotification(
485492
notificationPendingIntent: PendingIntent,
486493
callDisplayName: String,
487494
notificationId: Int,
@@ -493,7 +500,7 @@ public open class DefaultNotificationHandler(
493500
}
494501
}
495502

496-
private fun showMissedCallNotification(
503+
open fun showMissedCallNotification(
497504
notificationPendingIntent: PendingIntent,
498505
callDisplayName: String,
499506
notificationId: Int,
@@ -504,7 +511,7 @@ public open class DefaultNotificationHandler(
504511
}
505512
}
506513

507-
private fun showLiveCallNotification(
514+
open fun showLiveCallNotification(
508515
liveCallPendingIntent: PendingIntent,
509516
callDisplayName: String,
510517
notificationId: Int,
@@ -525,7 +532,7 @@ public open class DefaultNotificationHandler(
525532
notificationManager.notify(notificationId, notification)
526533
}
527534

528-
private fun getNotification(
535+
open fun getNotification(
529536
builder: NotificationCompat.Builder.() -> Unit,
530537
): Notification {
531538
return NotificationCompat.Builder(application, getChannelId())
@@ -535,7 +542,7 @@ public open class DefaultNotificationHandler(
535542
.build()
536543
}
537544

538-
private fun NotificationCompat.Builder.addHangUpAction(
545+
open fun NotificationCompat.Builder.addHangUpAction(
539546
hangUpIntent: PendingIntent,
540547
callDisplayName: String,
541548
remoteParticipantCount: Int,
@@ -569,17 +576,11 @@ public open class DefaultNotificationHandler(
569576
),
570577
)
571578
} else {
572-
addAction(
573-
NotificationCompat.Action.Builder(
574-
null,
575-
application.getString(R.string.stream_video_call_notification_action_leave),
576-
hangUpIntent,
577-
).build(),
578-
)
579+
addAction(getLeaveAction(hangUpIntent))
579580
}
580581
}
581582

582-
private fun NotificationCompat.Builder.addCallActions(
583+
open fun NotificationCompat.Builder.addCallActions(
583584
acceptCallPendingIntent: PendingIntent,
584585
rejectCallPendingIntent: PendingIntent,
585586
callDisplayName: String?,
@@ -605,24 +606,36 @@ public open class DefaultNotificationHandler(
605606
),
606607
)
607608
} else {
608-
addAction(
609-
NotificationCompat.Action.Builder(
610-
null,
611-
application.getString(R.string.stream_video_call_notification_action_accept),
612-
acceptCallPendingIntent,
613-
).build(),
614-
)
615-
addAction(
616-
NotificationCompat.Action.Builder(
617-
null,
618-
application.getString(R.string.stream_video_call_notification_action_reject),
619-
rejectCallPendingIntent,
620-
).build(),
621-
)
609+
addAction(getAcceptAction(acceptCallPendingIntent))
610+
addAction(getRejectAction(rejectCallPendingIntent))
622611
}
623612
}
624613

625-
private fun isInForeground(): Boolean {
614+
open fun getLeaveAction(intent: PendingIntent): NotificationCompat.Action {
615+
return NotificationCompat.Action.Builder(
616+
null,
617+
application.getString(R.string.stream_video_call_notification_action_leave),
618+
intent,
619+
).build()
620+
}
621+
622+
open fun getAcceptAction(intent: PendingIntent): NotificationCompat.Action {
623+
return NotificationCompat.Action.Builder(
624+
null,
625+
application.getString(R.string.stream_video_call_notification_action_accept),
626+
intent,
627+
).build()
628+
}
629+
630+
open fun getRejectAction(intent: PendingIntent): NotificationCompat.Action {
631+
return NotificationCompat.Action.Builder(
632+
null,
633+
application.getString(R.string.stream_video_call_notification_action_reject),
634+
intent,
635+
).build()
636+
}
637+
638+
open fun isInForeground(): Boolean {
626639
val appProcessInfo = ActivityManager.RunningAppProcessInfo()
627640
ActivityManager.getMyMemoryState(appProcessInfo)
628641
return (

0 commit comments

Comments
 (0)