首页 > 解决方案 > 使用真实设备和缺少默认通知通道时,Firebase FCM 声音不起作用

问题描述

我对 fcm 中的通知声音有一些问题。发送通知时,它不会播放任何声音。震动没问题,只是声音没了。这是我来自节点 js 的有效负载。

 var payload = {
      notification: {
         title   : 'Antrian ' + data.dokter,
         body    : "Pasien : " + pasien.id + '\nAntrian Anda Kurang : ' + counter + "\nAntrian Sekarang : " + noAntrian,
         sound   : "betelgeuse.ogg"
      }
 };

在应用程序中,我还收到缺少默认频道警告。我已经把细节放在了 androidManifest

  <meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="@string/default_notification_channel_id" />

并在 res/values/ 中创建 string.xml

<?xml version='1.0' encoding='utf-8'?>
<resources>
    <string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
</resources>

这就是我处理消息的方式

FirebaseMessaging.onBackgroundMessage(LocalNotification.showNotification);


static Future<void> showNotification(RemoteMessage payload) async {
    var android = AndroidInitializationSettings('logo_rs');
    var initiallizationSettingsIOS = IOSInitializationSettings();
    var initialSetting = new InitializationSettings(android: android, iOS: initiallizationSettingsIOS);
    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initialSetting);

    // Daftar jenis notifikasi dari aplikasi.

    const AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
        'default_notification_channel_id',
        'Notification',
        'All Notification is Here',
        importance: Importance.max,
        priority: Priority.high,
        ticker: 'ticker',
        icon: "logo_rs",
        sound: RawResourceAndroidNotificationSound('betelgeuse'),
        playSound: true,
    );
    const iOSDetails = IOSNotificationDetails();
    const NotificationDetails platformChannelSpecifics = NotificationDetails(android: androidDetails, iOS: iOSDetails);
    // Menampilkan Notifikasi
    await flutterLocalNotificationsPlugin.show(0, payload.notification!.title, payload.notification!.body, platformChannelSpecifics);
  }

也许我错过了一些东西,所以任何建议都值得赞赏。谢谢你。

** 编辑:尝试使用 Pixel Emulator 并且它工作,当使用我自己的设备时,它不工作(当前使用 Pco F1)

标签: flutterfirebase-cloud-messaging

解决方案


推荐阅读