【问题标题】:Flutter - Call Firebase onMessage method when the app is in backgroundFlutter - 当应用程序处于后台时调用 Firebase onMessage 方法
【发布时间】:2020-01-25 09:06:41
【问题描述】:

问题与Call onMessage method when the app is in background in flutter 相同,我正在关注该主题,但由于没有回应,我再次问这个问题。

我已将我的 Flutter 应用程序与 Firebase Messaging 连接,当我的应用程序位于 前台 时,我使用 Flutter Local Notifications Plugin 收到通知。但是当我的应用处于后台

时,我无法收到任何通知

以下是我的代码:

// FIREBASE CONFIGURATION & HANDLING
_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    debugPrint("onMessage: $message");
    final notification = message['data']['notification'];

    Map responseBody = convert.jsonDecode(notification);

    _showNotification(
      icon: responseBody['icon'],
      title: responseBody['title'],
      body: responseBody['body'],
    );

    setState(() {});
  },
);


// DISPLAY NOTIFICATION - HEADS UP
Future<void> _showNotification({String icon, String title, String body}) async {
  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,
    title,
    body,
    platformChannelSpecifics,
  );
}

有人可以指导我如何在我的应用处于后台

时接收通知

【问题讨论】:

标签: android firebase flutter push-notification firebase-cloud-messaging


【解决方案1】:

已解决!!

问题在于 curl 调用的格式。按照here的说明进行

改变了这个:

{
  "data": {
    "notification": {
        "title": "Notification Title",
        "body": "Notification Body",
    },
        "click_action": "FLUTTER_NOTIFICATION_CLICK"
  },
  "to": "YOUR_DEVICE_TOKEN"
}

到这里:

{
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "id": "1",
    "status": "done",
  },
  "notification": {
    "body": "Notification Body",
    "title": "Notification Title"
  },
  "priority": "high",
  "to": "YOUR_DEVICE_TOKEN"
}

【讨论】:

    猜你喜欢
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2017-03-10
    • 2017-01-26
    • 2020-05-08
    • 2012-01-11
    • 2021-03-11
    相关资源
    最近更新 更多