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,43 +32,44 @@ 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) {
// required network type for the work
val networkType = when (
PreferenceHelper.getString(PreferenceKeys.REQUIRED_NETWORK, "all")
) {
"all" -> NetworkType.CONNECTED
"wifi" -> NetworkType.UNMETERED
"metered" -> NetworkType.METERED
else -> NetworkType.CONNECTED
}
// requirements for the work
// here: network needed to run the task
val constraints = Constraints.Builder()
.setRequiredNetworkType(networkType)
.build()
// create the worker
val notificationWorker = PeriodicWorkRequest.Builder(
NotificationWorker::class.java,
checkingFrequency,
TimeUnit.MINUTES
)
.setConstraints(constraints)
.build()
// enqueue the task
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(
NOTIFICATION_WORK_NAME,
existingPeriodicWorkPolicy,
notificationWorker
)
} else {
// cancel the work if notifications are disabled or the user is not logged in // cancel the work if notifications are disabled or the user is not logged in
WorkManager.getInstance(context) WorkManager.getInstance(context)
.cancelUniqueWork(NOTIFICATION_WORK_NAME) .cancelUniqueWork(NOTIFICATION_WORK_NAME)
return
} }
// required network type for the work
val networkType = when (
PreferenceHelper.getString(PreferenceKeys.REQUIRED_NETWORK, "all")
) {
"all" -> NetworkType.CONNECTED
"wifi" -> NetworkType.UNMETERED
"metered" -> NetworkType.METERED
else -> NetworkType.CONNECTED
}
// requirements for the work
// here: network needed to run the task
val constraints = Constraints.Builder()
.setRequiredNetworkType(networkType)
.build()
// create the worker
val notificationWorker = PeriodicWorkRequest.Builder(
NotificationWorker::class.java,
checkingFrequency,
TimeUnit.MINUTES
)
.setConstraints(constraints)
.build()
// enqueue the task
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(
NOTIFICATION_WORK_NAME,
existingPeriodicWorkPolicy,
notificationWorker
)
} }
} }

View File

@ -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 notificationManager.notify(notificationId, builder.build())
notify(notificationId, builder.build())
}
} }
} }