Improve notification grouping for new streams.

This commit is contained in:
Isira Seneviratne 2023-04-11 09:32:06 +05:30
parent 0cce8e4c9b
commit 7bea6fdebd
3 changed files with 60 additions and 49 deletions

View File

@ -384,7 +384,7 @@ class MainActivity : BaseActivity() {
intent?.getStringExtra(IntentData.channelId)?.let { intent?.getStringExtra(IntentData.channelId)?.let {
navController.navigate( navController.navigate(
R.id.channelFragment, R.id.channelFragment,
bundleOf(IntentData.channelName to it) bundleOf(IntentData.channelId to it)
) )
} }
intent?.getStringExtra(IntentData.channelName)?.let { intent?.getStringExtra(IntentData.channelName)?.let {

View File

@ -13,6 +13,7 @@ import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.SubscriptionHelper import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PUSH_CHANNEL_ID import com.github.libretube.constants.PUSH_CHANNEL_ID
@ -118,22 +119,8 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
Log.d(TAG(), "Create notifications for new videos") Log.d(TAG(), "Create notifications for new videos")
// create a notification for each new stream // create a notification for each new stream
channelGroups.forEach { (_, streams) -> channelGroups.forEach { (uploaderUrl, streams) ->
createNotification( createNotificationsForChannel(uploaderUrl!!, streams)
group = streams.first().uploaderUrl!!.toID(),
title = streams.first().uploaderName.toString(),
urlPath = streams.first().uploaderUrl!!,
isGroupSummary = true
)
streams.forEach { streamItem ->
createNotification(
title = streamItem.title.toString(),
description = streamItem.uploaderName.toString(),
urlPath = streamItem.url!!,
group = streamItem.uploaderUrl!!.toID()
)
}
} }
// save the latest streams that got notified about // save the latest streams that got notified about
PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID()) PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID())
@ -142,46 +129,66 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
} }
/** /**
* Notification that is created when new streams are found * Group of notifications created when new streams are found in a given channel.
*
* For more information, see https://developer.android.com/develop/ui/views/notifications/group
*/ */
private fun createNotification( private fun createNotificationsForChannel(group: String, streams: List<StreamItem>) {
title: String, val intentFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or
group: String, Intent.FLAG_ACTIVITY_CLEAR_TASK
urlPath: String,
description: String? = null,
isGroupSummary: Boolean = false
) {
// increase the notification ID to guarantee uniqueness
notificationId += 1
val intent = Intent(applicationContext, MainActivity::class.java).apply { // Create stream notifications. These are automatically grouped on Android 7.0 and later.
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK streams.forEach {
if (isGroupSummary) { val intent = Intent(applicationContext, MainActivity::class.java)
putExtra(IntentData.channelId, urlPath.toID()) .setFlags(intentFlags)
} else { .putExtra(IntentData.videoId, it.url!!.toID())
putExtra(IntentData.videoId, urlPath.toID()) val code = ++notificationId
} val pendingIntent = PendingIntentCompat
.getActivity(applicationContext, code, intent, FLAG_UPDATE_CURRENT, false)
val notification = createNotificationBuilder(group)
.setContentTitle(it.title)
.setContentText(it.uploaderName)
// The intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.build()
notificationManager.notify(code, notification)
} }
val summaryId = ++notificationId
val intent = Intent(applicationContext, MainActivity::class.java)
.setFlags(intentFlags)
.putExtra(IntentData.channelId, group.toID())
val pendingIntent = PendingIntentCompat val pendingIntent = PendingIntentCompat
.getActivity(applicationContext, notificationId, intent, FLAG_UPDATE_CURRENT, false) .getActivity(applicationContext, summaryId, intent, FLAG_UPDATE_CURRENT, false)
val builder = NotificationCompat.Builder(applicationContext, PUSH_CHANNEL_ID) // Create summary notification containing new streams for Android versions below 7.0.
.setContentTitle(title) val newStreams = applicationContext.resources
.setGroup(group) .getQuantityString(R.plurals.channel_new_streams, streams.size, streams.size)
.setSmallIcon(R.drawable.ic_launcher_lockscreen) val summary = NotificationCompat.InboxStyle()
.setPriority(NotificationCompat.PRIORITY_DEFAULT) streams.forEach {
summary.addLine(it.title)
}
val summaryNotification = createNotificationBuilder(group)
.setContentTitle(streams[0].uploaderName)
.setContentText(newStreams)
// The intent that will fire when the user taps the notification // The intent that will fire when the user taps the notification
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.setGroupSummary(true)
.setStyle(summary)
.build()
notificationManager.notify(summaryId, summaryNotification)
}
private fun createNotificationBuilder(group: String): NotificationCompat.Builder {
return NotificationCompat.Builder(applicationContext, PUSH_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_lockscreen)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true) .setAutoCancel(true)
.setGroup(group)
if (isGroupSummary) {
builder.setGroupSummary(true)
} else {
builder.setContentText(description)
}
// [notificationId] is a unique int for each notification that you must define
notificationManager.notify(notificationId, builder.build())
} }
} }

View File

@ -485,4 +485,8 @@
<item quantity="one">%d week ago</item> <item quantity="one">%d week ago</item>
<item quantity="other">%d weeks ago</item> <item quantity="other">%d weeks ago</item>
</plurals> </plurals>
<plurals name="channel_new_streams">
<item quantity="one">%d new stream</item>
<item quantity="other">%d new streams</item>
</plurals>
</resources> </resources>