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

how to implement click action in firebase push notifications both foreground and background in flutter?

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

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

但在后台我无法使用“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”功能,但当您单击它时,它在前台停止工作,我检查了日志,甚至没有通过“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>