Skip to content

[firebase_messaging] onMessage is not fired for DATA message when app is in background - Android #2111

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

Closed
liri2006 opened this issue Mar 3, 2020 · 8 comments
Labels
impact: customer A bug with low impact (e.g. affecting only a few customers or has a workaround). (P3) plugin: messaging Stale Issue with no recent activity type: bug Something isn't working

Comments

@liri2006
Copy link

liri2006 commented Mar 3, 2020

Describe the bug

I've set up firebase_messaging and I'm able to receive messages from FCM in general. All the callbacks work just fine both on Android and iOS except onMessage when app is in background state.

Message I'm sending (through postman):

{ 
	"to": "<device token>",
	"priority": "high", 
	"data": {
		"id": 123,
		"status": "some status",
		"click_action": "FLUTTER_NOTIFICATION_CLICK", 
		"type": "AccountConfirmation"
	}
}

Message is received when app is in foreground, but not when in background.

(Also funny fact - when in foreground this message is received as {notification: {title: null, body: null}, data: ...., but maybe its just how FCM sends these types of messages - with title and body present but set to null)

I've tried different versions of firebase_messaging package, 6.0.12 included.

Also worth mentioning that background messages (onBackgroundMessage ones, but as far as I understand that is not the same as onMessage callback in background state) work well if configured. I've tested with and without that config.

To Reproduce
Steps to reproduce the behavior:

  1. Configure flutter project to use firebase_messaging (or just use example project from here https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging/example)
  2. Push app to background
  3. Send DATA message
  4. No message is received by application

Expected behavior
Application receives message from FCM

Flutter doctor
[√] Flutter (Channel dev, v1.15.3, on Microsoft Windows [Version 10.0.18363.657], locale en-US)
• Flutter version 1.15.3 at C:\flutter
• Framework revision 67826bdce5 (3 weeks ago), 2020-02-10 14:59:32 -0800
• Engine revision 6158f03ef5
• Dart version 2.8.0 (build 2.8.0-dev.8.0 514a8d4c84)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at C:\Users\admin\AppData\Local\Android\sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 28.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
• All Android licenses accepted.

[√] Android Studio (version 3.5)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 42.1.1
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] VS Code (version 1.42.1)
• VS Code at C:\Users\amunk\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.8.1

[√] Connected device (1 available)
• AOSP on IA Emulator • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)

• No issues found!

@liri2006 liri2006 added the type: bug Something isn't working label Mar 3, 2020
@iapicca
Copy link

iapicca commented Mar 9, 2020

Hi @liri2006
I see there's an open issue addressing the case you described.
Please follow up on that issue,
I'm closing the current one as duplicate.
If you disagree please write in the comments
and I will reopen it.
Thank you

@iapicca iapicca closed this as completed Mar 9, 2020
@liri2006
Copy link
Author

liri2006 commented Mar 9, 2020

@iapicca cases are not the same. Issue that you linked is about onBackgroundMessage callback while mine is about onMessage.
Documentation on firebase_messaging states:
image
And I'm not getting onMessage called. Maybe I'm just understanding it wrong and onMessage should not be called when app is in background, but onBackgroundMessage should be used instead to handle those types of messages.
But if that's the case then documentation is quite misleading and should be fixed.

@iapicca iapicca reopened this Mar 9, 2020
@akshaybenk
Copy link

akshaybenk commented Mar 11, 2020

This issue still persists

Channel stable, v1.12.13+hotfix.8
firebase_messaging: ^6.0.12
for a data message - onMessage gets fired twice when the notification is triggered from API call

@Ehesp Ehesp added plugin: messaging impact: customer A bug with low impact (e.g. affecting only a few customers or has a workaround). (P3) labels Apr 22, 2020
@Ehesp
Copy link
Member

Ehesp commented Apr 22, 2020

For data only, you need to set the message priority to "high" for android & set "content-available" for iOS, otherwise the devices class these as unimportant and ignore them if the app is in the background.

@xfumihiro
Copy link

@Ehesp I tried your suggestion but now I'm getting triggered twice on "onMessage" per push notification like #1911

here's my config:

{
  "token": [token],
  "notification": {
    "title": [title],
    "body": [body],
  },
  "data": {
    "some_data": [data]  
  },
  "android": {
    "notification": {
      "sound": "default"
    },
  },
  "apns": {
    "headers": {
      "apns-priority": 5,
    },
    "payload": {
      "aps": {
        "content-available": 1,
      },
    },
  },
}

@Boehrsi
Copy link

Boehrsi commented Jun 3, 2020

I can also confirm that this is not working with firebase_messaging: ^6.0.16

I'm using the following setup:

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, v1.17.2, on Microsoft Windows [Version 10.0.18363.836], locale de-DE)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Android Studio (version 4.0)
[√] Connected device (2 available)
await post('https://fcm.googleapis.com/fcm/send',
                      headers: <String, String>{
                        'Content-Type': 'application/json',
                        'Authorization': 'key=$serverToken',
                      },
                      body: jsonEncode(
                        <String, dynamic>{
                          'priority': 'high',
                          'data': <String, dynamic>{
                            'click_action': 'FLUTTER_NOTIFICATION_CLICK',
                            'id': '1',
                            'status': 'done'
                          },
                          'to': await firebaseMessaging.getToken(),
                        },
                      ),
                    );

If the app is in the foreground onMessage() is called. If the app is in the background (but not killed) onMessage() isn't called.

@naveenkumardot25
Copy link

I solved this issue

#2777 (comment)

@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
impact: customer A bug with low impact (e.g. affecting only a few customers or has a workaround). (P3) plugin: messaging Stale Issue with no recent activity type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants