You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter如何展示Node.js发送的后台通知?已配置仍无法显示

问题排查与解决方案

1. 后台消息未触发通知展示

你的_firebaseMessagingBackgroundHandler仅打印日志,未处理通知展示逻辑。当App处于后台或被杀死状态时,后台消息需要手动调用本地通知插件来弹出通知:

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print("Handling a background message: ${message.data}");

  RemoteNotification? notification = message.notification;
  if (notification != null) {
    await flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      NotificationDetails(
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          channel.description,
          icon: '@mipmap/ic_launcher', // 需确保项目存在对应图标资源
        ),
      ),
    );
  }
}

2. 补全Android通知配置细节

在AndroidNotificationDetails中补充图标、优先级等关键配置,确保通知能正常显示:

AndroidNotificationDetails(
  channel.id,
  channel.name,
  channel.description,
  icon: '@mipmap/ic_launcher', // 替换为你项目的通知图标路径
  importance: Importance.max,
  priority: Priority.high,
  showWhen: true,
)

3. 调整通知通道创建顺序

将创建通知通道的代码移到消息监听之前,保证通道在接收消息前已完成初始化:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.android,
  );

  // 先创建通知通道
  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  await FirebaseMessaging.instance.subscribeToTopic("newService");

  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    print('for');
    RemoteNotification? notification = message.notification;

    if (notification != null) {
      flutterLocalNotificationsPlugin.show(
          notification.hashCode,
          notification.title,
          notification.body,
          NotificationDetails(
            android: AndroidNotificationDetails(
              channel.id,
              channel.name,
              channel.description,
              icon: '@mipmap/ic_launcher',
              importance: Importance.max,
              priority: Priority.high,
            ),
          ));
    }
  });
  
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  runApp(MyApp());
}

4. 校验Node.js消息格式

确保Node.js发送的消息包含notification字段,若仅发送data字段,message.notification会为空,需手动从data中提取内容构建通知:

// Node.js示例:包含notification字段的合法推送消息
admin.messaging().send({
  topic: "newService",
  notification: {
    title: "通知标题",
    body: "通知内容"
  },
  data: {
    key: "value"
  },
  android: {
    priority: "high" // 高优先级保证后台能收到消息
  }
});

5. 确认通知权限已获取

添加权限请求代码,确保App已获得通知展示权限:

void main() async {
  // ... 其他初始化代码

  NotificationSettings settings = await FirebaseMessaging.instance.requestPermission(
    alert: true,
    badge: true,
    sound: true,
  );
  print('User granted permission: ${settings.authorizationStatus}');

  // ... 后续逻辑
}

内容的提问来源于stack exchange,提问作者Ahmed Ebrahim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 18:47:14