Create notification channels prior to starting service

We don't need notification permission to start services however
channels must still exist and notification must be sent regardless.

Trying to do this in service results in crash, thus do it before
service is triggered.

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2023-04-19 23:39:38 +05:30
parent c4f84bd0c9
commit 04ae2cb91d
2 changed files with 39 additions and 33 deletions

View File

@@ -19,8 +19,12 @@
package com.aurora.store
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.multidex.MultiDexApplication
import com.aurora.Constants
import com.aurora.store.data.downloader.DownloadManager
import com.aurora.store.data.providers.NetworkProvider
import com.aurora.store.data.receiver.PackageManagerReceiver
@@ -28,6 +32,7 @@ import com.aurora.store.data.service.NotificationService
import com.aurora.store.util.CommonUtil
import com.aurora.store.util.PackageUtil
import com.tonyodev.fetch2.Fetch
import java.util.ArrayList
import nl.komponents.kovenant.android.startKovenant
import nl.komponents.kovenant.android.stopKovenant
@@ -45,6 +50,9 @@ class AuroraApplication : MultiDexApplication() {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
//Create Notification Channels : General & Alert
createNotificationChannel()
NotificationService.startService(this)
fetch = DownloadManager.with(this).fetch
@@ -79,4 +87,33 @@ class AuroraApplication : MultiDexApplication() {
.unbind()
super.onLowMemory()
}
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val channels = ArrayList<NotificationChannel>()
channels.add(
NotificationChannel(
Constants.NOTIFICATION_CHANNEL_ALERT,
getString(R.string.notification_channel_alert),
NotificationManager.IMPORTANCE_HIGH
)
)
channels.add(
NotificationChannel(
Constants.NOTIFICATION_CHANNEL_GENERAL,
getString(R.string.notification_channel_general),
NotificationManager.IMPORTANCE_MIN
)
)
channels.add(
NotificationChannel(
Constants.NOTIFICATION_CHANNEL_UPDATER_SERVICE,
getString(R.string.notification_channel_updater_service),
NotificationManager.IMPORTANCE_MIN
)
)
notificationManager.createNotificationChannels(channels)
}
}
}

View File

@@ -92,9 +92,6 @@ class NotificationService : Service() {
notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
//Create Notification Channels : General & Alert
createNotificationChannel()
fetch = DownloadManager.with(this).fetch
fetchListener = object : AbstractFetchGroupListener() {
@@ -147,34 +144,6 @@ class NotificationService : Service() {
fetch.addListener(fetchListener)
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channels = ArrayList<NotificationChannel>()
channels.add(
NotificationChannel(
Constants.NOTIFICATION_CHANNEL_ALERT,
getString(R.string.notification_channel_alert),
NotificationManager.IMPORTANCE_HIGH
)
)
channels.add(
NotificationChannel(
Constants.NOTIFICATION_CHANNEL_GENERAL,
getString(R.string.notification_channel_general),
NotificationManager.IMPORTANCE_MIN
)
)
channels.add(
NotificationChannel(
Constants.NOTIFICATION_CHANNEL_UPDATER_SERVICE,
getString(R.string.notification_channel_updater_service),
NotificationManager.IMPORTANCE_MIN
)
)
notificationManager.createNotificationChannels(channels)
}
}
private fun showNotification(groupId: Int, download: Download, fetchGroup: FetchGroup) {
val status = download.status
@@ -466,4 +435,4 @@ class NotificationService : Service() {
EventBus.getDefault().unregister(this);
super.onDestroy()
}
}
}