Fix notifications when not logged in with account

This commit is contained in:
Bnyro 2023-01-08 17:05:18 +01:00
parent ce8e08576a
commit 1a9a12a1ae
2 changed files with 42 additions and 42 deletions

View File

@ -32,7 +32,13 @@ object NotificationHelper {
).toLong()
// 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
val networkType = when (
PreferenceHelper.getString(PreferenceKeys.REQUIRED_NETWORK, "all")
@ -65,10 +71,5 @@ object NotificationHelper {
existingPeriodicWorkPolicy,
notificationWorker
)
} else {
// cancel the work if notifications are disabled or the user is not logged in
WorkManager.getInstance(context)
.cancelUniqueWork(NOTIFICATION_WORK_NAME)
}
}
}

View File

@ -27,12 +27,12 @@ import kotlinx.coroutines.runBlocking
class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
Worker(appContext, parameters) {
private val notificationManager =
appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val notificationManager = NotificationManagerCompat.from(appContext)
// the id where notification channels start
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 {
5
}
@ -93,7 +93,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
val lastSeenStreamId = PreferenceHelper.getLastSeenVideoId()
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) {
PreferenceHelper.setLatestVideoId(lastSeenStreamId)
return@runBlocking
@ -117,6 +117,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
// group the new streams by the uploader
val channelGroups = filteredVideos.groupBy { it.uploaderUrl }
// create a notification for each new stream
channelGroups.forEach { (_, streams) ->
createNotification(
@ -176,9 +177,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
builder.setContentText(description)
}
with(NotificationManagerCompat.from(applicationContext)) {
// notificationId is a unique int for each notification that you must define
notify(notificationId, builder.build())
}
notificationManager.notify(notificationId, builder.build())
}
}