【问题标题】:Flutter Firebase Background MessagingFlutter Firebase 后台消息
【发布时间】:2021-07-09 02:37:14
【问题描述】:

我想使用 onBackgroundMessage,所以我的想法是,如果在应用程序最小化或终止时收到通知,我尝试使用 AudioPlayer 包从资产中播放一些音频文件,但是当颤振运行时出现这样的错误:

E/flutter (31608): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] 未处理异常:空值检查运算符用于空值 E/flutter (31608): #0 MethodChannelFirebaseMessaging.registerBackgroundMessageHandler

尽管出现上述错误,但在 Flutter 运行时应用程序仍然可以运行。当我尝试发送通知时,应用程序收到了标题上的通知,但我收到了另一条这样的消息

由于没有注册 onBackgroundMessage 处理程序,因此无法在 Dart 中处理后台消息。 AndroidManifest 中缺少默认通知通道元数据。将使用默认值。

我从不更改 android 文件夹或 ios 文件夹中的任何内容。请帮我解决这个问题

这是我的飞镖代码:

  FirebaseMessaging messaging = FirebaseMessaging.instance;
  AudioCache audioCache = AudioCache();
  AudioPlayer audioPlayer = AudioPlayer();

  Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
    await Firebase.initializeApp();
    print("Background message service");
    audioPlayer = await audioCache.play('dangeronehour.mp3');
  }

  void initMessaging() async {
    NotificationSettings settings = await messaging.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );
    messaging.getToken().then((value) => print(value));
    print('${settings.authorizationStatus}');
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initMessaging();
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print('Message in the foreground!');
      print('Message data: ${message.data}');
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
      print('App opened');
      audioPlayer = await audioCache.play('dangeronehour.mp3');
    });
  }

我使用的 Firebase 软件包

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  firebase_core: ^1.0.3
  firebase_messaging: ^9.1.1
  flutter_local_notifications:
  audioplayers:

颤振医生

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 2.1.0-13.0.pre.586, on Mac OS X 10.15.2 19C57 darwin-x64, locale en)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[!] Xcode - develop for iOS and macOS
    ✗ Xcode 11.6.0 out of date (12.0.1 is recommended).
      Download the latest version or update via the Mac App Store.
    ! CocoaPods 1.8.4 out of date (1.10.0 is recommended).
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To upgrade see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] Connected device (2 available)

颤振版本

Flutter 2.1.0-13.0.pre.586 • channel master • https://github.com/flutter/flutter.git
Framework • revision fbf3c4e41c (26 hours ago) • 2021-04-12 17:42:25 -0700
Engine • revision 8863afff16
Tools • Dart 2.13.0 (build 2.13.0-222.0.dev)

【问题讨论】:

    标签: android firebase flutter dart firebase-cloud-messaging


    【解决方案1】:

    把它放在你会在 android 文件夹中找到的 AndroidManifest.xml 中:

       <intent-filter>
             <action android:name="FLUTTER_NOTIFICATION_CLICK" />
             <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
    

    你可以把它放在活动的最后,但在你关闭活动之前

    【讨论】:

    • 我已按照您的指示进行操作,现在我收到了消息:W/FLTFireMsgService(4915):由于未注册 onBackgroundMessage 处理程序,因此无法在 Dart 中处理后台消息。 W/FirebaseMessaging(4915):应用程序尚未创建 AndroidManifest.xml 中设置的通知通道。将使用默认值。当我收到通知然后我单击顶部标题上的通知并且应用程序没有出现我也收到这样的消息:E/CloudMessagingReceiver(4915): Notification pending intent cancelled
    • 您是为 iOS 开发还是为 Android 开发?
    • 我正在为安卓开发
    猜你喜欢
    • 2020-05-15
    • 2020-05-15
    • 2019-01-10
    • 2019-12-12
    • 2021-10-20
    • 2020-05-17
    • 2021-04-18
    • 2018-09-07
    • 2021-03-12
    相关资源
    最近更新 更多