From 38298d9e43a72f5b3d86ccb55d9539f4ccabe3a6 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 11 Jan 2023 19:48:43 +0100 Subject: [PATCH] Open video when clicking feed notification --- .../libretube/ui/activities/MainActivity.kt | 2 ++ .../libretube/util/NotificationHelper.kt | 13 ++++++++- .../libretube/workers/NotificationWorker.kt | 28 ++++++++++++++----- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt index b6fa2bec7..ba9e7a6ee 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/MainActivity.kt @@ -8,6 +8,7 @@ import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Looper +import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View @@ -31,6 +32,7 @@ import com.github.libretube.R import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys import com.github.libretube.databinding.ActivityMainBinding +import com.github.libretube.extensions.TAG import com.github.libretube.extensions.toID import com.github.libretube.services.ClosingService import com.github.libretube.ui.base.BaseActivity diff --git a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt index d54bb77f6..d8931ea57 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt @@ -4,6 +4,7 @@ import android.content.Context import androidx.work.Constraints import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.NetworkType +import androidx.work.OneTimeWorkRequest import androidx.work.PeriodicWorkRequest import androidx.work.WorkManager import com.github.libretube.constants.NOTIFICATION_WORK_NAME @@ -64,12 +65,22 @@ object NotificationHelper { .setConstraints(constraints) .build() - // enqueue the task + // enqueue the task to the work manager instance WorkManager.getInstance(context) .enqueueUniquePeriodicWork( NOTIFICATION_WORK_NAME, existingPeriodicWorkPolicy, notificationWorker ) + + // for testing the notifications by the work manager + /* + WorkManager.getInstance(context) + .enqueue( + OneTimeWorkRequest.Builder(NotificationWorker::class.java) + .setConstraints(constraints) + .build() + ) + */ } } diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index b8d14e773..2bc5fea22 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -5,14 +5,17 @@ import android.app.PendingIntent import android.content.Context import android.content.Intent import android.os.Build +import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.work.Worker import androidx.work.WorkerParameters import com.github.libretube.R import com.github.libretube.api.SubscriptionHelper +import com.github.libretube.constants.IntentData import com.github.libretube.constants.PUSH_CHANNEL_ID import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.extensions.TAG import com.github.libretube.extensions.filterUntil import com.github.libretube.extensions.toID import com.github.libretube.ui.activities.MainActivity @@ -81,6 +84,8 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : private fun checkForNewStreams(): Boolean { var success = true + Log.d(TAG(), "Work manager started") + runBlocking { // fetch the users feed val videoFeed = try { @@ -118,11 +123,14 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : // group the new streams by the uploader val channelGroups = filteredVideos.groupBy { it.uploaderUrl } + Log.d(TAG(), "Create notifications for new videos") + // create a notification for each new stream channelGroups.forEach { (_, streams) -> createNotification( group = streams.first().uploaderUrl!!.toID(), title = streams.first().uploaderName.toString(), + urlPath = streams.first().uploaderUrl!!, isGroupSummary = true ) @@ -130,6 +138,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : createNotification( title = streamItem.title.toString(), description = streamItem.uploaderName.toString(), + urlPath = streamItem.url!!, group = streamItem.uploaderUrl!!.toID() ) } @@ -147,19 +156,24 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : private fun createNotification( title: String, group: String, + 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 { + flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + if (isGroupSummary) putExtra(IntentData.channelId, urlPath.toID()) + else putExtra(IntentData.videoId, urlPath.toID()) + } + val pendingIntent: PendingIntent = PendingIntent.getActivity( applicationContext, - 0, - Intent(applicationContext, MainActivity::class.java).apply { - flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK - }, - PendingIntent.FLAG_IMMUTABLE + notificationId, + intent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) val builder = NotificationCompat.Builder(applicationContext, PUSH_CHANNEL_ID) @@ -167,7 +181,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : .setGroup(group) .setSmallIcon(R.drawable.ic_launcher_lockscreen) .setPriority(NotificationCompat.PRIORITY_DEFAULT) - // Set the intent that will fire when the user taps the notification + // The intent that will fire when the user taps the notification .setContentIntent(pendingIntent) .setAutoCancel(true) @@ -177,7 +191,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : builder.setContentText(description) } - // notificationId is a unique int for each notification that you must define + // [notificationId] is a unique int for each notification that you must define notificationManager.notify(notificationId, builder.build()) } }