如何在flutter中前台和后台的firebase推送通知中实现点击操作



我有一个问题还没能解决碰巧我需要在前台和后台使用firebase推送通知的点击操作

在前台,我使用了flatter_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, dynamic> message) async {
_openScreen(message);
},
);

但在背景中,我不能使用";onBackgroundMessage";所以在论坛上搜索,我发现我需要从kotlin注册插件,我这样做:

文件应用程序.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"));
}
}
}

文件主活动.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";函数被执行,但它在前台停止了工作,我检查了日志,甚至没有通过";onMessage";燃烧基地配置有人知道我能做些什么让它在前台和后台都能工作吗?

https://github.com/FirebaseExtended/flutterfire/issues/2311

https://pub.dev/packages/firebase_messaging#receiving-消息

您必须在编写云函数的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>

相关内容

  • 没有找到相关文章