flutter - 在Flutter中处理Firebase Data推送通知

标签 flutter dart

我想从Firebase向Flutter应用发送数据类型推送通知。根据文档,我们需要处理此类通知,以便在通知中心显示它们。但是没有文件说明我们如何做到这一点。

在Flutter中处理数据类型推送通知是否可行?有没有人尝试过onbackgroundMessage函数实现?

最佳答案

使用official firebase_messaging library来使用推送通知,您也可以在那里找到不错的文档。
您可以像这样接收数据或通知有效载荷,

Define a TOP-LEVEL or STATIC function to handle background messages


 Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
   if (message.containsKey('data')) {
     // Handle data message
     final dynamic data = message['data'];
   }

   if (message.containsKey('notification')) {
     // Handle notification message
     final dynamic notification = message['notification'];
   }

   // Or do other work.
 }

Set onBackgroundMessage handler when calling configure


 _firebaseMessaging.configure(
       onMessage: (Map<String, dynamic> message) async {
         print("onMessage: $message");
         _showItemDialog(message);
       },
       onBackgroundMessage: myBackgroundMessageHandler,
       onLaunch: (Map<String, dynamic> message) async {
         print("onLaunch: $message");
         _navigateToItemDetail(message);
       },
       onResume: (Map<String, dynamic> message) async {
         print("onResume: $message");
         _navigateToItemDetail(message);
       },
     );

要显示本地通知,请在该库中使用flutter_local_notifications
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'your channel id', 'your channel name', 'your channel description',
    importance: Importance.Max, priority: Priority.High, ticker: 'ticker');

var iOSPlatformChannelSpecifics = IOSNotificationDetails();

var platformChannelSpecifics = NotificationDetails(
    androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);

await flutterLocalNotificationsPlugin.show(
    0, 'plain title', 'plain body', platformChannelSpecifics,
    payload: 'item x');

关于flutter - 在Flutter中处理Firebase Data推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59015044/

相关文章:

flutter - Flutter 中的 LinearPercentIndicator

Firebase 服务器时间戳与本地(几乎)相同

android - 如何在flutter中打开android平台上的添加联系人页面?

dart - polymer Dart 与 polymer js : does it matter when using the component?

android - 插件和应用程序发布的 Flutter 问题

flutter - 测量两个位置之间的距离

Flutter AdMob Banner 广告重叠屏幕

即使模拟器打开也没有检测到任何设备

android - 如何为图标勾勒颜色,但您也可以填充颜色 [Flutter]

function - 外部OnTap/OnPressed传递数据(抖动)