Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web Support #2466

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Used dart format
Levi-Lesches committed Nov 15, 2024
commit 2d7d1586836712d234d6fa2541d2d68ab9cbfd2e
13 changes: 8 additions & 5 deletions flutter_local_notifications/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -277,8 +277,10 @@ class _HomePageState extends State<HomePage> {
_notificationsEnabled = grantedNotificationPermission ?? false;
});
} else if (kIsWeb) {
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<WebFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
WebFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
}
}

@@ -300,9 +302,10 @@ class _HomePageState extends State<HomePage> {

@override
Widget build(BuildContext context) => Scaffold(
floatingActionButton: FloatingActionButton(onPressed: () =>
flutterLocalNotificationsPlugin.show(0, null, null, null),
),
floatingActionButton: FloatingActionButton(
onPressed: () =>
flutterLocalNotificationsPlugin.show(0, null, null, null),
),
appBar: AppBar(
title: const Text('Plugin example app'),
),
Original file line number Diff line number Diff line change
@@ -48,33 +48,28 @@ class FlutterLocalNotificationsPlugin {
}

final FlutterLocalNotificationsPlatform instance =
FlutterLocalNotificationsPlatform.instance;
FlutterLocalNotificationsPlatform.instance;
if (kIsWeb && T == WebFlutterLocalNotificationsPlugin && instance is T) {
return instance;
} else if (defaultTargetPlatform == TargetPlatform.android &&
T == AndroidFlutterLocalNotificationsPlugin &&
instance is T
) {
T == AndroidFlutterLocalNotificationsPlugin &&
instance is T) {
return instance;
} else if (defaultTargetPlatform == TargetPlatform.iOS &&
T == IOSFlutterLocalNotificationsPlugin &&
instance is T
) {
T == IOSFlutterLocalNotificationsPlugin &&
instance is T) {
return instance;
} else if (defaultTargetPlatform == TargetPlatform.macOS &&
T == MacOSFlutterLocalNotificationsPlugin &&
instance is T
) {
T == MacOSFlutterLocalNotificationsPlugin &&
instance is T) {
return instance;
} else if (defaultTargetPlatform == TargetPlatform.linux &&
T == LinuxFlutterLocalNotificationsPlugin &&
instance is T
) {
T == LinuxFlutterLocalNotificationsPlugin &&
instance is T) {
return instance;
} else if (defaultTargetPlatform == TargetPlatform.windows &&
T == FlutterLocalNotificationsWindows &&
instance is T
) {
T == FlutterLocalNotificationsWindows &&
instance is T) {
return instance;
}

@@ -119,8 +114,8 @@ class FlutterLocalNotificationsPlugin {
onDidReceiveBackgroundNotificationResponse,
}) async {
if (kIsWeb) {
return resolvePlatformSpecificImplementation
<WebFlutterLocalNotificationsPlugin>()
return resolvePlatformSpecificImplementation<
WebFlutterLocalNotificationsPlugin>()
?.initialize();
}

@@ -242,8 +237,8 @@ class FlutterLocalNotificationsPlugin {
String? payload,
}) async {
if (kIsWeb) {
await resolvePlatformSpecificImplementation
<WebFlutterLocalNotificationsPlugin>()
await resolvePlatformSpecificImplementation<
WebFlutterLocalNotificationsPlugin>()
?.show(id, title, body, payload: payload);
}
if (defaultTargetPlatform == TargetPlatform.android) {
Original file line number Diff line number Diff line change
@@ -1004,7 +1004,6 @@ class MacOSFlutterLocalNotificationsPlugin
}
}


/// Checks [didReceiveBackgroundNotificationResponseCallback], if not `null`,
/// for eligibility to be used as a background callback.
///
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export 'web_mock.dart'
if (dart.library.js_interop) 'web_real.dart';
export 'web_mock.dart' if (dart.library.js_interop) 'web_real.dart';
3 changes: 1 addition & 2 deletions flutter_local_notifications/lib/src/web_mock.dart
Original file line number Diff line number Diff line change
@@ -2,8 +2,7 @@ import 'package:flutter_local_notifications_platform_interface/flutter_local_not

/// A stub implementation of the web plugin, for non-web platforms.
class WebFlutterLocalNotificationsPlugin
extends FlutterLocalNotificationsPlatform
{
extends FlutterLocalNotificationsPlatform {
/// Initializes the plugin.
Future<bool?> initialize() async => null;

21 changes: 9 additions & 12 deletions flutter_local_notifications/lib/src/web_real.dart
Original file line number Diff line number Diff line change
@@ -6,34 +6,31 @@ import '../flutter_local_notifications.dart';

/// Web implementation of the local notifications plugin.
class WebFlutterLocalNotificationsPlugin
extends FlutterLocalNotificationsPlatform
{
extends FlutterLocalNotificationsPlatform {
/// Registers the web plugin with the platform interface.
static void registerWith(_) {
FlutterLocalNotificationsPlatform.instance =
WebFlutterLocalNotificationsPlugin();
WebFlutterLocalNotificationsPlugin();
}

ServiceWorkerRegistration? _registration;

@override
Future<void> show(
int id, String? title, String? body, {String? payload}
) async {
Future<void> show(int id, String? title, String? body,
{String? payload}) async {
final Map<String, int> data = <String, int>{'id': id};
final NotificationOptions options =
NotificationOptions(data: jsonEncode(data).toJS);
NotificationOptions(data: jsonEncode(data).toJS);
_registration?.showNotification(title ?? 'This is a notification', options);
}

/// Initializes the plugin.
Future<bool?> initialize() async {
_registration = await window.navigator.serviceWorker
.getRegistration().toDart;
_registration =
await window.navigator.serviceWorker.getRegistration().toDart;
return _registration != null;
}


/// Requests notification permission from the browser.
///
/// It is highly recommended and sometimes required that this be called only
@@ -48,8 +45,8 @@ class WebFlutterLocalNotificationsPlugin
if (_registration == null) {
return <ActiveNotification>[];
}
final JSArray<Notification> notificationsArray = await _registration
!.getNotifications().toDart;
final JSArray<Notification> notificationsArray =
await _registration!.getNotifications().toDart;
final List<ActiveNotification> result = <ActiveNotification>[];
final Set<int> ids = <int>{};
for (final Notification jsNotification in notificationsArray.toDart) {