firebase / flutterfire

πŸ”₯ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.71k stars 3.97k forks source link

[FIREBASE_MESSAGING] Permission dialog does not show during the first boot #9279

Closed slice-mohit closed 2 years ago

slice-mohit commented 2 years ago

Bug report

On running the application for first time, the notification permission dialog is not shown and permission status is itself granted to be denied. On killing the app and then starting it, the notification dialog is shown.

Tested on Android Emulator API 33

Expected behavior

Expected to be shown the dialog on first boot.

Sample project

  static Future<void> initialize() async {
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);

    requestPermission();

    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);

    await _fcm.setForegroundNotificationPresentationOptions(
      alert: true,
      badge: true,
      sound: true,
    );

    FirebaseMessaging.onMessageOpenedApp.listen(
      (RemoteMessage message) {
        log('A new onMessageOpenedApp event was published!');
        RemoteNotification? notification = message.notification;
        AndroidNotification? android = message.notification?.android;
        if (notification != null && android != null) {
          log('Received this in onMessageOpenedApp: ${notification.title.toString()}');
        }
      },
    );

    FirebaseMessaging.onMessage.listen(
      (RemoteMessage message) {
        RemoteNotification? notification = message.notification;
        AndroidNotification? android = message.notification?.android;
        if (notification != null && android != null) {
          flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                subText: channel.description,
                color: Colors.blue,
                playSound: true,
                icon: '@mipmap/ic_launcher',
              ),
            ),
          );
        }
      },
    );
  }

initialized in initstate of materialApp.


Flutter doctor

[βœ“] Flutter (Channel stable, 3.0.5, on macOS 12.4 21F79 darwin-arm, locale en-IN)
    β€’ Flutter version 3.0.5 at /Users/mohitsingh/development/flutter
    β€’ Upstream repository https://github.com/flutter/flutter.git
    β€’ Framework revision f1875d570e (3 weeks ago), 2022-07-13 11:24:16 -0700
    β€’ Engine revision e85ea0e79c
    β€’ Dart version 2.17.6
    β€’ DevTools version 2.12.2

[βœ“] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    β€’ Android SDK at /Users/mohitsingh/Library/Android/sdk
    β€’ Platform android-33, build-tools 33.0.0
    β€’ Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    β€’ Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    β€’ All Android licenses accepted.

[βœ“] Xcode - develop for iOS and macOS (Xcode 13.4.1)
    β€’ Xcode at /Applications/Xcode.app/Contents/Developer
    β€’ CocoaPods version 1.11.3

[βœ“] Chrome - develop for the web
    β€’ Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[βœ“] Android Studio (version 2021.2)
    β€’ 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 11.0.12+0-b1504.28-7817840)

[βœ“] VS Code (version 1.69.2)
    β€’ VS Code at /Users/mohitsingh/Downloads/Visual Studio Code 2.app/Contents
    β€’ Flutter extension version 3.44.0

[βœ“] VS Code (version 1.69.2)
    β€’ VS Code at /Users/mohitsingh/Downloads/Visual Studio Code.app/Contents
    β€’ Flutter extension version 3.44.0

[βœ“] Connected device (3 available)
    β€’ sdk gphone64 arm64 (mobile) β€’ emulator-5554 β€’ android-arm64  β€’ Android 13 (API 33) (emulator)
    β€’ macOS (desktop)             β€’ macos         β€’ darwin-arm64   β€’ macOS 12.4 21F79 darwin-arm
    β€’ Chrome (web)                β€’ chrome        β€’ web-javascript β€’ Google Chrome 103.0.5060.134

[βœ“] HTTP Host Availability
    β€’ All required HTTP hosts are available


Flutter dependencies

Dart SDK 2.17.6
Flutter SDK 3.0.5
fi 1.0.0+1

dependencies:
- another_flushbar 1.10.29 [flutter]
- cloud_firestore 3.4.0 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- connectivity 3.0.6 [flutter meta connectivity_platform_interface connectivity_macos connectivity_for_web]
- cupertino_icons 1.0.5
- firebase_analytics 9.3.0 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter]
- firebase_core 1.20.0 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_crashlytics 2.8.6 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace]
- firebase_messaging 12.0.1 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- firebase_remote_config 2.0.13 [firebase_core firebase_core_platform_interface firebase_remote_config_platform_interface firebase_remote_config_web flutter]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_local_notifications 9.7.0 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone]
- fluttertoast 8.0.9 [flutter flutter_web_plugins]
- http 0.13.4 [async http_parser meta path]
- permission_handler 10.0.0 [flutter meta permission_handler_android permission_handler_apple permission_handler_windows permission_handler_platform_interface]
- pinwheel 2.3.5 [flutter built_value]
- plaid_flutter 2.2.2 [flutter plugin_platform_interface flutter_web_plugins js]
- provider 6.0.3 [collection flutter nested]

dev dependencies:
- flutter_lints 2.0.1 [lints]
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph]

transitive dependencies:
- args 2.3.1
- async 2.8.2 [collection meta]
- boolean_selector 2.1.0 [source_span string_scanner]
- built_collection 5.1.1
- built_value 8.4.0 [built_collection collection fixnum meta]
- characters 1.2.0
- charcode 1.3.1
- clock 1.1.0
- cloud_firestore_platform_interface 5.7.0 [collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 2.8.0 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]
- collection 1.16.0
- connectivity_for_web 0.4.0+1 [connectivity_platform_interface flutter_web_plugins flutter]
- connectivity_macos 0.2.1+2 [flutter connectivity_platform_interface]
- connectivity_platform_interface 2.0.1 [flutter meta plugin_platform_interface]
- dbus 0.7.7 [args ffi meta xml]
- fake_async 1.3.0 [clock collection]
- ffi 2.0.1
- file 6.1.2 [meta path]
- firebase_analytics_platform_interface 3.3.0 [firebase_core flutter meta plugin_platform_interface]
- firebase_analytics_web 0.4.2 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]
- firebase_core_platform_interface 4.5.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 1.7.1 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_crashlytics_platform_interface 3.2.12 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_platform_interface 4.1.0 [firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.1.0 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- firebase_remote_config_platform_interface 1.1.12 [firebase_core flutter meta plugin_platform_interface]
- firebase_remote_config_web 1.1.1 [firebase_core firebase_core_web firebase_remote_config_platform_interface flutter flutter_web_plugins js]
- fixnum 1.0.1
- flutter_local_notifications_linux 0.5.0+1 [flutter flutter_local_notifications_platform_interface dbus path xdg_directories]
- flutter_local_notifications_platform_interface 5.0.0 [flutter plugin_platform_interface]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- js 0.6.4
- lints 2.0.0
- matcher 0.12.11 [stack_trace]
- material_color_utilities 0.1.4
- meta 1.7.0
- nested 1.0.0 [flutter]
- path 1.8.1
- permission_handler_android 10.0.0 [flutter permission_handler_platform_interface]
- permission_handler_apple 9.0.4 [flutter permission_handler_platform_interface]
- permission_handler_platform_interface 3.7.0 [flutter meta plugin_platform_interface]
- permission_handler_windows 0.1.0 [flutter permission_handler_platform_interface]
- petitparser 5.0.0 [meta]
- platform 3.1.0
- plugin_platform_interface 2.1.2 [meta]
- process 4.2.4 [file path platform]
- sky_engine 0.0.99
- source_span 1.8.2 [collection path term_glyph]
- stack_trace 1.10.0 [path]
- stream_channel 2.1.0 [async]
- string_scanner 1.1.0 [charcode source_span]
- term_glyph 1.2.0
- test_api 0.4.9 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- timezone 0.8.0 [path]
- typed_data 1.3.1 [collection]
- vector_math 2.1.2
- xdg_directories 0.2.0+1 [meta path process]
- xml 6.1.0 [collection meta petitparser]


darshankawar commented 2 years ago

@slice-mohit Can you provide code for requestPermission() ? Does the same behavior occur on a real device too ?

Also, can you try the plugin example on same emulator and on real device and see if you get same behavior or not ?

slice-mohit commented 2 years ago

@darshankawar Here is requestPermission. It just calls requestPermission of package and logs the status.

  static Future<void> requestPermission() async {
    final settings = await _fcm.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );

    log('User granted permission: ${settings.authorizationStatus}');
  }

Haven't tested on real device. Will let you know after running the example code.

darshankawar commented 2 years ago

${settings.authorizationStatus}');

What does this print when the permission dialog doesn't show up at first ? as compared to when it does ?

Also, can you uninstall app, compile and run again and observe what does the authorizationStatus returns ?

slice-mohit commented 2 years ago

What does this print when the permission dialog doesn't show up at first ? as compared to when it does ?

It prints

[log] User granted permission: AuthorizationStatus.denied

Also, can you uninstall app, compile and run again and observe what does the authorizationStatus returns ?

In this case also, it prints the same message.

However if I kill the app, rerun it and allow notifications. Then in subsequent builds, it prints status as authorized.

slice-mohit commented 2 years ago

@darshankawar similar behaviour on example app as well. Although I had to add android:exported to true, add my services.json file and change package name to my app name for it work. Hope it was intended to run it this way.

darshankawar commented 2 years ago

Thanks for the update. I now noticed that the Android version you are using is 13, so you are most probably hitting https://github.com/firebase/flutterfire/issues/8720. You may try on lesser Android version and see if you still get same behavior or not. If not, then you will need to follow-up in the linked issue.

slice-mohit commented 2 years ago

@darshankawar Yes, on lower android versions, I am directly getting authorised status without any dialog which I believe is expected behaviour in older versions. Thanks for the link to relevant issue. Closing this one.