Firebase推送通知未在屏幕上弹出[Flutter]

时间:2020-02-05 10:54:34

标签: android flutter dart

我正在尝试为Flutter应用程序实现Firebase推送通知。

但是它只是在状态栏中显示为图标。我如何使其弹出?

enter image description here

我想在收到通知时在屏幕上弹出。

这是我的代码的样子:

 Future<bool> sendNotification(var userToken, String bodyMessage) async {
 final data = {
  "notification": {
    "body": bodyMessage,
    "title": "Leave Application",
  },
  "priority": "high",
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "id": "1",
    "status": "done"
  },
  "registration_ids": userToken,
 };

 final headers = {
  'content-type': 'application/json',
  'Authorization':
  'key=<Firebase web key>',
 };

 final response = await http.post(postUrl,
    body: json.encode(data),
    encoding: Encoding.getByName('utf-8'),
    headers: headers);

 if (response.statusCode == 200) {
  print(response.statusCode.toString());
  print("Working");
  return true;
 } else {
  print(postUrl.toString());
  print(response.statusCode.toString());
  print("Not Working");
  return false;
 }
}

3 个答案:

答案 0 :(得分:1)

我试图重现问题并进行更深入的挖掘,从发现的内容中您无法达到Flutter所需要的。不过,您可以使用Java / Kotlin处理Firebase onMessageReceived,然后显示所需的通知。

请参考此处:FCM for android: popup system notification when app is in background

答案 1 :(得分:1)

如果未在 FCM HTTP API 中指定 android_channel_id,所有通知都将发送到杂项通道。 杂项频道具有默认的重要性级别,只有声音但不会在屏幕上弹出。

要使通知在屏幕上弹出,您需要创建一个具有最大/高重要性的通知渠道。有关如何创建通知渠道的详细步骤,您可以参考Notification Channel Documentation by FlutterFire

通知频道创建后,您可以将频道 ID(例如:high_importance_channel)添加到 FCM HTTP API 请求正文中,当您的应用程序处于后台时,发送到该频道的通知应该开始在屏幕上弹出。

{
  "notification": {
    "android_channel_id": "high_importance_channel",
    "title": "Notification Title",
    "body": "Notification body ...",
    "sound": "default",
    "priority": "high",
    "time_to_live": 86400,
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
  },
  "data": {
    "post_id": 10000012,
    "type": "NEWS",
  },
  "condition": "'dogs' in topics"
}

1st image 2nd image

答案 2 :(得分:-1)

您可以检查天气应用是否处于电池优化模式。 如果是,请从那里删除它。

步骤如下: 设置->电池->电池优化->“选择您的应用”->单击“ Don's Optimise”

谢谢。