Skip to content

Commit

Permalink
Merge pull request #388 from gianlucaparadise/feature/android-visibility
Browse files Browse the repository at this point in the history
Added notification visibility (Android only)
  • Loading branch information
MaikuB authored Dec 13, 2019
2 parents cf2c249 + e919dfa commit 5286b41
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -348,6 +349,34 @@ 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;
}

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:
throw new IllegalArgumentException("Unknown index: " + notificationDetails.visibility);
}

builder.setVisibility(visibility);
}

private static void applyGrouping(NotificationDetails notificationDetails, NotificationCompat.Builder builder) {
Boolean isGrouped = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -140,6 +142,7 @@ public class NotificationDetails {
public Integer ledOnMs;
public Integer ledOffMs;
public String ticker;
public Integer visibility;
public Boolean allowWhileIdle;


Expand Down Expand Up @@ -210,6 +213,7 @@ 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);
}
return notificationDetails;
Expand Down
20 changes: 20 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ class _HomePageState extends State<HomePage> {
await _showNotificationWithUpdatedChannelDescription();
},
),
PaddedRaisedButton(
buttonText:
'Show plain notification as public on every lockscreen [Android]',
onPressed: () async {
await _showPublicNotification();
},
),
PaddedRaisedButton(
buttonText: 'Cancel notification',
onPressed: () async {
Expand Down Expand Up @@ -765,6 +772,19 @@ class _HomePageState extends State<HomePage> {
payload: 'item x');
}

Future<void> _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');
}
Expand Down
10 changes: 10 additions & 0 deletions lib/src/platform_specifics/android/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
10 changes: 7 additions & 3 deletions lib/src/platform_specifics/android/notification_details.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -134,7 +136,8 @@ class AndroidNotificationDetails {
this.ledColor,
this.ledOnMs,
this.ledOffMs,
this.ticker});
this.ticker,
this.visibility});

Map<String, dynamic> toMap() {
return <String, dynamic>{
Expand Down Expand Up @@ -177,7 +180,8 @@ class AndroidNotificationDetails {
'ledColorBlue': ledColor?.blue,
'ledOnMs': ledOnMs,
'ledOffMs': ledOffMs,
'ticker': ticker
'ticker': ticker,
'visibility': visibility?.index
};
}
}

0 comments on commit 5286b41

Please sign in to comment.