From a78100c0ea1490f9223fa911319ce1dd2f761ace Mon Sep 17 00:00:00 2001 From: Prathamesh More Date: Sun, 28 Nov 2021 13:37:48 +0530 Subject: [PATCH] Fixed ForegroundServiceStartNotAllowedException when song ended on Android 12. This was due to Android 12's foreground service restrictions. --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 1 + .../service/notification/PlayingNotification.kt | 11 ++++++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f3b25e483..76c3b034f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,7 +9,7 @@ android { defaultConfig { minSdkVersion 21 - targetSdkVersion 30 + targetSdkVersion 31 renderscriptTargetApi 29//must match target sdk and build tools vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bf69e2ee1..e785fea33 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -262,6 +262,7 @@ android:enabled="true" android:exported="true" android:label="@string/app_name" + android:foregroundServiceType="mediaPlayback" tools:ignore="ExportedService"> diff --git a/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotification.kt b/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotification.kt index 4d0476ca0..5557a285e 100644 --- a/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotification.kt +++ b/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotification.kt @@ -19,6 +19,7 @@ import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context.NOTIFICATION_SERVICE +import android.content.pm.ServiceInfo import android.os.Build import androidx.annotation.RequiresApi import code.name.monkey.retromusic.R @@ -62,7 +63,15 @@ abstract class PlayingNotification { } if (newNotifyMode == NOTIFY_MODE_FOREGROUND) { - service.startForeground(NOTIFICATION_ID, notification) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + service.startForeground( + NOTIFICATION_ID, + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK + ) + } else { + service.startForeground(NOTIFICATION_ID,notification) + } } else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) { notificationManager!!.notify(NOTIFICATION_ID, notification) }