mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Fix notifications when not logged in with account
This commit is contained in:
parent
ce8e08576a
commit
1a9a12a1ae
@ -32,7 +32,13 @@ object NotificationHelper {
|
|||||||
).toLong()
|
).toLong()
|
||||||
|
|
||||||
// schedule the work manager request if logged in and notifications enabled
|
// schedule the work manager request if logged in and notifications enabled
|
||||||
if (notificationsEnabled && PreferenceHelper.getToken() != "") {
|
if (!notificationsEnabled) {
|
||||||
|
// cancel the work if notifications are disabled or the user is not logged in
|
||||||
|
WorkManager.getInstance(context)
|
||||||
|
.cancelUniqueWork(NOTIFICATION_WORK_NAME)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// required network type for the work
|
// required network type for the work
|
||||||
val networkType = when (
|
val networkType = when (
|
||||||
PreferenceHelper.getString(PreferenceKeys.REQUIRED_NETWORK, "all")
|
PreferenceHelper.getString(PreferenceKeys.REQUIRED_NETWORK, "all")
|
||||||
@ -65,10 +71,5 @@ object NotificationHelper {
|
|||||||
existingPeriodicWorkPolicy,
|
existingPeriodicWorkPolicy,
|
||||||
notificationWorker
|
notificationWorker
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
// cancel the work if notifications are disabled or the user is not logged in
|
|
||||||
WorkManager.getInstance(context)
|
|
||||||
.cancelUniqueWork(NOTIFICATION_WORK_NAME)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,12 +27,12 @@ import kotlinx.coroutines.runBlocking
|
|||||||
class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
||||||
Worker(appContext, parameters) {
|
Worker(appContext, parameters) {
|
||||||
|
|
||||||
private val notificationManager =
|
private val notificationManager = NotificationManagerCompat.from(appContext)
|
||||||
appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
||||||
|
|
||||||
// the id where notification channels start
|
// the id where notification channels start
|
||||||
private var notificationId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
private var notificationId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
notificationManager.activeNotifications.size + 5
|
val nManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
|
nManager.activeNotifications.size + 5
|
||||||
} else {
|
} else {
|
||||||
5
|
5
|
||||||
}
|
}
|
||||||
@ -93,7 +93,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
|||||||
val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId()
|
val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId()
|
||||||
val latestFeedStreamId = videoFeed.firstOrNull()?.url?.toID() ?: return@runBlocking
|
val latestFeedStreamId = videoFeed.firstOrNull()?.url?.toID() ?: return@runBlocking
|
||||||
|
|
||||||
// first time notifications enabled or no new video available
|
// first time notifications are enabled or no new video available
|
||||||
if (lastSeenStreamId == "" || lastSeenStreamId == latestFeedStreamId) {
|
if (lastSeenStreamId == "" || lastSeenStreamId == latestFeedStreamId) {
|
||||||
PreferenceHelper.setLatestVideoId(lastSeenStreamId)
|
PreferenceHelper.setLatestVideoId(lastSeenStreamId)
|
||||||
return@runBlocking
|
return@runBlocking
|
||||||
@ -117,6 +117,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
|||||||
|
|
||||||
// group the new streams by the uploader
|
// group the new streams by the uploader
|
||||||
val channelGroups = filteredVideos.groupBy { it.uploaderUrl }
|
val channelGroups = filteredVideos.groupBy { it.uploaderUrl }
|
||||||
|
|
||||||
// create a notification for each new stream
|
// create a notification for each new stream
|
||||||
channelGroups.forEach { (_, streams) ->
|
channelGroups.forEach { (_, streams) ->
|
||||||
createNotification(
|
createNotification(
|
||||||
@ -176,9 +177,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
|||||||
builder.setContentText(description)
|
builder.setContentText(description)
|
||||||
}
|
}
|
||||||
|
|
||||||
with(NotificationManagerCompat.from(applicationContext)) {
|
|
||||||
// notificationId is a unique int for each notification that you must define
|
// notificationId is a unique int for each notification that you must define
|
||||||
notify(notificationId, builder.build())
|
notificationManager.notify(notificationId, builder.build())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user