Fcm notification shown by both Firebase_messaging sdk and flutter local notifications package in flutter

huangapple go评论73阅读模式
英文:

Fcm notification shown by both Firebase_messaging sdk and flutter local notifications package in flutter

问题

我正在尝试在Flutter中实现推送通知,我正在使用firebase_messaging来实现这一点,以及flutter_local_notification来在应用程序处于前台时显示通知。

我知道Firebase发送的消息内容如下:

{
   "message":{
      "token":"token_1",
      "data":{},
      "notification":{
        "title":"FCM消息",
        "body":"这是一条FCM通知消息!",
      }
   }
}

firebase_messaging会在应用程序后台或终止状态下的消息中自动创建通知。

当从Firebase控制台或服务器发送通知和数据时,FCM SDK本身会创建两个通知,而flutter_local_notification插件会在应用程序后台或终止状态下分别创建通知。

由于FCM SDK创建的通知的自定义性较少,因此我希望仅使用flutter_local_notification来显示通知。因此,服务器只发送数据通知,但当应用程序在后台或终止状态下时(不在后台运行)时,应用程序无法捕获它。

因此,我的要求是:

  1. 阻止firebase_messaging在从服务器或Firebase控制台中的消息中同时发送数据和通知时创建通知。

或者

  1. 在应用程序后台或终止状态下能够捕获仅包含数据的消息,并使用flutter_local_notification插件显示通知。

我已经努力解决这个问题好几个月了,请帮忙。

英文:

I am trying to implement push notifications in flutter, I am using firebase_messaging for that and flutter_local_notification to show notification when the app is in foreground.

I know the contents of the message sent by firebase it is as follows:
{
"message":{
"token":"token_1",
"data":{},
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message!",
}
}
}

notification and data.

firebase_messaging creates notification automatically when notification is the part of the message when the app is background or terminated state.

There are two notification created by the FCM sdk itself and the flutter_local_notification plugin separately when the app is in background or terminated state, when both notification and data is sent from firebase console or from server.

I want to show the notification from the flutter_local_notification only due to the less customization available in the FCM sdk created notification for that the server is sending only data notification but the app is not able to catch it when the app is in background or terminated state (not running in background).

So, what I want is:

==> To prevent firebase_messaging from creating the notification when both data and notification is sent in the message from server or firebase console.

OR

==> To be able to catch the data only message from the server when the app is background or terminated state and show from the flutter_local_notification plugin.

Struggling through it from months please help.

答案1

得分: 2

为了防止 Firebase Messaging 在从服务器或 Firebase 控制台发送的消息中自动创建通知,您可以使用 FirebaseMessaging.configure 方法,并将 onMessage 回调设置为处理传入消息。

在 onMessage 回调中,您可以检查消息是否包含数据和通知字段,如果是,您可以选择忽略通知字段,而使用 flutter_local_notification 处理通知。

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    if (message.data.isNotEmpty && message.notification != null) {
        // 使用 flutter_local_notifications 显示通知
        showNotification(message.data['title'], message.data['body']);
    } else {
        // 正常处理消息
        // Firebase Messaging 会自动为没有数据和通知字段的消息创建通知
    }
});

这样,您可以使用 flutter_local_notification 自定义通知,同时仍然接收消息并适当处理它们。

另外,如果您希望在应用程序处于后台或终止状态时捕获仅包含数据的消息,并使用 flutter_local_notifications 插件显示它,您可以修改 FirebaseMessaging.onBackgroundMessage 监听器以实现此目的。

FirebaseMessaging.onBackgroundMessage((message) async {
  if (message.data != null) {
    // 使用 flutter_local_notifications 显示通知
    await showNotification(message.data['title'], message.data['body']);
  }
});
英文:

To prevent Firebase Messaging from automatically creating a notification when both data and notification are sent in the message from the server or Firebase console, you can use the FirebaseMessaging.configure method and set the onMessage callback to handle the incoming messages.

In the onMessage callback, you can check if the message contains both data and notification fields, and if so, you can choose to ignore the notification field and handle the notification using flutter_local_notification instead.

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    if (message.data.isNotEmpty && message.notification != null) {
        // Show notification using flutter_local_notifications
        showNotification(message.data['title'], message.data['body']);
    } else {
        // Handle the message as usual
        // Firebase Messaging will automatically create a notification for messages without both data and notification fields
    }
});

This way, you can customize the notification to your liking using flutter_local_notification, while still receiving messages and handling them appropriately.

Alternatively, if you want to catch the data-only message from the server when the app is in the background or terminated state and show it using the flutter_local_notifications plugin, you can modify your FirebaseMessaging.onBackgroundMessage listener to achieve this:

FirebaseMessaging.onBackgroundMessage((message) async {
  if (message.data != null) {
    // Show notification using flutter_local_notifications
    await showNotification(message.data['title'], message.data['body']);
  }
});

huangapple
  • 本文由 发表于 2023年3月9日 14:21:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定