Skip to content

Commit

Permalink
removed code and docs on handling didReceiveLocalNotification
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikuB committed Oct 22, 2024
1 parent a12f3ef commit 49c21de
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 153 deletions.
57 changes: 1 addition & 56 deletions flutter_local_notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,57 +349,6 @@ By design, iOS applications *do not* display notifications while the app is in t

For iOS 10+, use the presentation options to control the behaviour for when a notification is triggered while the app is in the foreground. The default settings of the plugin will configure these such that a notification will be displayed when the app is in the foreground.

For older versions of iOS, you need to handle the callback as part of specifying the method that should be fired to the `onDidReceiveLocalNotification` argument when creating an instance `DarwinInitializationSettings` object that is passed to the function for initializing the plugin.

Here is an example:

```dart
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');
final DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
final LinuxInitializationSettings initializationSettingsLinux =
LinuxInitializationSettings(
defaultActionName: 'Open notification');
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
linux: initializationSettingsLinux);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: onDidReceiveNotificationResponse);
...
void onDidReceiveLocalNotification(
int id, String? title, String? body, String? payload) async {
// display a dialog with the notification details, tap ok to go to another page
showDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: Text(title??''),
content: Text(body??''),
actions: [
CupertinoDialogAction(
isDefaultAction: true,
child: Text('Ok'),
onPressed: () async {
Navigator.of(context, rootNavigator: true).pop();
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(payload),
),
);
},
)
],
),
);
}
```

## ❓ Usage

Before going on to copy-paste the code snippets in this section, double-check you have configured your application correctly.
Expand Down Expand Up @@ -576,8 +525,7 @@ FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('app_icon');
final DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
DarwinInitializationSettings();
final LinuxInitializationSettings initializationSettingsLinux =
LinuxInitializationSettings(
defaultActionName: 'Open notification');
Expand Down Expand Up @@ -615,8 +563,6 @@ The `LinuxInitializationSettings` class requires a name for the default action t

On iOS and macOS, initialisation may show a prompt to requires users to give the application permission to display notifications (note: permissions don't need to be requested on Android). Depending on when this happens, this may not be the ideal user experience for your application. If so, please refer to the next section on how to work around this.

For an explanation of the `onDidReceiveLocalNotification` callback associated with the `DarwinInitializationSettings` class, please read [this](https://github.com/MaikuB/flutter_local_notifications/tree/master/flutter_local_notifications#handling-notifications-whilst-the-app-is-in-the-foreground).

### [iOS (all supported versions) and macOS 10.14+] Requesting notification permissions

The constructor for the `DarwinInitializationSettings` class has three named parameters (`requestSoundPermission`, `requestBadgePermission` and `requestAlertPermission`) that controls which permissions are being requested. If you want to request permissions at a later point in your application on iOS, set all of the above to false when initialising the plugin.
Expand All @@ -631,7 +577,6 @@ The constructor for the `DarwinInitializationSettings` class has three named pa
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
onDidReceiveLocalNotification: onDidReceiveLocalNotification,
);
final MacOSInitializationSettings initializationSettingsMacOS =
MacOSInitializationSettings(
Expand Down
51 changes: 0 additions & 51 deletions flutter_local_notifications/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'dart:io';
import 'dart:typed_data';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand All @@ -22,11 +21,6 @@ int id = 0;
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

/// Streams are created so that app can respond to notification-related events
/// since the plugin is initialised in the `main` function
final StreamController<ReceivedNotification> didReceiveLocalNotificationStream =
StreamController<ReceivedNotification>.broadcast();

final StreamController<String?> selectNotificationStream =
StreamController<String?>.broadcast();

Expand Down Expand Up @@ -153,17 +147,6 @@ Future<void> main() async {
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
onDidReceiveLocalNotification:
(int id, String? title, String? body, String? payload) async {
didReceiveLocalNotificationStream.add(
ReceivedNotification(
id: id,
title: title,
body: body,
payload: payload,
),
);
},
notificationCategories: darwinNotificationCategories,
);
final LinuxInitializationSettings initializationSettingsLinux =
Expand Down Expand Up @@ -262,7 +245,6 @@ class _HomePageState extends State<HomePage> {
super.initState();
_isAndroidPermissionGranted();
_requestPermissions();
_configureDidReceiveLocalNotificationSubject();
_configureSelectNotificationSubject();
}

Expand Down Expand Up @@ -311,38 +293,6 @@ class _HomePageState extends State<HomePage> {
}
}

void _configureDidReceiveLocalNotificationSubject() {
didReceiveLocalNotificationStream.stream
.listen((ReceivedNotification receivedNotification) async {
await showDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: receivedNotification.title != null
? Text(receivedNotification.title!)
: null,
content: receivedNotification.body != null
? Text(receivedNotification.body!)
: null,
actions: <Widget>[
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () async {
Navigator.of(context, rootNavigator: true).pop();
await Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (BuildContext context) =>
SecondPage(receivedNotification.payload),
),
);
},
child: const Text('Ok'),
)
],
),
);
});
}

void _configureSelectNotificationSubject() {
selectNotificationStream.stream.listen((String? payload) async {
await Navigator.of(context).push(MaterialPageRoute<void>(
Expand All @@ -353,7 +303,6 @@ class _HomePageState extends State<HomePage> {

@override
void dispose() {
didReceiveLocalNotificationStream.close();
selectNotificationStream.close();
super.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1031,31 +1031,4 @@ - (BOOL)application:(UIApplication *)application
return YES;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
#pragma clang diagnostic pop
if (@available(iOS 10.0, *)) {
return;
}
if (![self isAFlutterLocalNotification:notification.userInfo]) {
return;
}

NSMutableDictionary *arguments = [[NSMutableDictionary alloc] init];
arguments[ID] = notification.userInfo[NOTIFICATION_ID];
if (notification.userInfo[TITLE] != [NSNull null]) {
arguments[TITLE] = notification.userInfo[TITLE];
}
if (notification.alertBody != nil) {
arguments[BODY] = notification.alertBody;
}
if (notification.userInfo[PAYLOAD] != [NSNull null]) {
arguments[PAYLOAD] = notification.userInfo[PAYLOAD];
}
[_channel invokeMethod:DID_RECEIVE_LOCAL_NOTIFICATION arguments:arguments];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import 'platform_specifics/darwin/mappers.dart';
import 'platform_specifics/darwin/notification_details.dart';
import 'platform_specifics/darwin/notification_enabled_options.dart';
import 'platform_specifics/ios/enums.dart';
import 'typedefs.dart';
import 'types.dart';
import 'tz_datetime_mapper.dart';

Expand Down Expand Up @@ -609,7 +608,6 @@ class IOSFlutterLocalNotificationsPlugin
}

DidReceiveNotificationResponseCallback? _onDidReceiveNotificationResponse;
DidReceiveLocalNotificationCallback? _onDidReceiveLocalNotification;

/// Initializes the plugin.
///
Expand Down Expand Up @@ -645,8 +643,6 @@ class IOSFlutterLocalNotificationsPlugin
onDidReceiveBackgroundNotificationResponse,
}) async {
_onDidReceiveNotificationResponse = onDidReceiveNotificationResponse;
_onDidReceiveLocalNotification =
initializationSettings.onDidReceiveLocalNotification;
_channel.setMethodCallHandler(_handleMethod);

final Map<String, Object> arguments = initializationSettings.toMap();
Expand Down Expand Up @@ -823,13 +819,6 @@ class IOSFlutterLocalNotificationsPlugin
),
);
break;
case 'didReceiveLocalNotification':
_onDidReceiveLocalNotification!(
call.arguments['id'],
call.arguments['title'],
call.arguments['body'],
call.arguments['payload']);
break;
default:
return await Future<void>.error('Method not defined');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import '../../typedefs.dart';
import 'notification_category.dart';

/// Plugin initialization settings for Darwin-based operating systems
Expand All @@ -16,7 +15,6 @@ class DarwinInitializationSettings {
this.defaultPresentBadge = true,
this.defaultPresentBanner = true,
this.defaultPresentList = true,
this.onDidReceiveLocalNotification,
this.notificationCategories = const <DarwinNotificationCategory>[],
});

Expand Down Expand Up @@ -125,12 +123,6 @@ class DarwinInitializationSettings {
/// On macOS, this property is only applicable to macOS 11 or newer.
final bool defaultPresentList;

/// Callback for handling when a notification is triggered while the app is
/// in the foreground.
///
/// This property is only applicable to iOS versions older than 10.
final DidReceiveLocalNotificationCallback? onDidReceiveLocalNotification;

/// Configure the notification categories ([DarwinNotificationCategory])
/// available. This allows for fine-tuning of preview display.
///
Expand Down

0 comments on commit 49c21de

Please sign in to comment.