Skip to content

Commit

Permalink
[flutter_local_notifications] removed all code related to UILocalNoti…
Browse files Browse the repository at this point in the history
…fication APIs (#2434)

* removed UILocalNotification implementation of pendingNotificationRequests

* Clang Format

* cleaned up implementation of getActiveNotifications

* Clang Format

* remove redundant/old method from iOS interface

* updated code on handling configuration of notification categories

* Clang Format

* removed code on building UILocalNotification

* Clang Format

* cleaned up implementation of cancel

* Clang Format

* cleaned up cancelAll

* Clang Format

* cleaned up checkPermissions

* Clang Format

* removed code and docs on handling didReceiveLocalNotification

* replaced NS_AVAILABLE_IOS(10.0) with API_AVAILABLE(ios(10.0))

* updated pubspec and changelog for 18.0.0-dev.2 release

* Clang Format

* removed handling of didFinishLaunchingWithOptions

* cleaned up duplicate  completionHandler() call

* Clang Format

---------

Co-authored-by: Anka <[email protected]>
Co-authored-by: Anka <[email protected]>
Co-authored-by: Anka <[email protected]>
Co-authored-by: Anka <[email protected]>
Co-authored-by: Anka <[email protected]>
Co-authored-by: Anka <[email protected]>
Co-authored-by: Anka <[email protected]>
Co-authored-by: Anka <[email protected]>
Co-authored-by: Anka <[email protected]>
  • Loading branch information
10 people authored Oct 22, 2024
1 parent a3d3323 commit 8b8669e
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 666 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 @@
## [18.0.0-dev.2]

* [iOS] **Breaking change** Removed `onDidReceiveLocalNotification` callback as this was only relevant on iOS versions older than 10

## [18.0.0-dev.1]

* **Breaking changes** Bumped minimum Flutter SDK requirement to 3.13. Consequently the minimum OS requirements for each platform has been updated as well
Expand Down
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 @@ -2,5 +2,4 @@
#import <UserNotifications/UserNotifications.h>

@interface FlutterLocalNotificationsPlugin : NSObject <FlutterPlugin>
+ (void)setRegisterPlugins:(FlutterPluginRegistrantCallback *)callback;
@end
Loading

0 comments on commit 8b8669e

Please sign in to comment.