how to implement click action in firebase push notifications both foreground and background in flutter?
You have to specify the click_action
inside your js script, in which you have written the cloud functions, without specifying it, you would not be able to get onResume()
and onLaunch()
callbacks.
"data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", }
Make sure have this line of code in your Manifests.xml file.
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
![Jesus Marfil](https://lh3.googleusercontent.com/-AzJ9Hi3SvzQ/AAAAAAAAAAI/AAAAAAAAANo/07dbtMoSvZY/photo.jpg?sz=256)
Jesus Marfil
Updated on December 23, 2022Comments
-
Jesus Marfil over 1 year
I have a problem that I have not been able to solve yet It happens that I need to use the click action of the firebase push notifications both in the foreground and in the background
in the foreground I used the flutter_local_notifications library and everything worked fine
_firebaseMessaging.configure( onMessage: (Map<String, dynamic> message) async { ///HERE SHOW THE NOTIFICATION IN THE NOTIFICATION CENTER WITH flutter_local_notifications _showNotification(message); }, onBackgroundMessage: myBackgroundMessageHandler, onLaunch: (Map<String, dynamic> message) async { _openScreen(message); }, onResume: (Map<String, dynamic> message) async { _openScreen(message); }, );
but in the background I couldn't use the "onBackgroundMessage" so searching in forums I found that I needed to register the plugin from kotlin and I did it this way:
File Application.kt
class Application : FlutterApplication(), PluginRegistrantCallback { override fun onCreate() { super.onCreate() FlutterFirebaseMessagingService.setPluginRegistrant(this); FlutterMain.startInitialization(this) } override fun registerWith(registry: PluginRegistry?) { if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) { FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")); } } }
File MainActivity.kt
class MainActivity: FlutterActivity() { }
File AndroidManifest.xml
<application android:name=".Name" android:label="Negocio TinBin" android:icon="@mipmap/launcher_icon"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="FLUTTER_NOTIFICATION_CLICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application>
With this change it works without problems in the background and when you click if the "myBackgroundMessageHandler" function is executed, but it stopped working in the foreground, I check the log and not even go through "onMessage" of the firebase configuration Anyone have any idea what I could do to make it work both in the foreground and in the background?
https://github.com/FirebaseExtended/flutterfire/issues/2311
https://pub.dev/packages/firebase_messaging#receiving-messages
-
Jesus Marfil almost 4 yearsAfter hours looking for a solution, I decided to try to create a new project with the same code, it turns out that now if everything worked correctly, the only thing that occurs to me is that the initial flutter configurations had a problem with the version, and when creating a new project was solved
-
-
Madhavam Shahi almost 4 yearsPlease upvote and accept this as the answer if it helped :)