From 25e462111b78ef264300ff008e42604177c8f348 Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Sun, 8 Dec 2019 19:29:07 +0100 Subject: [PATCH 1/2] [Notification Visibility][Android] Notification visibility handling --- README.md | 1 + .../FlutterLocalNotificationsPlugin.java | 25 +++++++++++++++++++ .../models/NotificationDetails.java | 4 +++ example/lib/main.dart | 20 +++++++++++++++ lib/src/platform_specifics/android/enums.dart | 10 ++++++++ .../android/notification_details.dart | 10 +++++--- 6 files changed, 67 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 129cbc776..ff4c47a13 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ A cross platform plugin for displaying local notifications. * Messaging * [Android] Group notifications * [Android] Show progress notifications +* [Android] Configure notification visibility on the lockscreen * [iOS] Customise the permissions to be requested around displaying notifications Note that this plugin aims to provide abstractions for all platforms as opposed to having methods that only work on specific platforms. However, each method allows passing in "platform-specifics" that contains data that is specific for customising notifications on each platform. This approach means that some scenarios may not be covered by the plugin. Developers can either fork or maintain their code for showing notifications in these situations. Note that the plugin still under development so expect the API surface to change over time. diff --git a/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java b/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java index 7e1094226..722cbba73 100644 --- a/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java +++ b/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java @@ -136,6 +136,7 @@ public static Notification createNotification(Context context, NotificationDetai builder.setColor(notificationDetails.color.intValue()); } + setVisibility(notificationDetails, builder); applyGrouping(notificationDetails, builder); setSound(context, notificationDetails, builder); setVibrationPattern(notificationDetails, builder); @@ -348,6 +349,30 @@ private static IconCompat getIconFromSource(Context context, String iconPath, Ic return icon; } + private static void setVisibility(NotificationDetails notificationDetails, NotificationCompat.Builder builder) { + if (notificationDetails.visibility == null) { + return; + } + + int visibility; + switch (notificationDetails.visibility) { + case 0: // Private + visibility = NotificationCompat.VISIBILITY_PRIVATE; + break; + case 1: // Public + visibility = NotificationCompat.VISIBILITY_PUBLIC; + break; + case 2: // Secret + visibility = NotificationCompat.VISIBILITY_SECRET; + break; + + default: + System.out.println("FlutterLocalNotificationsPlugin.setVisibility - case not handled: " + notificationDetails.visibility); + return; + } + + builder.setVisibility(visibility); + } private static void applyGrouping(NotificationDetails notificationDetails, NotificationCompat.Builder builder) { Boolean isGrouped = false; diff --git a/android/src/main/java/com/dexterous/flutterlocalnotifications/models/NotificationDetails.java b/android/src/main/java/com/dexterous/flutterlocalnotifications/models/NotificationDetails.java index 8106c0a74..e147891bf 100644 --- a/android/src/main/java/com/dexterous/flutterlocalnotifications/models/NotificationDetails.java +++ b/android/src/main/java/com/dexterous/flutterlocalnotifications/models/NotificationDetails.java @@ -91,6 +91,8 @@ public class NotificationDetails { private static final String LED_ON_MS = "ledOnMs"; private static final String LED_OFF_MS = "ledOffMs"; + private static final String VISIBILITY = "visibility"; + public static final String ID = "id"; public static final String TITLE = "title"; @@ -140,6 +142,7 @@ public class NotificationDetails { public Integer ledOnMs; public Integer ledOffMs; public String ticker; + public Integer visibility; public Boolean allowWhileIdle; @@ -210,6 +213,7 @@ public static NotificationDetails from(Map arguments) { } } notificationDetails.ticker = (String) platformChannelSpecifics.get(TICKER); + notificationDetails.visibility = (Integer) platformChannelSpecifics.get(VISIBILITY); notificationDetails.allowWhileIdle = (Boolean) platformChannelSpecifics.get(ALLOW_WHILE_IDLE); } return notificationDetails; diff --git a/example/lib/main.dart b/example/lib/main.dart index 54968d651..c7ccb4b52 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -176,6 +176,13 @@ class _HomePageState extends State { await _showNotificationWithUpdatedChannelDescription(); }, ), + PaddedRaisedButton( + buttonText: + 'Show plain notification as public on every lockscreen [Android]', + onPressed: () async { + await _showPublicNotification(); + }, + ), PaddedRaisedButton( buttonText: 'Cancel notification', onPressed: () async { @@ -765,6 +772,19 @@ class _HomePageState extends State { payload: 'item x'); } + Future _showPublicNotification() async { + var androidPlatformChannelSpecifics = AndroidNotificationDetails( + 'your channel id', 'your channel name', 'your channel description', + importance: Importance.Max, priority: Priority.High, ticker: 'ticker', + visibility: NotificationVisibility.Public); + var iOSPlatformChannelSpecifics = IOSNotificationDetails(); + var platformChannelSpecifics = NotificationDetails( + androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics); + await flutterLocalNotificationsPlugin.show( + 0, 'public notification title', 'public notification body', platformChannelSpecifics, + payload: 'item x'); + } + String _toTwoDigitString(int value) { return value.toString().padLeft(2, '0'); } diff --git a/lib/src/platform_specifics/android/enums.dart b/lib/src/platform_specifics/android/enums.dart index 5db2441f6..0b8088564 100644 --- a/lib/src/platform_specifics/android/enums.dart +++ b/lib/src/platform_specifics/android/enums.dart @@ -47,3 +47,13 @@ class Priority { /// The available alert behaviours for grouped notifications enum GroupAlertBehavior { All, Summary, Children } + +/// Defines the notification visibility on the lockscreen +enum NotificationVisibility { + /// Show this notification on all lockscreens, but conceal sensitive or private information on secure lockscreens. + Private, + /// Show this notification in its entirety on all lockscreens. + Public, + /// Do not reveal any part of this notification on a secure lockscreen. + Secret, +} diff --git a/lib/src/platform_specifics/android/notification_details.dart b/lib/src/platform_specifics/android/notification_details.dart index 1843dcd5e..48af69c46 100644 --- a/lib/src/platform_specifics/android/notification_details.dart +++ b/lib/src/platform_specifics/android/notification_details.dart @@ -1,6 +1,5 @@ import 'dart:typed_data'; import 'dart:ui'; -import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'enums.dart'; import 'styles/style_information.dart'; @@ -104,6 +103,9 @@ class AndroidNotificationDetails { /// The action to take for managing notification channels. Defaults to creating the notification channel using the provided details if it doesn't exist AndroidNotificationChannelAction channelAction; + /// Defines the notification visibility on the lockscreen + NotificationVisibility visibility; + AndroidNotificationDetails( this.channelId, this.channelName, this.channelDescription, {this.icon, @@ -134,7 +136,8 @@ class AndroidNotificationDetails { this.ledColor, this.ledOnMs, this.ledOffMs, - this.ticker}); + this.ticker, + this.visibility}); Map toMap() { return { @@ -177,7 +180,8 @@ class AndroidNotificationDetails { 'ledColorBlue': ledColor?.blue, 'ledOnMs': ledOnMs, 'ledOffMs': ledOffMs, - 'ticker': ticker + 'ticker': ticker, + 'visibility': visibility?.index }; } } From e919dfa2e5a4faaa909d821b2b7ea43dd0265599 Mon Sep 17 00:00:00 2001 From: gianlucaparadise Date: Wed, 11 Dec 2019 20:02:23 +0100 Subject: [PATCH 2/2] [Notification Visibility][Android] Replaced log with exception as requested --- .../FlutterLocalNotificationsPlugin.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java b/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java index 722cbba73..6eec8ec67 100644 --- a/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java +++ b/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java @@ -349,6 +349,11 @@ private static IconCompat getIconFromSource(Context context, String iconPath, Ic return icon; } + /** + * Sets the visibility property to the input Notification Builder + * @throws IllegalArgumentException If `notificationDetails.visibility` is not null but also + * not matches any known index. + */ private static void setVisibility(NotificationDetails notificationDetails, NotificationCompat.Builder builder) { if (notificationDetails.visibility == null) { return; @@ -367,8 +372,7 @@ private static void setVisibility(NotificationDetails notificationDetails, Notif break; default: - System.out.println("FlutterLocalNotificationsPlugin.setVisibility - case not handled: " + notificationDetails.visibility); - return; + throw new IllegalArgumentException("Unknown index: " + notificationDetails.visibility); } builder.setVisibility(visibility);