From 88a7bd5b7438a62b6e27910b014d2c920f996e75 Mon Sep 17 00:00:00 2001 From: Guillaume BOEHM Date: Tue, 6 Sep 2022 17:29:05 +0200 Subject: [PATCH] Avoid permission request on launch on Android 13 --- .../com/example/quick_notify/QuickNotifyPlugin.kt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/quick_notify/android/src/main/kotlin/com/example/quick_notify/QuickNotifyPlugin.kt b/packages/quick_notify/android/src/main/kotlin/com/example/quick_notify/QuickNotifyPlugin.kt index 658a52a..d65f750 100644 --- a/packages/quick_notify/android/src/main/kotlin/com/example/quick_notify/QuickNotifyPlugin.kt +++ b/packages/quick_notify/android/src/main/kotlin/com/example/quick_notify/QuickNotifyPlugin.kt @@ -24,20 +24,27 @@ class QuickNotifyPlugin: FlutterPlugin, MethodCallHandler { private lateinit var notificationManager: NotificationManager + private final var channelCreated: Boolean = false + private val channelProps = object { var id = "quick_notify" var name = "quick_notify" var importance = NotificationManager.IMPORTANCE_DEFAULT } + private fun createChannel(){ + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ + notificationManager.createNotificationChannel(NotificationChannel(channelProps.id, channelProps.name, channelProps.importance)) + channelCreated = true + } + } + override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { channel = MethodChannel(flutterPluginBinding.binaryMessenger, "quick_notify") channel.setMethodCallHandler(this) applicationContext = flutterPluginBinding.applicationContext notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - notificationManager.createNotificationChannel(NotificationChannel(channelProps.id, channelProps.name, channelProps.importance)) - } + if (notificationManager.areNotificationsEnabled()) createChannel() } override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) { @@ -56,6 +63,8 @@ class QuickNotifyPlugin: FlutterPlugin, MethodCallHandler { result.notImplemented() } } else if (call.method == "notify") { + if(!channelCreated) createChannel() + val args = call.arguments as Map val title = args["title"] as String val content = args["content"] as String