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] Message lost when app terminated #3091

Closed
alejandroaap opened this issue Aug 4, 2020 · 7 comments
Closed

[firebase_messaging] Message lost when app terminated #3091

alejandroaap opened this issue Aug 4, 2020 · 7 comments
Labels
plugin: messaging Stale Issue with no recent activity type: documentation Improvements or additions to documentation type: question A question where further information is requested

Comments

@alejandroaap
Copy link

alejandroaap commented Aug 4, 2020

In the Readme file of cloud_messaging plugin, which can be found at https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging#receiving-messages , it is referenced that when app is terminated the data message on Android device is lost because the plugin not supported. In the case of ios the message is stored by FCM and delivered to app via onMessage when the app is brought back to foreground.

My question is, Are you going to implement the option to handle notifications with data when the application is finished?

@LasseRosenow
Copy link

I think the readme for android is wrong. In my case handling data messages even when the app is terminated worked after doing the following steps:

Applikation.kt

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.FlutterFirebaseMessagingService
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin

class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {

  override fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingService.setPluginRegistrant(this)
  }

  override fun registerWith(registry: PluginRegistry?) {
    if (registry != null) {
      FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
      FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"))
    }
  }
}

Also I had to set "priority" to "high" and "content_available" to "true" in the message payload:

{
	"to": "/topics/schulinfoapp_beta-v4_283",
	"data": {
		"dataid": "Test data",
		"moduleId": "113a20f6-08ce-47a5-a85c-b0179b6b862a"
	},
	"priority": "high",
	"mutable_content": true,
	"content_available": true,
	"time_to_live": 86400
}

@LasseRosenow
Copy link

LasseRosenow commented Aug 4, 2020

I also want to mention that:
FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))

merely is a workaround and was used to be done by the GeneratedPluginRegistrant back in the days. But since android embedding v2 its not done by the GeneratedPluginRegistrant anymore because it should work without that, but it doesn't ... so we have to hack around it for a little bit right now :(

Would be cool if that would be fixed

@darshankawar darshankawar added plugin: messaging type: documentation Improvements or additions to documentation type: question A question where further information is requested labels Aug 4, 2020
@cuong292
Copy link

cuong292 commented Oct 2, 2020

@lazylazyllama its can show noti,but not possible to get the data of it

@LasseRosenow
Copy link

LasseRosenow commented Oct 2, 2020

@lazylazyllama its can show noti,but not possible to get the data of it

Yes because normal notifications don't need dart code to be executed in the background.

But data messages need to be handled by the dart background service, which needs the plugin to implement v2 embedding correctly. There already is an open pr for that. (#3572)

@cuong292
Copy link

cuong292 commented Oct 2, 2020

@lazylazyllama any work arround for it yet? something like native stuff which get notification from Java code to Flutter ? I really need this for my application

@LasseRosenow
Copy link

I think the readme for android is wrong. In my case handling data messages even when the app is terminated worked after doing the following steps:

Applikation.kt

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.FlutterFirebaseMessagingService
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin

class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {

  override fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingService.setPluginRegistrant(this)
  }

  override fun registerWith(registry: PluginRegistry?) {
    if (registry != null) {
      FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
      FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"))
    }
  }
}

Also I had to set "priority" to "high" and "content_available" to "true" in the message payload:

{
	"to": "/topics/schulinfoapp_beta-v4_283",
	"data": {
		"dataid": "Test data",
		"moduleId": "113a20f6-08ce-47a5-a85c-b0179b6b862a"
	},
	"priority": "high",
	"mutable_content": true,
	"content_available": true,
	"time_to_live": 86400
}

This code did the trick for me. That's all I can do for you. There also are tons of open issues with many many workarounds that all are somewhat similar. You should get it to be working after some playing around. Also keep in mind, that not only firebase is doing it wrong but also plugins like flutter local notifications have similar issues. So make sure that firebase really is the issue in your case.

@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 type: question A question where further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants