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

Flutter应用关闭时无法接收Firebase推送通知问题求助

FCM推送通知异常问题

问题情况

我正在测试firebase_messaging的官方示例代码,目前遇到两个问题:

  • 应用关闭状态下无法接收PUSH通知,后台进程已创建,但LogCat无任何输出信息。
  • 2023年4月5日补充:应用处于运行状态时,接收PUSH通知会触发控制台错误,该错误可能与关闭状态下无法接收通知的问题相关,错误日志如下:

测试代码

FirebaseMessaging messaging = FirebaseMessaging.instance;

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

  await setupFlutterNotifications();
  showFlutterNotification(message);
  print('Handling a background message ${message.messageId}');
}

late AndroidNotificationChannel channel;
bool isFlutterLocalNotificationsInitialized = false;

Future<void> setupFlutterNotifications() async {
  if (isFlutterLocalNotificationsInitialized) {return;}
  channel = const AndroidNotificationChannel(
    'high_importance_channel', // id
    'High Importance Notifications', // title
    description: 'High Importance Notifications', // description
    importance: Importance.high,
  );
  flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);

  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true, badge: true, sound: true,);
  isFlutterLocalNotificationsInitialized = true;
}
void showFlutterNotification(RemoteMessage message) {
  RemoteNotification? notification = message.notification;
  AndroidNotification? android = message.notification?.android;
  if (notification != null && android != null && !kIsWeb) {
    flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      NotificationDetails(
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          channelDescription: channel.description,
          icon: 'launch_background',
        ),
      ),
    );
  }
}
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // 推送服务需要
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  await setupFlutterNotifications();
  runApp(const MyApp());
}

前台接收通知时的错误日志(中文翻译)

D/FLTFireMsgReceiver(11337): 收到消息广播
D/Android: [Awesome Notifications](11337): 新的Awesome FCM服务已启动(AwesomeFcmService:46)
D/Android: [Awesome Notifications](11337): 收到新的FCM推送(ID: 0:1680679410618292%59af665959af6659)(AwesomeFcmService:134)
E/Android: [Awesome Notifications](11337): 索引:0,大小:0(FcmInterpreter:58)
W/System.err(11337): me.carda.awesome_notifications.core.exceptions.AwesomeNotificationsException: 索引:0,大小:0
W/System.err(11337):    在 java.util.ArrayList.get(ArrayList.java:437)
W/System.err(11337):    在 me.carda.awesome_notifications_fcm.core.builders.FcmNotificationBuilder.getFirstAvailableChannelKey(FcmNotificationBuilder.java:149)
W/System.err(11337):    在 me.carda.awesome_notifications_fcm.core.builders.FcmNotificationBuilder.receiveStandardNotificationContent(FcmNotificationBuilder.java:131)
W/System.err(11337):    在 me.carda.awesome_notifications_fcm.core.builders.FcmNotificationBuilder.buildNotificationFromExtras(FcmNotificationBuilder.java:79)
W/System.err(11337):    在 me.carda.awesome_notifications_fcm.core.interpreters.FcmInterpreter.deliveryAwesomeNotification(FcmInterpreter.java:134)
W/System.err(11337):    在 me.carda.awesome_notifications_fcm.core.interpreters.FcmInterpreter.processPushContent(FcmInterpreter.java:100)
W/System.err(11337):    在 me.carda.awesome_notifications_fcm.core.interpreters.FcmInterpreter.executeRemoteInstructions(FcmInterpreter.java:79)
W/System.err(11337):    在 me.carda.awesome_notifications_fcm.core.services.AwesomeFcmService.passMessageIntentToSdk(AwesomeFcmService.java:174)
W/System.err(11337):    在 me.carda.awesome_notifications_fcm.core.services.AwesomeFcmService.handleMessageIntent(AwesomeFcmService.java:137)
W/System.err(11337):    在 me.carda.awesome_notifications_fcm.core.services.AwesomeFcmService.handleIntent(AwesomeFcmService.java:70)
W/System.err(11337):    在 com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0$com-google-firebase-messaging-EnhancedIntentService(EnhancedIntentService.java:78)
W/System.err(11337):    在 com.google.firebase.messaging.EnhancedIntentService$$ExternalSyntheticLambda1.run(未知来源:6)
W/System.err(11337):    在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err(11337):    在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err(11337):    在 com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@18.1.0:2)
W/System.err(11337):    在 java.lang.Thread.run(Thread.java:923)

内容的提问来源于Stack Exchange,提问作者Pavel Gribov

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 01:42:23