From d8ef44674c500d02a2cf7a556d4b0efd8d51f359 Mon Sep 17 00:00:00 2001 From: Nicolas Schneider Date: Fri, 24 Apr 2020 09:40:15 +0200 Subject: [PATCH] add support for `setWhen` on Android notifications Closes #596. --- .../FlutterLocalNotificationsPlugin.java | 3 +++ .../models/NotificationDetails.java | 23 +++++++++++-------- .../android/notification_details.dart | 13 +++++++++++ ...form_flutter_local_notifications_test.dart | 14 +++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) 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 dc0cd484d..6c4b4e167 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 @@ -156,6 +156,9 @@ private static Notification createNotification(Context context, NotificationDeta builder.setShowWhen(BooleanUtils.getValue(notificationDetails.showWhen)); } + if (notificationDetails.when != null) { + builder.setWhen(notificationDetails.when); + } setVisibility(notificationDetails, builder); applyGrouping(notificationDetails, builder); 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 fc3fabde6..1181936b1 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 @@ -102,6 +102,7 @@ public class NotificationDetails { private static final String CATEGORY = "category"; private static final String TIMEOUT_AFTER = "timeoutAfter"; private static final String SHOW_WHEN = "showWhen"; + private static final String WHEN = "when"; public Integer id; @@ -151,6 +152,7 @@ public class NotificationDetails { public Long timeoutAfter; public String category; public Boolean showWhen; + public Long when; @@ -200,6 +202,7 @@ private static void readPlatformSpecifics(Map arguments, Notific readGroupingInformation(notificationDetails, platformChannelSpecifics); notificationDetails.onlyAlertOnce = (Boolean) platformChannelSpecifics.get(ONLY_ALERT_ONCE); notificationDetails.showWhen = (Boolean) platformChannelSpecifics.get(SHOW_WHEN); + notificationDetails.when = parseLong(platformChannelSpecifics.get(WHEN)); readProgressInformation(notificationDetails, platformChannelSpecifics); readColor(notificationDetails, platformChannelSpecifics); readChannelInformation(notificationDetails, platformChannelSpecifics); @@ -208,19 +211,21 @@ private static void readPlatformSpecifics(Map arguments, Notific notificationDetails.ticker = (String) platformChannelSpecifics.get(TICKER); notificationDetails.visibility = (Integer) platformChannelSpecifics.get(VISIBILITY); notificationDetails.allowWhileIdle = (Boolean) platformChannelSpecifics.get(ALLOW_WHILE_IDLE); - Object timeoutAfter = platformChannelSpecifics.get(TIMEOUT_AFTER); - if(timeoutAfter != null) { - if(timeoutAfter instanceof Integer) { - notificationDetails.timeoutAfter = ((Integer) timeoutAfter).longValue(); - } - else if(timeoutAfter instanceof Long) { - notificationDetails.timeoutAfter = (Long) timeoutAfter; - } - } + notificationDetails.timeoutAfter = parseLong(platformChannelSpecifics.get(TIMEOUT_AFTER)); notificationDetails.category = (String) platformChannelSpecifics.get(CATEGORY); } } + private static Long parseLong(Object object) { + if(timeoutAfter instanceof Integer) { + return ((Integer) object).longValue(); + } + if(timeoutAfter instanceof Long) { + return (Long) object; + } + return null; + } + private static void readProgressInformation(NotificationDetails notificationDetails, Map platformChannelSpecifics) { notificationDetails.showProgress = (Boolean) platformChannelSpecifics.get(SHOW_PROGRESS); if (platformChannelSpecifics.containsKey(MAX_PROGRESS)) { 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 75ef755c5..1c662d1fa 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 @@ -35,6 +35,7 @@ class AndroidNotificationDetails { this.largeIcon, this.onlyAlertOnce, this.showWhen = true, + this.when, this.channelShowBadge = true, this.showProgress = false, this.maxProgress = 0, @@ -141,8 +142,19 @@ class AndroidNotificationDetails { final bool onlyAlertOnce; /// Specifies if the notification should display the timestamp of when it occurred. + /// + /// To control the actual timestamp of the notification, use [when]. final bool showWhen; + /// Specifies the timestamp of the notification. + /// + /// If not specified, this will be set to the current time when the + /// notification gets created. + /// + /// To control whether the timestamp is shown in the notification, use + /// [showWhen]. + final int when; + /// Specifies if the notification will be used to show progress. final bool showProgress; @@ -216,6 +228,7 @@ class AndroidNotificationDetails { 'colorBlue': color?.blue, 'onlyAlertOnce': onlyAlertOnce, 'showWhen': showWhen, + 'when': when, 'showProgress': showProgress, 'maxProgress': maxProgress, 'progress': progress, diff --git a/flutter_local_notifications/test/platform_flutter_local_notifications_test.dart b/flutter_local_notifications/test/platform_flutter_local_notifications_test.dart index 6d2a6abf2..86fcb3b65 100644 --- a/flutter_local_notifications/test/platform_flutter_local_notifications_test.dart +++ b/flutter_local_notifications/test/platform_flutter_local_notifications_test.dart @@ -109,6 +109,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -188,6 +189,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -266,6 +268,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -342,6 +345,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -420,6 +424,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -513,6 +518,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -600,6 +606,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -693,6 +700,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -778,6 +786,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -867,6 +876,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -947,6 +957,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -1024,6 +1035,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -1108,6 +1120,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0, @@ -1221,6 +1234,7 @@ void main() { 'colorBlue': null, 'onlyAlertOnce': null, 'showWhen': true, + 'when': null, 'showProgress': false, 'maxProgress': 0, 'progress': 0,