From 04ae2cb91d3eef9ad9baa9e15cd5e431c2e0a516 Mon Sep 17 00:00:00 2001 From: Aayush Gupta Date: Wed, 19 Apr 2023 23:39:38 +0530 Subject: [PATCH] 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 --- .../com/aurora/store/AuroraApplication.kt | 39 ++++++++++++++++++- .../store/data/service/NotificationService.kt | 33 +--------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/aurora/store/AuroraApplication.kt b/app/src/main/java/com/aurora/store/AuroraApplication.kt index f4196dcbd..89bf57e69 100644 --- a/app/src/main/java/com/aurora/store/AuroraApplication.kt +++ b/app/src/main/java/com/aurora/store/AuroraApplication.kt @@ -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() } -} \ No newline at end of file + + private fun createNotificationChannel() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager + val channels = ArrayList() + 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) + } + } +} diff --git a/app/src/main/java/com/aurora/store/data/service/NotificationService.kt b/app/src/main/java/com/aurora/store/data/service/NotificationService.kt index 3fa6fcf3f..fda9f1884 100644 --- a/app/src/main/java/com/aurora/store/data/service/NotificationService.kt +++ b/app/src/main/java/com/aurora/store/data/service/NotificationService.kt @@ -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() - 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() } -} \ No newline at end of file +}