简体   繁体   English

Flutter 推送通知应用程序后台:firebase_messaging

[英]Flutter push notifications app background : firebase_messaging

I want to display a push notification when my app is in the background.我想在我的应用程序处于后台时显示推送通知。
I am using the flutter_local_notifications package and the firebase_messaging package.我正在使用flutter_local_notifications package 和firebase_messaging package。

Push notifications are working great with firebase_messaging and when my app is the background.推送通知与 firebase_messaging 以及我的应用程序作为后台时配合得很好。
However, the below method:但是,下面的方法:

 // The following handler is called, when App is in the background.
 FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

is not called if a RemoteNotification object is passed through the RemoteMessage :如果RemoteNotification object 通过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);
}

Therefore, i have to pass the message.data object which is a Map<String, dynamic> .因此,我必须传递message.data object 这是一个Map<String, dynamic>

My problem is that i don't receive push notification anymore when this firebaseMessagingBackgroundHandler handler method is called.我的问题是,当调用此firebaseMessagingBackgroundHandler处理程序方法时,我不再收到推送通知。

So i have been trying to use the flutter_local_notification package to display a push notification but as it is said, it's "local" so it's working fine in the Foreground but apparently not in the background (the same code, with the same data is not showing in the background as a push notification).所以我一直在尝试使用flutter_local_notification package 来显示推送通知,但正如所说,它是“本地”的,所以它在前台工作正常但显然不在后台(相同的代码,相同的数据未显示在后台作为推送通知)。

Question:问题:

I need to call the firebaseMessagingBackgroundHandler handler to handle events in my app.我需要调用firebaseMessagingBackgroundHandler处理程序来处理我的应用程序中的事件。 So i can do to still have push notifications when my app is in the background please?所以当我的应用程序在后台时,我可以做些什么来仍然有推送通知吗?

Thanks谢谢

Okay i have found the solution.好的,我找到了解决方案。 It was to set the content_available value to True in the server-side code.它是在服务器端代码中将content_available值设置为True

To replace the firebase_messaging.Notification item, we need to override to the android and apns configs.要替换firebase_messaging.Notification项,我们需要覆盖androidapns配置。

This looks like this in python:这在 python 中看起来像这样:

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

I am now receiving push notifications and call the background handler inside the dart code.我现在正在接收推送通知并调用 dart 代码中的后台处理程序。

I know this bit too late but anyone trying to get custom notification using firebase then here is the solution我知道这有点太晚了,但任何人都试图使用 firebase 获得自定义通知,那么这里是解决方案

For Android (Flutter)对于 Android(颤振)

On Android, notification messages are sent to Notification Channels which are used to control how a notification is delivered.在 Android 上,通知消息被发送到用于控制通知传递方式的通知通道 The default FCM channel used is hidden from users, however provides a "default" importance level.使用的默认 FCM 通道对用户是隐藏的,但提供了“默认”重要性级别。 Heads up notifications require a "max" importance level.注意通知需要“最大”重要性级别。

This means that we need to first create a new channel with a maximum importance level & then assign incoming FCM notifications to this channel.这意味着我们需要首先创建一个具有最高重要性级别的新通道,然后将传入的 FCM 通知分配给该通道。 we can take advantage of the flutter_local_notifications package我们可以利用flutter_local_notifications package

  1. Add the flutter_local_notifications package to your local project.flutter_local_notifications package 添加到本地项目。
  2. Create a new AndroidNotificationChannel instance:创建一个新的 AndroidNotificationChannel 实例:
const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'high_importance_channel', // id
  'High Importance Notifications', // title
  'This channel is used for important notifications.', // description
  importance: Importance.max,
);

(keep in mind this is example code but you can custimize notification through notification channel code) (请记住,这是示例代码,但您可以通过通知渠道代码自定义通知)

  1. Create the channel on the device:在设备上创建通道:
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

    await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);
  1. Once created, we can now update FCM to use our own channel rather than the default FCM one.创建后,我们现在可以更新 FCM 以使用我们自己的频道而不是默认的 FCM 频道。 To do this, open the android/app/src/main/AndroidManifest.xml file for your FlutterProject project.为此,打开 FlutterProject 项目的android/app/src/main/AndroidManifest.xml文件。 Add the following meta-data schema within the application component:application组件中添加以下meta-data模式:
    <meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="high_importance_channel" />

now whenever you send notification specify your channel id and your app automatically show your custom notification (through api or firebase console)现在,每当您发送通知时指定您的频道 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!",
      }
   }
}

as for ios they don't support notification channel i don't have necessary resources and info how things works if you guys know comment below will be much appriciated至于ios ,他们不支持通知渠道,我没有必要的资源和信息,如果你们知道下面的评论将会非常有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在 flutter 中使用操作按钮实现推送通知? 使用 firebase_messaging - Is there any way to implement push notifications with action buttons in flutter? Using firebase_messaging [FLUTTER][firebase_messaging] 仅在检查权限时接收“Android 12”的后台通知时出错 - [FLUTTER][firebase_messaging] Error receiving background notifications for "Android 12" just when permissions are checked FIREBASE_MESSAGING:当应用程序处于后台或已终止时,onBackgroundMessage 不处理通知 - FIREBASE_MESSAGING: onBackgroundMessage not handling notification when app is on Background or Terminated Flutter 错误:错误:无法解析 'package:firebase_messaging/firebase_messaging.dart' 中的 package 'firebase_messaging' - Flutter Error: Error: Could not resolve the package 'firebase_messaging' in 'package:firebase_messaging/firebase_messaging.dart' Flutter Firebase 消息(推送通知)不适用于 iOS 在现有应用程序中 - Flutter Firebase Messaging (Push Notifications) Not Working for iOS in an Already Existing Application Flutter with firebase_messaging - 如何在权限对话框中自定义消息? - Flutter with firebase_messaging - how to custom message on permission dialog? firebase_messaging/未知:Flutter IOS 发生未知错误 - firebase_messaging/unknown: An unknown error has occured on Flutter IOS flutter “firebase_messaging”和“localstorage”版本解决失败 - flutter "firebase_messaging" and "localstorage" version solving failed Flutter:如何在 firebase_messaging package 中定义通知通道? - Flutter: How to define notification channel in firebase_messaging package? Flutter Firebase 消息 - 应用打开时不显示推送通知 - Flutter Firebase messaging - push notification is not showing when app is open
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM