Skip to content

Commit

Permalink
[flutter_local_notification] Add timeoutAfter and category to Android…
Browse files Browse the repository at this point in the history
…NotificationDetails (#445)

* Add timeoutAfter and category to AndroidNotificationDetails

* bump version in pubspec to 1.1.1
  • Loading branch information
MaikuB authored Jan 19, 2020
1 parent 17a8587 commit 2f42734
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 18 deletions.
4 changes: 4 additions & 0 deletions flutter_local_notifications/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [1.1.1]
* [Android] Added ability to specify timeout duration of notification
* [Android] Added ability to specify the notification category

# [1.1.0]
* Updated plugin to make use of `flutter_local_notifications_platform_interface` version 1.0.1. This allows for platform-specific
implementations of the platform interface to now be accessible.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static void rescheduleNotifications(Context context) {
}
}

public static Notification createNotification(Context context, NotificationDetails notificationDetails) {
private static Notification createNotification(Context context, NotificationDetails notificationDetails) {
setupNotificationChannel(context, notificationDetails);
Intent intent = new Intent(context, getMainActivityClass(context));
intent.setAction(SELECT_NOTIFICATION);
Expand Down Expand Up @@ -143,6 +143,8 @@ public static Notification createNotification(Context context, NotificationDetai
setLights(notificationDetails, builder);
setStyle(context, notificationDetails, builder);
setProgress(notificationDetails, builder);
setCategory(notificationDetails, builder);
setTimeoutAfter(notificationDetails, builder);
return builder.build();
}

Expand All @@ -163,7 +165,7 @@ private static void setSmallIcon(Context context, NotificationDetails notificati
}

@NonNull
public static Gson buildGson() {
static Gson buildGson() {
RuntimeTypeAdapterFactory<StyleInformation> styleInformationAdapter =
RuntimeTypeAdapterFactory
.of(StyleInformation.class)
Expand Down Expand Up @@ -206,7 +208,7 @@ public static void registerWith(Registrar registrar) {
FlutterLocalNotificationsPlugin plugin = new FlutterLocalNotificationsPlugin(registrar);
}

public static void removeNotificationFromCache(Integer notificationId, Context context) {
static void removeNotificationFromCache(Integer notificationId, Context context) {
ArrayList<NotificationDetails> scheduledNotifications = loadScheduledNotifications(context);
for (Iterator<NotificationDetails> it = scheduledNotifications.iterator(); it.hasNext(); ) {
NotificationDetails notificationDetails = it.next();
Expand Down Expand Up @@ -419,6 +421,20 @@ private static void setSound(Context context, NotificationDetails notificationDe
}
}

private static void setCategory(NotificationDetails notificationDetails, NotificationCompat.Builder builder) {
if(notificationDetails.category == null) {
return;
}
builder.setCategory(notificationDetails.category);
}

private static void setTimeoutAfter(NotificationDetails notificationDetails, NotificationCompat.Builder builder) {
if(notificationDetails.timeoutAfter == null) {
return;
}
builder.setTimeoutAfter(notificationDetails.timeoutAfter);
}

private static Class getMainActivityClass(Context context) {
String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
Expand All @@ -433,8 +449,6 @@ private static Class getMainActivityClass(Context context) {

private static void setStyle(Context context, NotificationDetails notificationDetails, NotificationCompat.Builder builder) {
switch (notificationDetails.style) {
case Default:
break;
case BigPicture:
setBigPictureStyle(context, notificationDetails, builder);
break;
Expand Down Expand Up @@ -630,7 +644,7 @@ private static boolean isValidDrawableResource(Context context, String name, Res
return true;
}

public static void showNotification(Context context, NotificationDetails notificationDetails) {
static void showNotification(Context context, NotificationDetails notificationDetails) {
Notification notification = createNotification(context, notificationDetails);
NotificationManagerCompat notificationManagerCompat = getNotificationManager(context);
notificationManagerCompat.notify(notificationDetails.id, notification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.util.Map;

public class NotificationDetails {
private static final String ID = "id";
private static final String TITLE = "title";
private static final String BODY = "body";
private static final String PAYLOAD = "payload";
private static final String MILLISECONDS_SINCE_EPOCH = "millisecondsSinceEpoch";
private static final String CALLED_AT = "calledAt";
Expand Down Expand Up @@ -90,16 +93,12 @@ 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";
public static final String BODY = "body";

public static final String TICKER = "ticker";
public static final String ALLOW_WHILE_IDLE = "allowWhileIdle";
private static final String TICKER = "ticker";
private static final String ALLOW_WHILE_IDLE = "allowWhileIdle";
private static final String CATEGORY = "category";
private static final String TIMEOUT_AFTER = "timeoutAfter";

public Integer id;
public String title;
Expand Down Expand Up @@ -144,6 +143,8 @@ public class NotificationDetails {
public String ticker;
public Integer visibility;
public Boolean allowWhileIdle;
public Long timeoutAfter;
public String category;


// Note: this is set on the Android to save details about the icon that should be used when re-hydrating scheduled notifications when a device has been restarted.
Expand Down Expand Up @@ -215,6 +216,16 @@ public static NotificationDetails from(Map<String, Object> arguments) {
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.category = (String) platformChannelSpecifics.get(CATEGORY);
}
return notificationDetails;
}
Expand Down
23 changes: 23 additions & 0 deletions flutter_local_notifications/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ class _HomePageState extends State<HomePage> {
await _showNotificationWithNoSound();
},
),
PaddedRaisedButton(
buttonText:
'Show notification that times out after 3 seconds [Android]',
onPressed: () async {
await _showTimeoutNotification();
},
),
PaddedRaisedButton(
buttonText: 'Show big picture notification [Android]',
onPressed: () async {
Expand Down Expand Up @@ -394,6 +401,21 @@ class _HomePageState extends State<HomePage> {
'<b>silent</b> body', platformChannelSpecifics);
}

Future<void> _showTimeoutNotification() async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'silent channel id',
'silent channel name',
'silent channel description',
timeoutAfter: 3000,
styleInformation: DefaultStyleInformation(true, true));
var iOSPlatformChannelSpecifics =
IOSNotificationDetails(presentSound: false);
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(0, 'timeout notification',
'Times out after 3 seconds', platformChannelSpecifics);
}

Future<String> _downloadAndSaveImage(String url, String fileName) async {
var directory = await getApplicationDocumentsDirectory();
var filePath = '${directory.path}/$fileName';
Expand Down Expand Up @@ -557,6 +579,7 @@ class _HomePageState extends State<HomePage> {
'message channel id',
'message channel name',
'message channel description',
category: 'msg',
style: AndroidNotificationStyle.Messaging,
styleInformation: messagingStyle);
var platformChannelSpecifics =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class AndroidNotificationDetails {
/// Sets how long the light colour will remain off. Not applicable for Android 8.0+
int ledOffMs;

/// Set the "ticker" text which is sent to accessibility services.
/// Set the "ticker" text which is sent to accessibility services
String ticker;

/// The action to take for managing notification channels. Defaults to creating the notification channel using the provided details if it doesn't exist
Expand All @@ -106,6 +106,13 @@ class AndroidNotificationDetails {
/// Defines the notification visibility on the lockscreen
NotificationVisibility visibility;

/// The duration in milliseconds after which the notification will be cancelled if it hasn't already
int timeoutAfter;

/// The notification category. Refer to Android notification API documentation at https://developer.android.com/reference/androidx/core/app/NotificationCompat.html#constants_2
/// for the available categories
String category;

AndroidNotificationDetails(
this.channelId, this.channelName, this.channelDescription,
{this.icon,
Expand Down Expand Up @@ -137,7 +144,9 @@ class AndroidNotificationDetails {
this.ledOnMs,
this.ledOffMs,
this.ticker,
this.visibility});
this.visibility,
this.timeoutAfter,
this.category});

Map<String, dynamic> toMap() {
return <String, dynamic>{
Expand Down Expand Up @@ -181,7 +190,9 @@ class AndroidNotificationDetails {
'ledOnMs': ledOnMs,
'ledOffMs': ledOffMs,
'ticker': ticker,
'visibility': visibility?.index
'visibility': visibility?.index,
'timeoutAfter': timeoutAfter,
'category': category
};
}
}
2 changes: 1 addition & 1 deletion flutter_local_notifications/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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: 1.1.0
version: 1.1.1
homepage: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications

dependencies:
Expand Down

0 comments on commit 2f42734

Please sign in to comment.