问题描述
我有一个尚无法解决的问题 碰巧我需要在前台和后台使用firebase推送通知的click操作
在前台,我使用了 Flutter_local_notifications 库,并且一切正常
_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,);
但是在后台我无法使用“ onBackgroundMessage”,因此在论坛中搜索时发现我需要从kotlin注册该插件,并且我是这样进行的:
文件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"));
}
}
}
文件MainActivity.kt
class MainActivity: FlutterActivity() {
}
文件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>
通过此更改,它可以在后台正常运行,并且在单击“ myBackgroundMessageHandler”功能后单击该功能,但在前台停止运行时,我检查了日志,甚至没有经过firebase配置的“ onMessage” 任何人都知道我要怎么做才能使其在前台和后台都能正常工作?
https://github.com/FirebaseExtended/flutterfire/issues/2311
https://pub.dev/packages/firebase_messaging#receiving-messages
解决方法
您必须在js脚本中指定click_action
,在其中编写了云函数,而没有指定它,将无法获得onResume()
和onLaunch()
回调
"data": {"click_action": "FLUTTER_NOTIFICATION_CLICK",}
确保在Manifests.xml文件中有这一行代码。
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>