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

使用Ionic Native FCM插件自定义通知音效失败求助

Troubleshooting Custom FCM Notification Sounds in Ionic

Hey there, let's work through your custom sound issue with Ionic Native FCM step by step. I've dealt with this exact problem before, so let's break down what might be going wrong.

First: Does Custom Sound Only Play in Background?

Great question! Here's the clear breakdown:

  • Background/Killed State: When your app is in the background or fully closed, FCM delivers the notification directly to the Android/iOS system. The system handles playing the sound, so your custom sound should work here if configured correctly.
  • Foreground State: By default, FCM doesn't trigger a system notification when your app is active—you have to build and show a local notification manually using Ionic's LocalNotifications plugin. That means you also need to explicitly set the custom sound in your local notification code; the FCM payload's sound won't auto-play here.

Common Issues & Fixes for Your Setup

1. Verify Your Sound File Placement & Format (Android)

You mentioned placing alarm1.mp3 in res/raw, but double-check these critical details:

  • Correct Directory: The file must live in platforms/android/app/src/main/res/raw (not just a root res folder in your Ionic project). If you added it to your Ionic src folder, you need to ensure it's being copied to the Android platform directory during build. You can either:
    • Manually copy the file to the platforms/android/app/src/main/res/raw folder for quick testing, or
    • Add a copy script in your config.xml to automate this:
      <resource-file src="src/assets/sounds/alarm1.mp3" target="app/src/main/res/raw/alarm1.mp3" />
      
  • File Requirements: The MP3 must be a standard format (128kbps, 44.1kHz is safe), and the filename should be lowercase with no spaces or special characters (your alarm1 is perfect).

2. Check FCM Payload Configuration

The way you set the sound field depends on whether you're sending a notification payload or data payload:

  • Notification Payload (Handled by System):
    For Android, use the filename without the extension in your FCM payload:
    {
      "notification": {
        "title": "Test Notification",
        "body": "Custom sound test",
        "sound": "alarm1"
      },
      "to": "DEVICE_TOKEN"
    }
    
    This will trigger the system to play your custom sound when the app is in background/killed state.
  • Data Payload (Handled by Your App):
    If you're sending only a data payload (or removing the notification payload), the system won't auto-process the sound. You need to listen for the FCM event and create a local notification with the sound explicitly set:
    import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
    import { FCM } from '@ionic-native/fcm/ngx';
    
    constructor(private fcm: FCM, private localNotifications: LocalNotifications) {}
    
    // Subscribe to FCM notifications
    this.fcm.onNotification().subscribe(data => {
      if (data.wasTapped) {
        // App was in background, user tapped notification
      } else {
        // App is in foreground—show local notification with custom sound
        this.localNotifications.schedule({
          title: data.title,
          text: data.body,
          sound: 'res://raw/alarm1' // Android-specific path
        });
      }
    });
    

3. Android 13+ Notification Permissions

If you're targeting Android 13 or higher, your app needs the POST_NOTIFICATIONS permission to show notifications (and play their sounds). Add this to your AndroidManifest.xml (in platforms/android/app/src/main/):

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

And request the permission at runtime in your Ionic code:

import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';

constructor(private androidPermissions: AndroidPermissions) {}

// Request permission when app starts
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.POST_NOTIFICATIONS)
  .then(result => {
    if (result.hasPermission) {
      console.log('Notification permission granted');
    }
  })
  .catch(err => console.error(err));

4. Test the Right Scenarios First

Start testing with your app in the background or fully closed—this is where the system handles the notification, so it's easier to confirm if the custom sound works. Once that's working, move on to fixing the foreground behavior with local notifications.

Final Checklist

  • Sound file is in platforms/android/app/src/main/res/raw
  • Filename is lowercase, no special characters, format is valid
  • Notification payload uses sound: "alarm1" (no extension) for Android
  • Data payload triggers a local notification with explicit sound set
  • Android 13+ has POST_NOTIFICATIONS permission granted

内容的提问来源于stack exchange,提问作者Tom Reichstetter

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:41:47