flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
162.97k stars 26.8k forks source link

Notification from dart:html not being shown. #150328

Open BenjiFarquhar opened 1 week ago

BenjiFarquhar commented 1 week ago

Steps to reproduce

Notification from dart:html is not working. Is this code meant to show a notification?

html.Notification('Hello, World!');

See the code sample for a full app that is not showing the notification.

Expected results

It shows a notification in the chrome browser.

Actual results

No notification is shown in the chrome browser.

Code sample

Code sample ```dart import 'package:flutter/material.dart'; import 'dart:html' as html; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { int _counter = 0; void _incrementCounter() { html.Notification('Hello, World!'); setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), ); } @override void initState() { super.initState(); html.window.console.log('Hello, World!'); html.Notification.requestPermission().then((userPermission) { if (userPermission != 'granted') { return; } html.Notification('Hello, World!'); html.Notification.new('Hello, World!'); }); } } ```

Flutter Doctor output

Doctor output ```console [✓] Flutter (Channel stable, 3.22.2, on macOS 14.5 23F79 darwin-arm64, locale en-GB) • Flutter version 3.22.2 on channel stable at /Users/benjaminfarquhar/development/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 761747bfc5 (11 days ago), 2024-06-05 22:15:13 +0200 • Engine revision edd8546116 • Dart version 3.4.3 • DevTools version 2.34.3 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/benjaminfarquhar/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • ANDROID_HOME = /Users/benjaminfarquhar/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.4) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15F31d • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2022.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231) [✓] IntelliJ IDEA Community Edition (version 2023.2.2) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart [✓] VS Code (version 1.90.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.90.0 [✓] Connected device (4 available) • Benji’s iPhone 13 mini (mobile) • 00008110-001119123C7B801E • ios • iOS 17.5.1 21F90 • macOS (desktop) • macos • darwin-arm64 • macOS 14.5 23F79 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 14.5 23F79 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 125.0.6422.176 [✓] Network resources • All expected network resources are available. • No issues found!```
darshankawar commented 1 week ago

@BenjiFarquhar

Notification from dart:html is not working. Is this code meant to show a notification?

This does work. You need to allow notifications to show, as below:

Screenshot 2024-06-17 at 4 03 15 PM

Then upon running the given code, it shows the notifications and gets printed in the console as well:

A Dart VM Service on Chrome is available at: http://127.0.0.1:52353/_CeaKEIrgOQ=
Hello, World!
BenjiFarquhar commented 1 week ago

@darshankawar Thanks. I click "Allow" when that pops up. But then no notification is shown to me.

Screenshot 2024-06-18 at 7 24 28 AM

I'm expecting to also see a new notification every time I click the FAB, but don't:

Screenshot 2024-06-18 at 7 22 28 AM

Can you please show a screenshot of the full page with the notification showing "Hello World!"? Are you running locally in debug mode with flutter run -d chrome, as I am?

My version of chrome is Version 125.0.6422.176 (Official Build) (arm64). I just updated to Version 126.0.6478.62 (Official Build) (arm64) but it didn't fix it.

darshankawar commented 1 week ago

Can you please show a screenshot of the full page with the notification showing "Hello World!"? Are you running locally in debug mode with flutter run -d chrome, as I am?

Yes, I ran locally with same command. Interestingly, when I for the first time ran the code, I did see the notification on the top right corner of the screen but I didn't take screenshot or video record it and now when I tried again, I don't see it occuring, so I missed the chance to record it : (

I will keep the issue open for team's attention / tracking.

stable, master flutter doctor -v ``` [!] Flutter (Channel stable, 3.22.2, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.22.2 on channel stable at /Users/dhs/documents/fluttersdk/flutter ! Warning: `flutter` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 761747bfc5 (33 hours ago), 2024-06-05 22:15:13 +0200 • Engine revision edd8546116 • Dart version 3.4.3 • DevTools version 2.34.3 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. [!] Flutter (Channel master, 3.23.0-13.0.pre.262, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.23.0-13.0.pre.262 on channel master at /Users/dhs/documents/fluttersdk/flutter ! Warning: `flutter` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 6a8f70f11e (2 hours ago), 2024-06-18 00:12:10 -0400 • Engine revision 78fdd06af5 • Dart version 3.5.0 (build 3.5.0-272.0.dev) • DevTools version 2.36.0 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/dhs/Library/Android/sdk ✗ cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. ✗ Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/macos#android-setup for more details. [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 13C100 • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2) • IntelliJ at /Applications/IntelliJ IDEA.app • Flutter plugin version 65.1.4 • Dart plugin version 213.7228 [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.29.0 [✓] Connected device (3 available) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 15.3.1 19D52 • macOS (desktop) • macos • darwin-x64 • macOS 12.2.1 21D62 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.119 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. ```
ditman commented 5 days ago

Thanks for creating this issue; dart:html is a very thin js-interop layer that enables Flutter Web apps use standard JS APIs, in this case, the "Notifications API". Here's its documentation:

Here's some highlights from the documentation that are causing this issue (and its reproduction):

the user needs to grant permission to display notifications, which is generally done when the app or site initializes, using the Notification.requestPermission() method. This should be done in response to a user gesture, such as clicking a button. ... This is not only best practice but going forward browsers will explicitly disallow notifications not triggered in response to a user gesture. 💔

The reproduction code provided is attempting to requestPermission and display notifications without user interaction (on its initState method)

Proposed solution

I'd modify the _MyHomePageState class from the reproduction to something like this:

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  /// onPressed, display a notification and increase the counter.
  void _incrementCounter() async {
    await displayNotification();
    setState(() {
      _counter++;
    });
  }

  /// Always request for permissions, and if 'granted', display a notification.
  Future<void> displayNotification() async {
    final permission = await html.Notification.requestPermission();
    if (permission == 'granted') {
      html.Notification('About to update counter to: ${_counter + 1}');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  // Removed `initState`
}

Some unsolicited advice:


Please, @BenjiFarquhar, let me know if this helps, so we can close this issue. Thanks!