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 title/body is missing for onResume and onLaunch #2012

Closed
JacobFrericks opened this issue Feb 17, 2020 · 25 comments
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) platform: android Issues / PRs which are specifically for Android. plugin: messaging type: bug Something isn't working

Comments

@JacobFrericks
Copy link

Describe the bug
The callbacks are set to print the message when it's received. When I send a notification to my app from the firebase console, I correctly get a title and body for onMessage, but onResume, I get an empty object under the Notification parent (see below for an example of what I see)

To Reproduce
Steps to reproduce the behavior:

Follow the instructions for adding firebase to your app: https://firebase.google.com/docs/flutter/setup?platform=android
(NOTE: I have added two packages firebase_core: ^0.4.4 and firebase_messaging: ^6.0.9
Copy and paste the example into main.dart. Example is found here: https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/example/lib/main.dart
Start the app in either a simulator or a real device, and copy the token printed in the terminal. Keep the application open.
NOTE: I have also published this code in a repository. If you want to skip steps 1-2, you can clone it here: https://github.com/JacobFrericks/sandbox

// OnMessage
4. Go to your firebase cloud_messaging page, and send a notification with the notification title test_title and the notification text test_text.
5. Click "Send test message", and paste in the token. Click "Test".
6. Observe the console. on_message appears, with the correct title and body. Here's my example:

onMessage: {notification: {title: test, body: test}, data: {click_action: FLUTTER_NOTIFICATION_CLICK}}

// OnResume
7. push the home button on the device, sending the application into the background.
8. Repeat steps 4-5.
9. Press the notification, which will open the app.
10. Observe the console. onResume appears, with no title or body. Here's my example:

onResume: {notification: {}, data: {collapse_key: com.sandbox.xxx, google.original_priority: high, google.sent_time: 1581959199135, google.delivered_priority: high, google.ttl: 2419200, from: xxxxxxxx, click_action: FLUTTER_NOTIFICATION_CLICK, google.message_id: xxxxxx}}

// OnLaunch
11. press the square multitask button and swipe up/down on the application, completely closing it.
12. Repeat steps 7-9.
13. This is hard to view, because closing the application disconnects from the console. I suspect it is the same as onResume, as the two receive the notification in the same way.

Expected behavior
The expected behavior is that when the notification is received, onResume/onLaunch messages should contain both a title and body under the notification parent. Instead, an empty notification object appears.

Flutter Doctor:

➜  ~ flutter doctor -v
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.15.2 19C57, locale en-US)
    • Flutter version 1.12.13+hotfix.8 at /Users/jacobf/flutter
    • Framework revision 0b8abb4724 (6 days ago), 2020-02-11 11:44:36 -0800
    • Engine revision e1e6ced81d
    • Dart version 2.7.0

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/jacobf/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.3
    • ANDROID_HOME = /Users/jacobf/Library/Android/sdk
    • ANDROID_SDK_ROOT = ~/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

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

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 43.0.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] IntelliJ IDEA Ultimate Edition (version 2019.2.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 42.2.2
    • Dart plugin version 192.7761

[✓] Connected device (1 available)
    • Android SDK built for x86 • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)

• No issues found!
@JacobFrericks JacobFrericks added the type: bug Something isn't working label Feb 17, 2020
@asimag
Copy link

asimag commented Feb 18, 2020

I am having the same issue

@mankeomorakort
Copy link

I am having the same issue

You should add your title and body in data like below:

{
  "registration_ids": [
   
  ],
  "notification": {
    "title": "Hello Notification API",
    "body": "Send From Notification API"
  },
  "priority": "high",
  "data": {
	"click_action" : "FLUTTER_NOTIFICATION_CLICK",
	"title": "Hello Notification API",
    "body": "Send From Notification API",
  }
}

@JacobFrericks
Copy link
Author

@mankeomorakort I'm aware that I can push the title and body through via data, but doesn't that negate the reason for having the "title" and "text" options available in Step 1 of the Firebase console? Why even have these as options if they don't work?

Screen Shot 2020-03-04 at 3 35 01 PM

I believe this is a bug that requires fixing for Flutter, and using the custom data in Step 5 with the keys "title" and "body" is just a workaround.

@lucianoiam
Copy link

I confirm this used to work a couple of months ago; it is definitely a bug somewhere. Adding title and body to data is a workaround not a solution.

@iapicca iapicca added severe: regression blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed severe: regression blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Mar 6, 2020
@iapicca
Copy link

iapicca commented Mar 6, 2020

Hi @JacobFrericks
could you please provide your pubspec.yaml
Thank you

@iapicca iapicca closed this as completed Mar 6, 2020
@iapicca iapicca reopened this Mar 6, 2020
@iapicca iapicca added severe: regression blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Mar 6, 2020
@JacobFrericks
Copy link
Author

JacobFrericks commented Mar 6, 2020

@iapicca
You can see the file you requested in my sandbox repository mentioned in the first comment. See https://github.com/JacobFrericks/sandbox/tree/master/pubspec.yaml

@iapicca
Copy link

iapicca commented Mar 8, 2020

Hi @JacobFrericks
I missed that
thanks

@iapicca iapicca removed the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Mar 8, 2020
@JacobFrericks
Copy link
Author

So what's the next step for this? Is there any testing that I can do? It's been almost two weeks, just wanted to get an update.

@thomasostfeld
Copy link

I ran into the same issue as well. This is very frustrating as we need to do further processing of the notification in our app.

It's hard to believe this has been reported 6 weeks ago and is very easy to reproduce, but hasn't been fixed yet. This breaks every app that actually wants to do anything with the contents of a notification.

@hungbu
Copy link

hungbu commented Apr 15, 2020

I also encountered the same issue:

send notification from cloud:
receive message:
onMessage: OK
onLaunch: NG
onResume: NG

I tried:

  1. Create new project: onMessage, onLaunch, onResume works normally
  2. Add all libraries in the current project to the new project: onMessage, onLaunch, onResume works normally
  3. copy all code of current project to new project: onMessage: OK, onLaunch: NG, onResume: NG

Actually, I still think that my code is in error, but I could not find the error anywhere.

NotificationService :

class NotificationService extends BaseGateway {
  WorkspaceGateway _workspaceGateway;

  FirebaseMessaging _firebaseMessaging;

  static final NotificationService _singleton = NotificationService._internal();

  NotificationService._internal();

  factory NotificationService({WorkspaceGateway wsGateway}) {
    _singleton._workspaceGateway = wsGateway;
    return _singleton;
  }

  Future registerNotifyDevice() async {
    if (_firebaseMessaging == null) {
      _firebaseMessaging = FirebaseMessaging();
    }

    final prefs = await SharedPreferences.getInstance();
    String firebaseToken = prefs.getString(XFirebase.token);
    String registeredAtString = prefs.getString(XFirebase.registeredAt);
    DateTime registeredAt;
    if (registeredAtString != null && registeredAtString.isNotEmpty) {
      print('stored registered at time $registeredAtString');
      registeredAt = DateTime.parse(registeredAtString);
    }

    if (firebaseToken != null &&
        firebaseToken.isNotEmpty &&
        registeredAt != null &&
        DateTime
            .now()
            .difference(registeredAt)
            .inDays <= 3) {
      print("firebase has registered");
      return;
    }

    _firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
   );
    _firebaseMessaging
        .requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));

    firebaseToken = await _firebaseMessaging.getToken();
    if (firebaseToken != null && firebaseToken.isNotEmpty) {
      _registerNotifyDevice(firebaseToken);
    }
  }
.
.
.

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  http: ^0.12.0
  flutter_webview_plugin: ^0.3.10+1
  shared_preferences: ^0.5.3+5
  flutter_secure_storage: ^3.3.1+1
  provider: ^4.0.1
  timeago: ^2.0.21
  intl: ^0.16.0
  web_socket_channel: ^1.1.0
  multi_image_picker: ^4.6.3
  uuid: 2.0.4
  sentry: ">=3.0.0 <4.0.0"

  http_extensions_log: ^0.1.2
  device_info: ^0.4.2+1

  #Fab menu
  flutter_speed_dial: ^1.2.5
  firebase_messaging: ^6.0.13
  flutter_local_notifications: ^1.4.1

  fluttertoast: ^3.1.3
  flutter_svg: ^0.15.0
  cached_network_image: ^2.0.0

  #text editor
  extended_text_field: ^0.4.9
  extended_text: any

  connectivity: ^0.4.8+1
  photo_view: ^0.9.1
  package_info: ^0.4.0+13

  url_launcher: ^5.4.1
  google_fonts: ^0.3.9

can anyone help me, please :)

@JacobFrericks
Copy link
Author

JacobFrericks commented Apr 17, 2020

@hungbu when you say that it "works normally" are you getting the title/body for onResume/onLaunch? I just updated flutter for the stable, then dev, then master channels, all with the same results as the first post: title/body are appearing in onMessage, but not onResume/onLaunch.

As for your question, I would recommend making a new issue.

@JacobFrericks
Copy link
Author

I also updated to the latest firebase_core and firebase_messaging with the same result.

firebase_core: ^0.4.4+3
firebase_messaging: ^6.0.13

@JacobFrericks
Copy link
Author

JacobFrericks commented Apr 17, 2020

So, I got tired of waiting for this fix to be prioritized/fixed, and created a workaround. I pushed it to my sandbox repo. Here's the code changes I performed to create the workaround (it should be a simple copy/paste for most people): https://github.com/JacobFrericks/sandbox/commit/398d1763cfb195a1f68a6f2ed9b8b8b281b9e606

Below is the code in case the link gets broken somewhere down the line:

  1. Create this function in the same scope as your initState (assuming you're doing your firebaseMessaging.config there, as is in the above link repo)
  Map<String, dynamic> fixMessageTitleAndBody(Map<String, dynamic> message) {
    if (!message.containsKey("notification")) {
      message["notification"] = {};
    }
    if (!message["notification"].containsKey("title") && message["data"].containsKey("title")) {
      message["notification"]["title"] = message["data"]["title"];
    }
    if (!message["notification"].containsKey("body") && message["data"].containsKey("body")) {
      message["notification"]["body"] = message["data"]["body"];
    }
    return message;
  }
  1. call that method first thing in your onMessage, onLaunch, and onResume functions.
    onResume: (Map<String, dynamic> message) async {
        // WORKAROUND
        message = fixMessageTitleAndBody(message);
        print("onResume: $message");
      },

This workaround requires two additional data fields to be sent from the console: "title" and "body". The workaround moves these two fields from the data section of the message to the notification (as if the message had them all along).

The advantage of this workaround is it won't require any code changes when Flutter fixes this issue. You simply need to provide the title and body in the flutter console as normal, and the workaround method do nothing.

@Ehesp Ehesp added plugin: messaging platform: android Issues / PRs which are specifically for Android. labels Apr 18, 2020
@helenaford helenaford added the impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) label Apr 27, 2020
@AntiqPeace
Copy link

@JacobFrericks even I am tired.... Workarounds, Workarounds, Workarounds...
Thanks for the help

@carlosfiori
Copy link

I am having the same issue using firebase_messaging: ^6.0.13

@ayush221b
Copy link

I have the same issue in firebase_messaging: ^7.0.0 but it is very interesting to note that the title and body does appear in the notification that appears when the app is in background or terminated, but when I print the message in my onResume and onLaunch callbacks, the notification key is an empty map.

@jpiabrantes
Copy link

I have this problem for iOs but it works fine on Android.

@jvincek
Copy link

jvincek commented Oct 8, 2020

This is still happening when using firebase_messaging: ^7.0.3 (Android 11, Pixel 3a).
Could someone take a look at this? It has been labeled with severe: regression for more than half a year now.

@Fibonacci-Solucoes-Ageis

I have the same error on Android using firebase_messaging: ^7.0.3.

@hiranthaPeiris
Copy link

This issue still happening with flutter on firebase_messaging: ^7.0.3. alternatively can use message Custom data to send the same data

@vedatsalmis
Copy link

I have the same problem. I could not get the title and body when notification is send from firebase console. onMessage there is no problem but onResume and onLaunch notification is empty like ''{notification: {}, data: {collapse_key: com.exaple.vedat, google.original_priority: high, google.sent_time: 1603976954143, google.delivered_priority: high, google.ttl: 2419200, from: 117431216057, click_action: FLUTTER_NOTIFICATION_CLICK, google.message_id: 0:1603976954318920%8a5620e18a5620e1}}"
Before it is printed on the console ı had a message like this;
W/FirebaseMessaging( 2431): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.

@alexmazza
Copy link

I have this problem for iOs but it works fine on Android.

Because all things should be balanced, I have the same problem on Android but not on iOS.

@jdd2405
Copy link

jdd2405 commented Jan 8, 2021

I have the same problem but now it doesen't even work with onMessage().
What am I doing wrong?

@Andreigr0
Copy link

Problem is still present (at least on Android 10) with version 7.0.3 😒

@russellwheatley
Copy link
Member

Hey @JacobFrericks, we're now on version 9 and the onResume & onLaunch handlers do not exist anymore. I'm going to close this, but if you come across any other problems, please feel free to create a new issue.

@firebase firebase locked and limited conversation to collaborators May 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) platform: android Issues / PRs which are specifically for Android. plugin: messaging type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests