Merge pull request #3556 from Isira-Seneviratne/Notification_improvements

Improve stream notifications
This commit is contained in:
Isira Seneviratne 2023-04-15 05:17:08 +05:30 committed by GitHub
commit c83761e5ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -137,46 +137,17 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
* For more information, see https://developer.android.com/develop/ui/views/notifications/group
*/
private suspend fun createNotificationsForChannel(group: String, streams: List<StreamItem>) {
val intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
// Create stream notifications. These are automatically grouped on Android 7.0 and later.
if (streams.size == 1) {
showStreamNotification(group, streams[0], true)
} else {
streams.forEach {
val intent = Intent(applicationContext, MainActivity::class.java)
.setFlags(intentFlags)
.putExtra(IntentData.videoId, it.url!!.toID())
val code = ++notificationId
val pendingIntent = PendingIntentCompat
.getActivity(applicationContext, code, intent, FLAG_UPDATE_CURRENT, false)
val notificationBuilder = createNotificationBuilder(group)
.setContentTitle(it.title)
.setContentText(it.uploaderName)
// The intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
// Load stream thumbnails if the relevant toggle is enabled.
if (PreferenceHelper.getBoolean(PreferenceKeys.SHOW_STREAM_THUMBNAILS, false)) {
val thumbnail = withContext(Dispatchers.IO) {
ImageHelper.getImage(applicationContext, it.thumbnail).drawable?.toBitmap()
}
notificationBuilder
.setLargeIcon(thumbnail)
.setStyle(
NotificationCompat.BigPictureStyle()
.bigPicture(thumbnail)
.bigLargeIcon(null as Bitmap?) // Hides the icon when expanding
)
}
notificationManager.notify(code, notificationBuilder.build())
showStreamNotification(group, it, false)
}
val summaryId = ++notificationId
val intent = Intent(applicationContext, MainActivity::class.java)
.setFlags(intentFlags)
.setFlags(INTENT_FLAGS)
.putExtra(IntentData.channelId, group.toID())
val pendingIntent = PendingIntentCompat
@ -196,10 +167,49 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
.setContentIntent(pendingIntent)
.setGroupSummary(true)
.setStyle(summary)
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
.build()
notificationManager.notify(summaryId, summaryNotification)
}
}
private suspend fun showStreamNotification(
group: String,
stream: StreamItem,
isSingleNotification: Boolean
) {
val intent = Intent(applicationContext, MainActivity::class.java)
.setFlags(INTENT_FLAGS)
.putExtra(IntentData.videoId, stream.url!!.toID())
val code = ++notificationId
val pendingIntent = PendingIntentCompat
.getActivity(applicationContext, code, intent, FLAG_UPDATE_CURRENT, false)
val notificationBuilder = createNotificationBuilder(group)
.setContentTitle(stream.title)
.setContentText(stream.uploaderName)
// The intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setSilent(!isSingleNotification)
// Load stream thumbnails if the relevant toggle is enabled.
if (PreferenceHelper.getBoolean(PreferenceKeys.SHOW_STREAM_THUMBNAILS, false)) {
val thumbnail = withContext(Dispatchers.IO) {
ImageHelper.getImage(applicationContext, stream.thumbnail).drawable?.toBitmap()
}
notificationBuilder
.setLargeIcon(thumbnail)
.setStyle(
NotificationCompat.BigPictureStyle()
.bigPicture(thumbnail)
.bigLargeIcon(null as Bitmap?) // Hides the icon when expanding
)
}
notificationManager.notify(code, notificationBuilder.build())
}
private fun createNotificationBuilder(group: String): NotificationCompat.Builder {
return NotificationCompat.Builder(applicationContext, PUSH_CHANNEL_ID)
@ -208,4 +218,9 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
.setAutoCancel(true)
.setGroup(group)
}
companion object {
private const val INTENT_FLAGS = Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
}