Skip to content

Commit

Permalink
[flutter_local_notifications] add ability to delete notification chan…
Browse files Browse the repository at this point in the history
…nels (#573)

* add ability to delete notification channel with tests and updated example

* bump version

* update example code around channel management

* update readme

* remove default id value

* remove usage of dynamic in test code
  • Loading branch information
MaikuB authored Apr 10, 2020
1 parent 44cd076 commit 60072d6
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 63 deletions.
5 changes: 5 additions & 0 deletions flutter_local_notifications/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# [1.4.1]

* [Android] added the ability to create notification channels before a notification is shown. This can be done by calling the `createNotificationChannel` within the `AndroidFlutterLocalNotificationsPlugin` class. This allows applications to create notification channels before a notification is shown. Thanks to the PR from [Vladimir Gerashchenko](https://github.com/ZaarU).
* [Android] added the ability to delete notification channels. This can be done by calling `deleteNotificationChannel` within `AndroidFlutterLocalNotificationsPlugin` class.

# [1.4.0]

Please note that there are a number of breaking changes in this release to improve the developer experience when using the plugin APIs. The changes should hopefully be straightforward but please through the changelog carefully just in case. The steps migrate your code has been covered below but the Git history of the example application's `main.dart` file can also be used as reference.
Expand Down
7 changes: 6 additions & 1 deletion flutter_local_notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ A cross platform plugin for displaying local notifications.
* [Android] Group notifications
* [Android] Show progress notifications
* [Android] Configure notification visibility on the lockscreen
* [Android] Ability to create and delete notification channels
* [iOS] Request notification permissions and customise the permissions being requested around displaying notifications
* [iOS] Display notifications with attachments

Expand Down Expand Up @@ -70,7 +71,11 @@ Note that this plugin aims to provide abstractions for all platforms as opposed

The GitHub repository has an example app that should demonstrate of all the supported features of the plugin. Please check the example for more detailed code samples. If you only copy and paste the Dart code then this will not work as there's setup required for each platform. Pub also generates API docs for the latest version [here](https://pub.dartlang.org/documentation/flutter_local_notifications/latest/). Besides referring to the example app and getting started section, please ensure that you have performed the steps in the integration guide for each platform further below.

The following samples will demonstrate the more commonly used functionalities. The first step is to create a new instance of the plugin class and then initialise it with the settings to use for each platform
The following samples will demonstrate the more commonly used functionalities.

### Initialisation

The first step is to create a new instance of the plugin class and then initialise it with the settings to use for each platform

```dart
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class FlutterLocalNotificationsPlugin implements MethodCallHandler, Plugi
private static final String SCHEDULED_NOTIFICATIONS = "scheduled_notifications";
private static final String INITIALIZE_METHOD = "initialize";
private static final String CREATE_NOTIFICATION_CHANNEL_METHOD = "createNotificationChannel";
private static final String DELETE_NOTIFICATION_CHANNEL_METHOD = "deleteNotificationChannel";
private static final String PENDING_NOTIFICATION_REQUESTS_METHOD = "pendingNotificationRequests";
private static final String SHOW_METHOD = "show";
private static final String CANCEL_METHOD = "cancel";
Expand All @@ -93,6 +94,7 @@ public class FlutterLocalNotificationsPlugin implements MethodCallHandler, Plugi
private static final String INVALID_BIG_PICTURE_ERROR_CODE = "INVALID_BIG_PICTURE";
private static final String INVALID_SOUND_ERROR_CODE = "INVALID_SOUND";
private static final String INVALID_LED_DETAILS_ERROR_CODE = "INVALID_LED_DETAILS";
private static final String DELETE_NOTIFICATION_CHANNEL_FAILED = "DELETE_NOTIFICATION_CHANNEL_FAILED";
private static final String INVALID_LED_DETAILS_ERROR_MESSAGE = "Must specify both ledOnMs and ledOffMs to configure the blink cycle on older versions of Android before Oreo";
private static final String NOTIFICATION_LAUNCHED_APP = "notificationLaunchedApp";
private static final String INVALID_DRAWABLE_RESOURCE_ERROR_MESSAGE = "The resource %s could not be found. Please make sure it has been added as a drawable resource to your Android head project.";
Expand Down Expand Up @@ -762,6 +764,9 @@ public void onMethodCall(MethodCall call, Result result) {
case CREATE_NOTIFICATION_CHANNEL_METHOD:
createNotificationChannel(call, result);
break;
case DELETE_NOTIFICATION_CHANNEL_METHOD:
deleteNotificationChannel(call, result);
break;
default:
result.notImplemented();
break;
Expand Down Expand Up @@ -952,4 +957,13 @@ private void createNotificationChannel(MethodCall call, Result result) {
setupNotificationChannel(applicationContext, notificationChannelDetails);
result.success(null);
}

private void deleteNotificationChannel(MethodCall call, Result result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = call.arguments();
notificationManager.deleteNotificationChannel(channelId);
result.success(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class NotificationChannelDetails {
private static final String LED_COLOR_GREEN = "ledColorGreen";
private static final String LED_COLOR_BLUE = "ledColorBlue";

public String id = "Default_Id";
public String id;
public String name;
public String description;
public Boolean showBadge;
Expand Down
34 changes: 32 additions & 2 deletions flutter_local_notifications/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ class _HomePageState extends State<HomePage> {
await _createNotificationChannel();
},
),
PaddedRaisedButton(
buttonText: 'Delete notification channel [Android]',
onPressed: () async {
await _deleteNotificationChannel();
},
),
],
),
),
Expand Down Expand Up @@ -993,8 +999,32 @@ class _HomePageState extends State<HomePage> {
context: context,
builder: (BuildContext context) {
return AlertDialog(
content:
Text('Channel \"${androidNotificationChannel.name}\" created'),
content: Text(
'Channel with name \"${androidNotificationChannel.name}\" created'),
actions: [
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
});
}

Future<void> _deleteNotificationChannel() async {
const channelId = 'your channel id 2';
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.deleteNotificationChannel(channelId);

await showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text('Channel with id \"$channelId\" deleted'),
actions: [
FlatButton(
child: Text('OK'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import 'types.dart';
const MethodChannel _channel =
MethodChannel('dexterous.com/flutter/local_notifications');

/// An implementation of a local notifications platform using method channels
/// An implementation of a local notifications platform using method channels.
class MethodChannelFlutterLocalNotificationsPlugin
extends FlutterLocalNotificationsPlatform {
@override
Expand Down Expand Up @@ -52,7 +52,7 @@ class MethodChannelFlutterLocalNotificationsPlugin
}
}

/// Android implementation of the local notifications plugin
/// Android implementation of the local notifications plugin.
class AndroidFlutterLocalNotificationsPlugin
extends MethodChannelFlutterLocalNotificationsPlugin {
SelectNotificationCallback _onSelectNotification;
Expand All @@ -68,7 +68,8 @@ class AndroidFlutterLocalNotificationsPlugin
'initialize', initializationSettings.toMap());
}

/// Schedules a notification to be shown at the specified time with an optional payload that is passed through when a notification is tapped
/// Schedules a notification to be shown at the specified time with an optional payload that is passed through when a notification is tapped.
///
/// The [androidAllowWhileIdle] parameter is Android-specific and determines if the notification should still be shown at the specified time
/// even when in a low-power idle mode.
Future<void> schedule(int id, String title, String body,
Expand Down Expand Up @@ -105,7 +106,7 @@ class AndroidFlutterLocalNotificationsPlugin
});
}

/// Shows a notification on a daily interval at the specified time
/// Shows a notification on a daily interval at the specified time.
Future<void> showWeeklyAtDayAndTime(
int id,
String title,
Expand Down Expand Up @@ -163,13 +164,17 @@ class AndroidFlutterLocalNotificationsPlugin

/// Creates a notification channel.
///
/// Only applies to Android 8.0+
/// Only applies to Android 8.0+.
Future<void> createNotificationChannel(
AndroidNotificationChannel notificationChannel) {
return _channel.invokeMethod(
'createNotificationChannel', notificationChannel.toMap());
}

Future<void> deleteNotificationChannel(String channelId) {
return _channel.invokeMethod('deleteNotificationChannel', channelId);
}

Future<void> _handleMethod(MethodCall call) {
switch (call.method) {
case 'selectNotification':
Expand All @@ -180,14 +185,16 @@ class AndroidFlutterLocalNotificationsPlugin
}
}

/// iOS implementation of the local notifications plugin
/// iOS implementation of the local notifications plugin.
class IOSFlutterLocalNotificationsPlugin
extends MethodChannelFlutterLocalNotificationsPlugin {
SelectNotificationCallback _onSelectNotification;

DidReceiveLocalNotificationCallback _onDidReceiveLocalNotification;

/// Initializes the plugin. Call this method on application before using the plugin further.
/// Initializes the plugin.
///
/// Call this method on application before using the plugin further.
/// This should only be done once. When a notification created by this plugin was used to launch the app,
/// calling `initialize` is what will trigger to the `onSelectNotification` callback to be fire.
///
Expand Down Expand Up @@ -215,7 +222,7 @@ class IOSFlutterLocalNotificationsPlugin
});
}

/// Schedules a notification to be shown at the specified time with an optional payload that is passed through when a notification is tapped
/// Schedules a notification to be shown at the specified time with an optional payload that is passed through when a notification is tapped.
Future<void> schedule(int id, String title, String body,
DateTime scheduledDate, IOSNotificationDetails notificationDetails,
{String payload}) async {
Expand All @@ -230,7 +237,7 @@ class IOSFlutterLocalNotificationsPlugin
});
}

/// Shows a notification on a daily interval at the specified time
/// Shows a notification on a daily interval at the specified time.
Future<void> showDailyAtTime(int id, String title, String body,
Time notificationTime, IOSNotificationDetails notificationDetails,
{String payload}) async {
Expand All @@ -247,7 +254,7 @@ class IOSFlutterLocalNotificationsPlugin
});
}

/// Shows a notification on a daily interval at the specified time
/// Shows a notification on a daily interval at the specified time.
Future<void> showWeeklyAtDayAndTime(
int id,
String title,
Expand Down
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.4.0
version: 1.4.1
homepage: https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications

dependencies:
Expand Down
Loading

0 comments on commit 60072d6

Please sign in to comment.