From b942e4a88b9034260f62ac63d882017102a26841 Mon Sep 17 00:00:00 2001 From: Akos Putz Date: Thu, 8 Jul 2021 17:03:05 -0400 Subject: [PATCH] Saving notification configuration to shared preferences, preventing the notification from resetting to the default text after an app auto-restart --- .../FlutterBackgroundPlugin.kt | 45 ++++++++++++++++--- .../IsolateHolderService.kt | 1 + 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/android/src/main/kotlin/de/julianassmann/flutter_background/FlutterBackgroundPlugin.kt b/android/src/main/kotlin/de/julianassmann/flutter_background/FlutterBackgroundPlugin.kt index 73805cd..5ca05d2 100644 --- a/android/src/main/kotlin/de/julianassmann/flutter_background/FlutterBackgroundPlugin.kt +++ b/android/src/main/kotlin/de/julianassmann/flutter_background/FlutterBackgroundPlugin.kt @@ -35,16 +35,47 @@ class FlutterBackgroundPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { @JvmStatic var notificationTitle: String? = "flutter_background foreground service" @JvmStatic + val NOTIFICATION_TITLE_KEY = "android.notificationTitle" + @JvmStatic var notificationText: String? = "Keeps the flutter app running in the background" @JvmStatic + val NOTIFICATION_TEXT_KEY = "android.notificationText" + @JvmStatic var notificationImportance: Int? = NotificationCompat.PRIORITY_DEFAULT - + @JvmStatic + val NOTIFICATION_IMPORTANCE_KEY = "android.notificationImportance" @JvmStatic var notificationIconName: String? = "ic_launcher" @JvmStatic + val NOTIFICATION_ICON_NAME_KEY = "android.notificationIconName" + @JvmStatic var notificationIconDefType: String? = "mipmap" + @JvmStatic + val NOTIFICATION_ICON_DEF_TYPE_KEY = "android.notificationIconDefType" + + fun loadNotificationConfiguration(context: Context?) { + var sharedPref = context?.getSharedPreferences(context?.packageName + "_preferences", Context.MODE_PRIVATE) + notificationTitle = sharedPref?.getString(NOTIFICATION_TITLE_KEY, notificationTitle) + notificationText = sharedPref?.getString(NOTIFICATION_TEXT_KEY, notificationText) + notificationImportance = sharedPref?.getInt(NOTIFICATION_IMPORTANCE_KEY, notificationImportance!!) + notificationIconName = sharedPref?.getString(NOTIFICATION_ICON_NAME_KEY, notificationIconName) + notificationIconDefType = sharedPref?.getString(NOTIFICATION_ICON_DEF_TYPE_KEY, notificationIconDefType) + } + + fun saveNotificationConfiguration(context: Context?) { + var sharedPref = context?.getSharedPreferences(context?.packageName + "_preferences", Context.MODE_PRIVATE) + with (sharedPref?.edit()) { + this?.putString(NOTIFICATION_TITLE_KEY, notificationTitle) + this?.putString(NOTIFICATION_TEXT_KEY, notificationText) + this?.putInt(NOTIFICATION_IMPORTANCE_KEY, notificationImportance!!) + this?.putString(NOTIFICATION_ICON_NAME_KEY, notificationIconName) + this?.putString(NOTIFICATION_ICON_DEF_TYPE_KEY, notificationIconDefType) + this?.apply() + } + } } + private fun isValidResource(context: Context, name: String, defType: String, result: Result, errorCode: String): Boolean { val resourceId = context.getResources().getIdentifier(name, defType, context.getPackageName()) if (resourceId == 0) { @@ -69,11 +100,11 @@ class FlutterBackgroundPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { result.success(hasPermissions) } "initialize" -> { - val title = call.argument("android.notificationTitle") - val text = call.argument("android.notificationText") - val importance = call.argument("android.notificationImportance") - val iconName = call.argument("android.notificationIconName") - val iconDefType = call.argument("android.notificationIconDefType") + val title = call.argument(NOTIFICATION_TITLE_KEY) + val text = call.argument(NOTIFICATION_TEXT_KEY) + val importance = call.argument(NOTIFICATION_IMPORTANCE_KEY) + val iconName = call.argument(NOTIFICATION_ICON_NAME_KEY) + val iconDefType = call.argument(NOTIFICATION_ICON_DEF_TYPE_KEY) // Set static values so the IsolateHolderService can use them later on to configure the notification notificationImportance = importance ?: notificationImportance @@ -82,6 +113,8 @@ class FlutterBackgroundPlugin: FlutterPlugin, MethodCallHandler, ActivityAware { notificationIconName = iconName ?: notificationIconName notificationIconDefType = iconDefType ?: notificationIconDefType + saveNotificationConfiguration(context) + if (permissionHandler!!.isWakeLockPermissionGranted() && permissionHandler!!.isIgnoringBatteryOptimizations()) { result.success(true) return diff --git a/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt b/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt index 1e5758a..9f456c8 100644 --- a/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt +++ b/android/src/main/kotlin/de/julianassmann/flutter_background/IsolateHolderService.kt @@ -42,6 +42,7 @@ class IsolateHolderService : Service() { @SuppressLint("WakelockTimeout") override fun onCreate() { + FlutterBackgroundPlugin.loadNotificationConfiguration(applicationContext) val pm = getApplicationContext().getPackageManager() val notificationIntent =