Use NotificationChannelCompat.

This commit is contained in:
Isira Seneviratne 2022-09-17 15:57:24 +05:30
parent 1ab8e7dec2
commit ad9f975b5f
3 changed files with 32 additions and 46 deletions

View File

@ -9,7 +9,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application <application
android:name=".MyApp" android:name=".LibreTubeApp"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"

View File

@ -1,12 +1,10 @@
package com.github.libretube package com.github.libretube
import android.annotation.SuppressLint
import android.app.Application import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.os.StrictMode import android.os.StrictMode
import android.os.StrictMode.VmPolicy import android.os.StrictMode.VmPolicy
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationManagerCompat
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
import com.github.libretube.api.CronetHelper import com.github.libretube.api.CronetHelper
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
@ -19,12 +17,12 @@ import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NotificationHelper import com.github.libretube.util.NotificationHelper
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
class MyApp : Application() { class LibreTubeApp : Application() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
/** /**
* Initialize the needed [NotificationChannel]s for DownloadService and BackgroundMode * Initialize the needed notification channels for DownloadService and BackgroundMode
*/ */
initializeNotificationChannels() initializeNotificationChannels()
@ -67,47 +65,27 @@ class MyApp : Application() {
} }
/** /**
* Initializes the required [NotificationChannel]s for the app. * Initializes the required notification channels for the app.
*/ */
@SuppressLint("InlinedApi")
private fun initializeNotificationChannels() { private fun initializeNotificationChannels() {
createNotificationChannel( val downloadChannel = NotificationChannelCompat.Builder(DOWNLOAD_CHANNEL_ID,
DOWNLOAD_CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_NONE)
"Download Service", .setName(getString(R.string.download_channel_name))
"Shows a notification when downloading media.", .setDescription(getString(R.string.download_channel_description))
NotificationManager.IMPORTANCE_NONE .build()
) val backgroundChannel = NotificationChannelCompat.Builder(BACKGROUND_CHANNEL_ID,
createNotificationChannel( NotificationManagerCompat.IMPORTANCE_LOW)
BACKGROUND_CHANNEL_ID, .setName(getString(R.string.background_channel_name))
"Background Mode", .setDescription(getString(R.string.background_channel_description))
"Shows a notification with buttons to control the audio player", .build()
NotificationManager.IMPORTANCE_LOW val pushChannel = NotificationChannelCompat.Builder(PUSH_CHANNEL_ID,
) NotificationManagerCompat.IMPORTANCE_DEFAULT)
createNotificationChannel( .setName(getString(R.string.push_channel_name))
PUSH_CHANNEL_ID, .setDescription(getString(R.string.push_channel_description))
"Notification Worker", .build()
"Shows a notification when new streams are available.",
NotificationManager.IMPORTANCE_DEFAULT
)
}
/** val notificationManager = NotificationManagerCompat.from(this)
* Creates a [NotificationChannel] notificationManager.createNotificationChannelsCompat(listOf(downloadChannel,
*/ backgroundChannel, pushChannel))
private fun createNotificationChannel(
id: String,
name: String,
descriptionText: String,
importance: Int
) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(id, name, importance)
channel.description = descriptionText
// Register the channel in the system
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
} }
} }

View File

@ -324,4 +324,12 @@
<string name="skip_segment">Skip segment</string> <string name="skip_segment">Skip segment</string>
<string name="sb_skip_manual">Skip manually</string> <string name="sb_skip_manual">Skip manually</string>
<string name="sb_skip_manual_summary">Don\'t skip segments automatically, always prompt before.</string> <string name="sb_skip_manual_summary">Don\'t skip segments automatically, always prompt before.</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>
<string name="download_channel_description">Shows a notification when downloading media.</string>
<string name="background_channel_name">Background Mode</string>
<string name="background_channel_description">Shows a notification with buttons to control the audio player.</string>
<string name="push_channel_name">Notification Worker</string>
<string name="push_channel_description">Shows a notification when new streams are available.</string>
</resources> </resources>