Flutterfire: [firebase_messaging] OnLaunch and OnResume do not fire on Android

Created on 20 Jan 2020  Â·  12Comments  Â·  Source: FirebaseExtended/flutterfire

Describe the bug
On both a simulator and a real device, onLaunch and onResume do not fire. I have tested this in Android only, at this point.

To Reproduce
Steps to reproduce the behavior:

  1. 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.3+2 and firebase_messaging: ^6.0.9
  2. 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
  3. 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

  1. Go to your firebase cloud_messaging page, and send a notification with the notification title test_title and the notification text test_text.
  2. Click "Send test message", and paste in the token. Click "Test".
  3. Observe the console. on_message appears, with the correct title and body.

// OnResume

  1. push the home button on the device, sending the application into the background.
  2. Repeat steps 4-5.
  3. Press the notification, which will open the app.
  4. Observe the console. No new messages appear. You are sent to the home page, rather than the items page, indicating that onResume did not trigger.

// OnLaunch

  1. press the square multitask button and swipe up/down on the application, completely closing it.
  2. Repeat steps 7-9.
  3. Observe the console. No new messages appear. You are sent to the home page, rather than the items page, indicating that onLaunch did not trigger.

Expected behavior
The expected behavior is the same for both onResume and onLaunch. When the notification is pressed, onResume/onLaunch messages should appear and you should be sent to the items page. Instead, no messages appear and you are sent to the home page.

Flutter Doctor:

[✓] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.15.2 19C57,
    locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
[✓] Android Studio (version 3.5)
[✓] IntelliJ IDEA Ultimate Edition (version 2019.2.2)
[✓] Connected device (1 available)

• No issues found!
bug

Most helpful comment

@GrahamDi Thank you for your answer, that is indeed what fixed this!

FWIW I didn't think anything in a "data" json applied to me when looking for answers online because I was sending this through the console, not a cloud function. After your answer, I did some more digging, and it seems like the Additional Options section in the console adds the data JSON to the request.

Here's a picture of what I changed in the console:
Screen Shot 2020-01-31 at 1 37 01 PM

Also, although unintuitive, once you change this option, you can scroll back up to Step 1 and send a test notification to a single device using its token. It will apply this change as well.

Thanks again, I'll close this issue.

All 12 comments

Move the _firebaseMessaging.configure() block of code before super.initState();

@superakabo Thank you for the suggestion! I moved super.initState(); to be last in the initState method, but nothing changed.

+1

@JacobFrericks Try adding click_action : FLUTTER_NOTIFICATION_CLICK as a k, v pair in the additional options section and then resending the messages from the console.

@GrahamDi Thank you for your answer, that is indeed what fixed this!

FWIW I didn't think anything in a "data" json applied to me when looking for answers online because I was sending this through the console, not a cloud function. After your answer, I did some more digging, and it seems like the Additional Options section in the console adds the data JSON to the request.

Here's a picture of what I changed in the console:
Screen Shot 2020-01-31 at 1 37 01 PM

Also, although unintuitive, once you change this option, you can scroll back up to Step 1 and send a test notification to a single device using its token. It will apply this change as well.

Thanks again, I'll close this issue.

@JacobFrericks Glad I could help. Took me a few months to get to the bottom of why this was happening (kept parking the issue and doing other things after each couple of hours of Googling). Maybe we should should raise an issue about FCM documentation being patchy at best.

@JacobFrericks Try adding click_action : FLUTTER_NOTIFICATION_CLICK as a k, v pair in the additional options section and then resending the messages from the console.

can you explain a bit more about what you are suggesting to do? I am having the exact same problem.

let payload = { "name":"Prototype", "notification": { "title": fname + "pressed button", "body":Keep ur phone handy.\nTap to track ${fname} details, "image": picture }, "data": { "click-action": "FLUTTER_NOTIFICATION_CLICK", "user_id": userId, }, "android": { "priority": "high", "notification": { "color": "#FF4040", "sound": "default", "sticky": true, "notification_priority": "priority_max", "default_sound": true, "default_vibrate_timings": true, "default_light_settings": true, "visibility": "public", /*"light_settings": { /!*"color": { "red": 255, "green": 0, "blue": 0, "alpha": 0 },*!/ "light_on_duration": "1.0s", "light_off_duration": "1.0s" }*/ }, }, "token": snap.data().NotifyToken };

this is my notification payload in cloud functions. and I have also added FLUTTER_NOTIFICATION_CLICK in Android manifest file in .

@Pratiknarola see my previous post. It gives a snapshot of what to add in the firebase console.

To clarify, I did not have to make any code changes. I just had to add what is in the snapshot into the firebase console before sending the notification.

Screen Shot 2020-01-31 at 1 37 01 PM

you mean this??

I have deployed a function on the trigger on document change so whenever cloudstore document changes this function gets called and this payload is sent as notification. onMessage works great. onResume and onLaunch never gets called and on notification tap main screen opens and nothing happens.

please can you explain all the steps you did exactly to solve this problem.

@Pratiknarola your code states 'click-action', it should be 'click_action'. Also why does the 'body:' text appear to have no double quotes around it?

Was this page helpful?
0 / 5 - 0 ratings