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

Web Support #2466

wants to merge 16 commits into from

Conversation

Levi-Lesches
Copy link
Contributor

@Levi-Lesches Levi-Lesches commented Nov 15, 2024

Note

This is a work in progress

Adds web support. The plan is as follows, taken from #481 (comment):

Basic functionality

  • Get a web version of the plugin to register on launch
  • Show a notification with show()
  • List all notifications using getActiveNotifications()
  • Cancel notifications using cancel() and cancelAll()
  • periodicallyShow and periodicallyShowWithDuration would throw UnsupportedError
  • pendingNotificationRequest would return an empty list

Advanced Functionality

  • Support all customizations options offered by web.NotificationOptions
  • Register a custom service worker that can listen for notification clicks
  • Call handlers when a notification is clicked in the foreground / background
  • Support getNotificationAppLaunchDetails() from terminated state

PR Cleanup

  • Document everything (ie, re-enable lint)
  • Add web-specific examples to the example app
  • Write tests where applicable
Custom service workers

Of course, getNotificationAppLaunchDetails and setting any handlers would require the ability to customize the service worker. That appears to be blocked on flutter/flutter#145828, but after reading flutter/flutter#156910, it seems that today's service worker is not technically needed and can be replaced. While we wait for an official mechanism to do so, there is a way to do so today:

let flutter_sw = await navigator.serviceWorker.getRegistration();
if (flutter_sw != null) flutter_sw.unregister();
let plugin_sw = await navigator.serviceWorker.register("/flutter_local_notifications_sw.js");

Where our custom service worker can start off as simple as

function _handleNotif(event) {
  console.log(`Clicked: ${event}`);
  clients.openWindow("/you-clicked-on-a-notification");

}

self.addEventListener("notificationclick", _handleNotif);

Where of course, we'd add more to pass the details of the notification into the Flutter app, and let the plugin pull the data.

@Levi-Lesches
Copy link
Contributor Author

@MaikuB I'll once again put in my request to disable the always_specify_types and lines_longer_than_80_chars lints, as they cause a lot of headache when trying to write readable code here. Disabling them now won't require changing old code, but will allow cleaner code going forward, and there may be a fair amount of code since this is a big feature. I'll also point out again that always_specify_types contradicts Effective Dart and there are even replacements such as omit_obvious_local_variable_types and specify_nonobvious_local_variabletypes. As for line lengths, the new formatter will allow custom line lengths, for which I find 100 is a reasonable limit, especially with variable names as long as WebFlutterLocalNotificationsPlugin.

@Levi-Lesches
Copy link
Contributor Author

Love this test feedback!

 Failed to load ".../flutter_local_notification_test.dart": Instance of 'VmServiceDisappearedException'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant