From 5e444a61f244d9544b529ba9c3ac8e457eb195cc Mon Sep 17 00:00:00 2001 From: Tran Leo Date: Wed, 29 May 2024 17:13:22 +0700 Subject: [PATCH] Add "selectable" property for AndroidNotificationDetails to disable click actions on the notification --- .../FlutterLocalNotificationsPlugin.java | 5 +++- .../models/NotificationDetails.java | 3 +++ .../android/method_channel_mappers.dart | 1 + .../android/notification_details.dart | 9 +++++++ ...roid_flutter_local_notifications_test.dart | 24 +++++++++++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java index 409a26a50..1056bc77f 100644 --- a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java +++ b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java @@ -274,7 +274,10 @@ protected static Notification createNotification( : notificationDetails.body) .setTicker(notificationDetails.ticker) .setAutoCancel(BooleanUtils.getValue(notificationDetails.autoCancel)) - .setContentIntent(pendingIntent) + .setContentIntent( + BooleanUtils.getValue(notificationDetails.selectable) + ? pendingIntent + : null) .setPriority(notificationDetails.priority) .setOngoing(BooleanUtils.getValue(notificationDetails.ongoing)) .setSilent(BooleanUtils.getValue(notificationDetails.silent)) diff --git a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/models/NotificationDetails.java b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/models/NotificationDetails.java index a5e72f52a..1d6eed4eb 100644 --- a/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/models/NotificationDetails.java +++ b/flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/models/NotificationDetails.java @@ -33,6 +33,7 @@ public class NotificationDetails implements Serializable { private static final String REPEAT_TIME = "repeatTime"; private static final String PLATFORM_SPECIFICS = "platformSpecifics"; private static final String AUTO_CANCEL = "autoCancel"; + private static final String SELECTABLE = "selectable"; private static final String ONGOING = "ongoing"; private static final String SILENT = "silent"; private static final String STYLE = "style"; @@ -153,6 +154,7 @@ public class NotificationDetails implements Serializable { public Boolean setAsGroupSummary; public Integer groupAlertBehavior; public Boolean autoCancel; + public Boolean selectable; public Boolean ongoing; public Boolean silent; public Integer day; @@ -246,6 +248,7 @@ private static void readPlatformSpecifics( (Map) arguments.get(PLATFORM_SPECIFICS); if (platformChannelSpecifics != null) { notificationDetails.autoCancel = (Boolean) platformChannelSpecifics.get(AUTO_CANCEL); + notificationDetails.selectable = (Boolean) platformChannelSpecifics.get(SELECTABLE); notificationDetails.ongoing = (Boolean) platformChannelSpecifics.get(ONGOING); notificationDetails.silent = (Boolean) platformChannelSpecifics.get(SILENT); notificationDetails.style = diff --git a/flutter_local_notifications/lib/src/platform_specifics/android/method_channel_mappers.dart b/flutter_local_notifications/lib/src/platform_specifics/android/method_channel_mappers.dart index cc1fa1994..d8485740f 100644 --- a/flutter_local_notifications/lib/src/platform_specifics/android/method_channel_mappers.dart +++ b/flutter_local_notifications/lib/src/platform_specifics/android/method_channel_mappers.dart @@ -188,6 +188,7 @@ extension AndroidNotificationDetailsMapper on AndroidNotificationDetails { 'setAsGroupSummary': setAsGroupSummary, 'groupAlertBehavior': groupAlertBehavior.index, 'autoCancel': autoCancel, + 'selectable': selectable, 'ongoing': ongoing, 'silent': silent, 'colorAlpha': color?.alpha, diff --git a/flutter_local_notifications/lib/src/platform_specifics/android/notification_details.dart b/flutter_local_notifications/lib/src/platform_specifics/android/notification_details.dart index 820395fa4..72292d6e9 100644 --- a/flutter_local_notifications/lib/src/platform_specifics/android/notification_details.dart +++ b/flutter_local_notifications/lib/src/platform_specifics/android/notification_details.dart @@ -114,6 +114,7 @@ class AndroidNotificationDetails { this.setAsGroupSummary = false, this.groupAlertBehavior = GroupAlertBehavior.all, this.autoCancel = true, + this.selectable = true, this.ongoing = false, this.silent = false, this.color, @@ -237,6 +238,14 @@ class AndroidNotificationDetails { /// Specifies if the notification should automatically dismissed upon tapping /// on it. final bool autoCancel; + + /// Specifies if the notification is selectable. + /// + /// If `selectable` is `false`, the `contentIntent` will be set to `null`, + /// disabling click actions on the notification. + /// + /// The default value is `true`. + final bool selectable; /// Specifies if the notification will be "ongoing". final bool ongoing; diff --git a/flutter_local_notifications/test/android_flutter_local_notifications_test.dart b/flutter_local_notifications/test/android_flutter_local_notifications_test.dart index a833d6dfd..0b711f6a8 100644 --- a/flutter_local_notifications/test/android_flutter_local_notifications_test.dart +++ b/flutter_local_notifications/test/android_flutter_local_notifications_test.dart @@ -140,6 +140,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -262,6 +263,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -346,6 +348,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -431,6 +434,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -517,6 +521,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -607,6 +612,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -696,6 +702,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -783,6 +790,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': true, 'colorAlpha': null, @@ -871,6 +879,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -961,6 +970,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1066,6 +1076,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1165,6 +1176,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1270,6 +1282,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1367,6 +1380,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1468,6 +1482,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1560,6 +1575,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1649,6 +1665,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1745,6 +1762,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1870,6 +1888,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -1983,6 +2002,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -2081,6 +2101,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -2178,6 +2199,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -2276,6 +2298,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': null, @@ -2576,6 +2599,7 @@ void main() { 'setAsGroupSummary': false, 'groupAlertBehavior': GroupAlertBehavior.all.index, 'autoCancel': true, + 'selectable': true, 'ongoing': false, 'silent': false, 'colorAlpha': 255,