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

[firebase_messaging] Notification blank on Android when backgrounded #3331

Closed
stephenhuey opened this issue Aug 24, 2020 · 9 comments
Closed
Labels
plugin: messaging Stale Issue with no recent activity type: documentation Improvements or additions to documentation

Comments

@stephenhuey
Copy link

stephenhuey commented Aug 24, 2020

I just upgraded my FlutterFire plugins to the versions that depend on the big breaking change of the new firebase_core and after following the migration guide, everything is working well for me (Firestore, Auth, etc). So for FCM my pubspec has firebase_messaging: "^7.0.0". When I use the Cloud Messaging composer in the Firebase console and enter a Notification title and text there, I correctly receive it as attributes title and body on both iOS and Android. However, when my Android phone is backgrounded, I see the notification in the system tray but the information is missing, as you can see:


FCM onResume: {notification: {}, data: {collapse_key: com.mydomain.myapp, google.original_priority: high, google.sent_time: 1598307121082, google.delivered_priority: high, google.ttl: 2419200, from: 153101433363, click_action: FLUTTER_NOTIFICATION_CLICK, google.message_id: 0:1598307121466405%7567ae977567ae97}}

As you may observe, I'm also putting the custom data key/value of click_action and FLUTTER_NOTIFICATION_CLICK in the Cloud Messaging composer form. My code which prints the output above looks like this:

      _firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          print("FCM onMessage: $message");
        },
        onBackgroundMessage: myBackgroundMessageHandler,
        onLaunch: (Map<String, dynamic> message) async { // only iOS when Terminated, notification launches app
          print("FCM onLaunch: $message");
        },
        onResume: (Map<String, dynamic> message) async {
          print("FCM onResume: $message");
        },
      );

This seems like a bug--shouldn't notification be populated? Again, I'm just using the Cloud Messaging composer in the Firebase console and it works in all other cases.


flutter doctor -v
[✓] Flutter (Channel stable, v1.17.5, on Mac OS X 10.15.4 19E287, locale en-US)
    • Flutter version 1.17.5 at /Users/stephen/wardrobe/flutter
    • Framework revision 8af6b2f038 (8 weeks ago), 2020-06-30 12:53:55 -0700
    • Engine revision ee76268252
    • Dart version 2.8.4


[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/stephen/Library/Android/sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_212-release-1586-b4-5784211)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • CocoaPods version 1.9.3

[✓] Android Studio (version 3.6)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 45.1.1
    • Dart plugin version 192.8052
    • Java version OpenJDK Runtime Environment (build
      1.8.0_212-release-1586-b4-5784211)

[✓] VS Code (version 1.48.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.13.2

[✓] Connected device (2 available)
    • Nokia 6 1                  • PL2GARM870103356                     • android-arm64
      • Android 10 (API 29)
    • iPhone SE (2nd generation) • 45D76A5A-C10A-42B7-9753-51B3B0DF66B9 • ios
      • com.apple.CoreSimulator.SimRuntime.iOS-13-5 (simulator)

• No issues found!

@stephenhuey
Copy link
Author

In case anyone asks, I added the FLUTTER_NOTIFICATION_CLICK intent-filter to my AndroidManifest.xml and it also has android:name=".Application" and my Application.java follows

the first answer here and also the tips in this issue

because the one in the README does not work, specifically GeneratedPluginRegistrant.registerWith(registry); no longer works.

@stephenhuey
Copy link
Author

Thanks to this answer I was able to send a message that I could do something with even when the app was in the background or terminated:

  let message = {
    notification: {
      title: 'My 10th Title',
      body: 'To be, or not to be!',
    },
    data: {
      title: 'Something Else',
      body: 'Surprise, surprise...',
    },
    android: {
      notification: {
        sound: 'default',
        click_action: 'FLUTTER_NOTIFICATION_CLICK',
      },
    },
    token: registrationToken
  };

I did not know that when sending a notification, you must also send a data component if you want to be able to process the message on Android with onResume or onLaunch, and I was feeling frustrated thinking the docs omitted that fact (or that it was a mistake). But then I found this:

When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.

Source: https://firebase.google.com/docs/cloud-messaging/concept-options

But that line is only partially true, because if I send the FCM message above to iOS when the Flutter app is terminated, both the notification portion and the data portion are present when tapping the notification opens the Flutter app, and even more surprising, both onResume and onLaunch are triggered (and both see the same message).

Maybe this is just a difference between Android and iOS, but if you look at the handy table for various states (foreground/background/terminated on iOS and Android) in the FlutterFire docs then it implies a data message will not reach iOS at all when the app is terminated and that just the onLaunch fires for a notification when the iOS app is terminated. So maybe the plugin is doing something weird by firing both for iOS? I'm ok with that--I just need to watch for duplicates in that case.

Anyway, if my use case is to show a notification message in the system tray and also display that message to the user, then I need to send the same message in both the notification and data portions. If my use case is to display a message in the system tray but I don't care about displaying that message in the app and just want to take some action based on hidden information when the app is open or opened, then that action information goes on the data portion.

@darshankawar
Copy link

darshankawar commented Aug 26, 2020

Hi @stephenhuey,
Thanks for the detailed explanation.

if my use case is to show a notification message in the system tray and also display that message to the user, then I need to send the same message in both the notification and data portions. If my use case is to display a message in the system tray but I don't care about displaying that message in the app and just want to take some action based on hidden information when the app is open or opened, then that action information goes on the data portion.

I think it'll be helpful if above is documented in the plugin.

@darshankawar darshankawar added plugin: messaging type: documentation Improvements or additions to documentation labels Aug 26, 2020
@ir4ever
Copy link

ir4ever commented Aug 27, 2020

Send your Application calls pls.

@stephenhuey
Copy link
Author

stephenhuey commented Aug 27, 2020

Send your Application calls pls.

Pretty straightforward: in the Firebase cloud function I just call this:

admin.messaging().send(message)

where message is an object similar to the one I showed up above. But as @darshankawar mentioned, ideally the documentation would clarify that the notification part of the object is no longer present for Android in the onResume handler:

_firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          print("FCM onMessage: $message");
        },
        onBackgroundMessage: myBackgroundMessageHandler,
        onLaunch: (Map<String, dynamic> message) async { 
          print("FCM onLaunch: $message");
        },
        onResume: (Map<String, dynamic> message) async {
          print("FCM onResume: $message"); // if Android, data is present but notification is not
        },
      );

@ir4ever
Copy link

ir4ever commented Aug 27, 2020

Sorry, typo. Send your Application.java class.

@stephenhuey
Copy link
Author

Sorry, typo. Send your Application.java class.

Here it is. As you can see, I found a tip since the README is not correct.


package com.mydomain.myapp;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin; // https://stackoverflow.com/questions/59446933/pluginregistry-cannot-be-converted-to-flutterengine
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        //GeneratedPluginRegistrant.registerWith(registry);
        // https://stackoverflow.com/questions/59446933/pluginregistry-cannot-be-converted-to-flutterengine
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));

    }
}

@ir4ever
Copy link

ir4ever commented Aug 27, 2020

Thanks man! Unfortunately it didn't solve to me

@Salakar Salakar added the Stale Issue with no recent activity label Apr 1, 2021
@russellwheatley
Copy link
Member

Closing in favour of trying the latest messaging version which has been been heavily reworked, along with documentation, since this issue was raised: https://firebase.flutter.dev/docs/messaging/overviewIf you still have a problem please raise a new GitHub issue with up to date information and code snippets if possible. Thanks.

@firebase firebase locked and limited conversation to collaborators May 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
plugin: messaging Stale Issue with no recent activity type: documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

5 participants