From 52af05413cba0c50bc82befe57539ca214b94f00 Mon Sep 17 00:00:00 2001 From: Michael Bui <25263378+MaikuB@users.noreply.github.com> Date: Wed, 17 Aug 2022 21:30:54 +1000 Subject: [PATCH] [flutter_local_notifications] add docs on request permission to show notification on Android (#1675) * add docs on request permission to show notification on Android * Google Java Format Co-authored-by: github-actions <> --- flutter_local_notifications/CHANGELOG.md | 4 ++++ flutter_local_notifications/README.md | 11 +++++++++++ flutter_local_notifications/pubspec.yaml | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/flutter_local_notifications/CHANGELOG.md b/flutter_local_notifications/CHANGELOG.md index e34c6e967..d71b2c4dd 100644 --- a/flutter_local_notifications/CHANGELOG.md +++ b/flutter_local_notifications/CHANGELOG.md @@ -1,3 +1,7 @@ +# [9.8.0] + +* [Android] added `requestPermission` method to the `AndroidFlutterLocalNotificationsPlugin` class. This make use of the new feature added to Android 13 where an app can request permissions to show notifications. This isn't a breaking change but requires the Android 13 SDK installed and only apps targeting Android 13 can request the permission. The latter can be done by updating the targetSdkVersion in an app's `build.gradle` file to `33`. Thanks to the PR from [Bartek Pacia](https://github.com/bartekpacia). **Note**: the ability to request the permission as part of calling `initialize` will be added later on + # [9.7.1] * [Android] updated how launch intent is read on Android for the `getNotificationAppLaunchDetails` method diff --git a/flutter_local_notifications/README.md b/flutter_local_notifications/README.md index 360f8e020..bdc95fbe0 100644 --- a/flutter_local_notifications/README.md +++ b/flutter_local_notifications/README.md @@ -21,6 +21,7 @@ A cross platform plugin for displaying local notifications. - **[📷 Screenshots](#-screenshots)** - **[👏 Acknowledgements](#-acknowledgements)** - **[🔧 Android Setup](#-android-setup)** + - [Requesting permissions on Android 13 or higher](#requesting-permissions-on-android-13-or-higher) - [Custom notification icons and sounds](#custom-notification-icons-and-sounds) - [Scheduled notifications](#scheduling-a-notification) - [Fullscreen intent notifications](#full-screen-intent-notifications) @@ -65,6 +66,7 @@ A cross platform plugin for displaying local notifications. * Specify a custom notification sound * Ability to handle when a user has tapped on a notification, when the app is in the foreground, background or is terminated * Determine if an app was launched due to tapping on a notification +* [Android] Request permission to show notifications * [Android] Configuring the importance level * [Android] Configuring the priority * [Android] Customising the vibration pattern for notifications @@ -164,6 +166,15 @@ To respond to notification after the application is terminated, your application Before proceeding, please make sure you are using the latest version of the plugin. The reason for this is that since version 3.0.1+4, the amount of setup needed has been reduced. Previously, applications needed changes done to the `AndroidManifest.xml` file and there was a bit more setup needed for release builds. If for some reason, your application still needs to use an older version of the plugin then make use of the release tags to refer back to older versions of readme. +### Requesting permissions on Android 13 or higher + +From Android 13 (API level 33) onwards, apps now have the ability to display a prompt where users can decide if they want to grant an app permission to show notifications. For further reading on this matter read https://developer.android.com/guide/topics/ui/notifiers/notification-permission. To support this applications need target their application to Android 13 or higher. For example, to target Android 13, update your app's `build.gradle` file to have a `targetSdkVersion` of `33`. Applications can then call the following code to request the permission where the `requestPermission` method is associated with the `AndroidFlutterLocalNotificationsPlugin` class (i.e. the Android implementation of the plugin) + +``` +flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation< + AndroidFlutterLocalNotificationsPlugin>().requestPermission(); +``` + ### Custom notification icons and sounds Notification icons should be added as a drawable resource. The example project/code shows how to set default icon for all notifications and how to specify one for each notification. It is possible to use launcher icon/mipmap and this by default is `@mipmap/ic_launcher` in the Android manifest and can be passed `AndroidInitializationSettings` constructor. However, the offical Android guidance is that you should use drawable resources. Custom notification sounds should be added as a raw resource and the sample illustrates how to play a notification with a custom sound. Refer to the following links around Android resources and notification icons. diff --git a/flutter_local_notifications/pubspec.yaml b/flutter_local_notifications/pubspec.yaml index bec3cf6b4..a696bde20 100644 --- a/flutter_local_notifications/pubspec.yaml +++ b/flutter_local_notifications/pubspec.yaml @@ -2,7 +2,7 @@ name: flutter_local_notifications description: A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform. -version: 9.7.1 +version: 9.8.0 homepage: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications dependencies: