Skip to content

Commit

Permalink
[flutter_local_notifications] added ability to specify silent notific…
Browse files Browse the repository at this point in the history
…ation through AndroidNotificationDetails (#2102)

* setSilent notification based on playSound argument

* Add silent option to AndroidNotificationDetails

* Add test for default AndroidDetails and silent enabled

* Add button with silent AndroidNotificationDetails

* Reorder "silent" field to be consistently after "ongoing"

---------

Co-authored-by: Michael Bui <25263378+MaikuB@users.noreply.github.com>
  • Loading branch information
aa-euclidk and MaikuB committed Nov 18, 2023
1 parent 06aa1e4 commit 9d8c793
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ protected static Notification createNotification(
.setContentIntent(pendingIntent)
.setPriority(notificationDetails.priority)
.setOngoing(BooleanUtils.getValue(notificationDetails.ongoing))
.setSilent(BooleanUtils.getValue(notificationDetails.silent))
.setOnlyAlertOnce(BooleanUtils.getValue(notificationDetails.onlyAlertOnce));

if (notificationDetails.actions != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class NotificationDetails implements Serializable {
private static final String PLATFORM_SPECIFICS = "platformSpecifics";
private static final String AUTO_CANCEL = "autoCancel";
private static final String ONGOING = "ongoing";
private static final String SILENT = "silent";
private static final String STYLE = "style";
private static final String ICON = "icon";
private static final String PRIORITY = "priority";
Expand Down Expand Up @@ -153,6 +154,7 @@ public class NotificationDetails implements Serializable {
public Integer groupAlertBehavior;
public Boolean autoCancel;
public Boolean ongoing;
public Boolean silent;
public Integer day;
public Integer color;
public Object largeIcon;
Expand Down Expand Up @@ -245,6 +247,7 @@ private static void readPlatformSpecifics(
if (platformChannelSpecifics != null) {
notificationDetails.autoCancel = (Boolean) platformChannelSpecifics.get(AUTO_CANCEL);
notificationDetails.ongoing = (Boolean) platformChannelSpecifics.get(ONGOING);
notificationDetails.silent = (Boolean) platformChannelSpecifics.get(SILENT);
notificationDetails.style =
NotificationStyle.values()[(Integer) platformChannelSpecifics.get(STYLE)];
readStyleInformation(notificationDetails, platformChannelSpecifics);
Expand Down
29 changes: 28 additions & 1 deletion flutter_local_notifications/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,18 @@ class _HomePageState extends State<HomePage> {
},
),
PaddedElevatedButton(
buttonText: 'Show notification with no sound',
buttonText: 'Show notification from silent channel',
onPressed: () async {
await _showNotificationWithNoSound();
},
),
PaddedElevatedButton(
buttonText:
'Show silent notification from channel with sound',
onPressed: () async {
await _showNotificationSilently();
},
),
PaddedElevatedButton(
buttonText: 'Cancel latest notification',
onPressed: () async {
Expand Down Expand Up @@ -1364,6 +1371,26 @@ class _HomePageState extends State<HomePage> {
id++, '<b>silent</b> title', '<b>silent</b> body', notificationDetails);
}

Future<void> _showNotificationSilently() async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('your channel id', 'your channel name',
channelDescription: 'your channel description',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker',
silent: true);
const DarwinNotificationDetails darwinNotificationDetails =
DarwinNotificationDetails(
presentSound: false,
);
const NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails,
iOS: darwinNotificationDetails,
macOS: darwinNotificationDetails);
await flutterLocalNotificationsPlugin.show(
id++, '<b>silent</b> title', '<b>silent</b> body', notificationDetails);
}

Future<void> _showSoundUriNotification() async {
/// this calls a method over a platform channel implemented within the
/// example app to return the Uri for the default alarm sound and uses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ extension AndroidNotificationDetailsMapper on AndroidNotificationDetails {
'groupAlertBehavior': groupAlertBehavior.index,
'autoCancel': autoCancel,
'ongoing': ongoing,
'silent': silent,
'colorAlpha': color?.alpha,
'colorRed': color?.red,
'colorGreen': color?.green,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class AndroidNotificationDetails {
this.groupAlertBehavior = GroupAlertBehavior.all,
this.autoCancel = true,
this.ongoing = false,
this.silent = false,
this.color,
this.largeIcon,
this.onlyAlertOnce = false,
Expand Down Expand Up @@ -240,6 +241,9 @@ class AndroidNotificationDetails {
/// Specifies if the notification will be "ongoing".
final bool ongoing;

/// Specifies if the notification will be "silent".
final bool silent;

/// Specifies the color.
final Color? color;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -262,6 +263,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -345,6 +347,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -429,6 +432,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -514,6 +518,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -603,6 +608,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -691,6 +697,94 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
'colorBlue': null,
'onlyAlertOnce': false,
'showWhen': true,
'when': null,
'usesChronometer': false,
'chronometerCountDown': false,
'showProgress': false,
'maxProgress': 0,
'progress': 0,
'indeterminate': false,
'enableLights': false,
'ledColorAlpha': null,
'ledColorRed': null,
'ledColorGreen': null,
'ledColorBlue': null,
'ledOnMs': null,
'ledOffMs': null,
'ticker': null,
'visibility': null,
'timeoutAfter': null,
'category': null,
'additionalFlags': null,
'fullScreenIntent': false,
'shortcutId': null,
'subText': null,
'style': AndroidNotificationStyle.defaultStyle.index,
'styleInformation': <String, Object>{
'htmlFormatContent': false,
'htmlFormatTitle': false,
},
'tag': null,
'colorized': false,
'number': null,
'audioAttributesUsage': 5,
},
}));
});

test('show with default Android-specific details and silent enabled',
() async {
const AndroidInitializationSettings androidInitializationSettings =
AndroidInitializationSettings('app_icon');
const InitializationSettings initializationSettings =
InitializationSettings(android: androidInitializationSettings);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
'channelId',
'channelName',
channelDescription: 'channelDescription',
silent: true,
);

await flutterLocalNotificationsPlugin.show(
1,
'notification title',
'notification body',
const NotificationDetails(android: androidNotificationDetails));
expect(
log.last,
isMethodCall('show', arguments: <String, Object>{
'id': 1,
'title': 'notification title',
'body': 'notification body',
'payload': '',
'platformSpecifics': <String, Object?>{
'icon': null,
'channelId': 'channelId',
'channelName': 'channelName',
'channelDescription': 'channelDescription',
'channelShowBadge': true,
'channelAction':
AndroidNotificationChannelAction.createIfNotExists.index,
'importance': Importance.defaultImportance.value,
'priority': Priority.defaultPriority.value,
'playSound': true,
'enableVibration': true,
'vibrationPattern': null,
'groupKey': null,
'setAsGroupSummary': false,
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': true,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -778,6 +872,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -867,6 +962,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -971,6 +1067,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1069,6 +1166,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1173,6 +1271,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1269,6 +1368,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1369,6 +1469,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1460,6 +1561,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1548,6 +1650,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1643,6 +1746,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1767,6 +1871,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1879,6 +1984,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -1976,6 +2082,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -2072,6 +2179,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -2169,6 +2277,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': null,
'colorRed': null,
'colorGreen': null,
Expand Down Expand Up @@ -2463,6 +2572,7 @@ void main() {
'groupAlertBehavior': GroupAlertBehavior.all.index,
'autoCancel': true,
'ongoing': false,
'silent': false,
'colorAlpha': 255,
'colorRed': 33,
'colorGreen': 150,
Expand Down

0 comments on commit 9d8c793

Please sign in to comment.