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

kgsdhlau  于 2023-04-22  发布在  Flutter
关注(0)|答案(4)|浏览(267)

我正在尝试为Flutter应用程序实现firebase推送通知。
但是它只是在状态栏中显示为一个图标。我如何才能使它弹出?

我想在屏幕上弹出时收到通知。
下面是我的代码的样子:

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;
 }
}
gcuhipw9

gcuhipw91#

如果FCM HTTP API中没有指定android_channel_id,则所有通知都将发送到Miscellaneous通道,Miscellaneous通道默认重要性级别,只有声音,不会弹出屏幕。
要让通知弹出屏幕,需要创建一个重要性为max/high的通知通道。创建通知通道的详细步骤可以参考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"
}

col17t5w

col17t5w2#

我试图重现这个问题,并深入挖掘,从我发现你不能在Flutter中实现你想要的。虽然,你可以在Java/Kotlin中处理Firebase onMessageReceived,然后显示你想要的通知。
请参考这里:FCM for android: popup system notification when app is in background

am46iovg

am46iovg3#

创建重要性为max/high的通知通道。

AndroidNotificationChannel channel = AndroidNotificationChannel(
    Random.secure().nextInt(100000).toString(),
    "High Notification Channel",
   importance: Importance.max,
);
AndroidNotificationDetails androidNotificationDetails = AndroidNotificationDetails(
    channel.id.toString(),
    channel.name.toString(),
    channelDescription: "Notes App Channel",
    importance: Importance.high,
    priority: Priority.high,
    icon: '@mipmap/ic_launcher',
    ticker: 'ticker',
);
7fhtutme

7fhtutme4#

您可以检查天气应用程序是否处于电池优化模式。如果是,请将其从那里删除。
步骤如下:设置-〉电池-〉电池优化-〉“选择您的应用程序”-〉点击“唐的优化”
谢谢。

相关问题