Flutter 推送通知应用程序背景:firebase_messaging

Flutter push notifications app background : firebase_messaging

我想在我的应用程序处于后台时显示推送通知。
我正在使用 flutter_local_notifications package and the firebase_messaging 包。

推送通知在 firebase_messaging 和我的应用程序作为后台时运行良好。
但是,下面的方法:

 // The following handler is called, when App is in the background.
 FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
如果 RemoteNotification 对象通过 RemoteMessage 传递,则

不会被调用:

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print('message from background handler');
  print("Handling a background message: ${message.messageId}");
  
  // If this is null, then this handler method is called
  RemoteNotification remoteNotification = message.notification; 

  showNotificationFromData(message.data);
}

因此,我必须传递 message.data 对象,它是 Map<String, dynamic>.

我的问题是调用此 firebaseMessagingBackgroundHandler 处理程序方法时我不再收到推送通知。

所以我一直在尝试使用 flutter_local_notification 包来显示推送通知,但正如所说,它是“本地”的,所以它在前台工作正常但显然不在后台(相同代码,具有相同数据的情况不会在后台显示为推送通知)。

问题:

我需要调用 firebaseMessagingBackgroundHandler 处理程序来处理我的应用程序中的事件。所以当我的应用程序在后台运行时我仍然可以收到推送通知吗?

谢谢

好的,我找到了解决方案。就是在服务器端代码中将content_available的值设置为True

要替换 firebase_messaging.Notification 项,我们需要覆盖 androidapns 配置。

这在 python 中看起来像这样:

apnsConfig = messaging.APNSConfig(
            payload=messaging.APNSPayload(
                aps=messaging.Aps(
                    content_available=True,
                )
            ),
     
        )

我现在正在接收推送通知并在 dart 代码中调用后台处理程序。

我知道这有点太晚了,但任何人都试图使用 firebase 获取自定义通知,那么这里就是解决方案

对于Android (Flutter)

在 Android,通知消息被发送到 Notification Channels,用于控制通知的发送方式。使用的默认 FCM 通道对用户是隐藏的,但提供了“默认”重要性级别。注意通知需要“最高”重要性级别。

这意味着我们需要先创建一个具有最高重要性级别的新频道,然后将传入的 FCM 通知分配给该频道。我们可以利用 flutter_local_notifications

  1. flutter_local_notifications 包添加到您的本地项目。
  2. 创建一个新的 AndroidNotificationChannel 实例:
const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'high_importance_channel', // id
  'High Importance Notifications', // title
  'This channel is used for important notifications.', // description
  importance: Importance.max,
);

(请记住这是示例代码,但您可以通过通知渠道代码自定义通知)

  1. 在设备上创建频道:
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

    await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);
  1. 创建后,我们现在可以更新 FCM 以使用我们自己的频道 比默认的 FCM 一个。为此,打开 android/app/src/main/AndroidManifest.xml 文件给你 FlutterProject 项目。在其中添加以下 meta-data 架构 application 分量:
    <meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="high_importance_channel" />

现在,每当您发送通知时指定您的频道 ID,您的应用程序会自动显示您的自定义通知(通过 api 或 firebase 控制台)

{
   "message":{
      "token":"token_1",
      "android_channel_id":"high_importance_channel" //specify channel id here
      "data":{},
      "notification":{
        "title":"FCM Message",
        "body":"This is an FCM notification message!",
      }
   }
}

至于ios他们不支持通知渠道我没有必要的资源和信息如何工作如果你们知道下面的评论会很多应用