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

如何让Expo React Native应用禁用FCM后台/杀死状态通知音效

解决React Native + Firebase后台/杀死状态下通知音效控制问题

问题根源

你当前发送的是Firebase Notification Message(服务器代码中包含Notification字段),这类消息由系统自动展示通知,即使未指定sound参数,系统也会播放默认通知音效。要实现用户可禁用音效的需求,需要从服务器端配置或改用Data Message两种方向入手。


方案一:修改服务器端配置,禁用Notification Message的默认音效

通过在Firebase消息中明确配置Android和iOS的静音参数,强制系统不播放默认音效。用户开启音效时,再恢复对应配置即可。

.NET服务器代码修改:

var messaging = FirebaseMessaging.DefaultInstance;
var message = new Message()
{
    Notification = new Notification
    {
        Title = notificationToBeSent.Title ?? string.Empty,
        Body = notificationToBeSent.Message ?? string.Empty
    },
    Data = notificationToBeSent.Data,
    Token = notificationToBeSent.RecipientId,
    // Android配置:禁用默认音效
    Android = new AndroidConfig
    {
        Notification = new AndroidNotification
        {
            UseDefaultSound = false,
            Sound = "" // 空字符串进一步确保静音
        }
    },
    // iOS配置:禁用默认音效
    Apns = new ApnsConfig
    {
        Aps = new Aps
        {
            Sound = "" // 空字符串表示静音
        }
    }
};
var result = await messaging.SendAsync(message);

当用户需要开启音效时,将AndroidNotification.UseDefaultSound设为true,Apns.Aps.Sound设为"default"即可。


方案二:改用Data Message,完全由APP控制通知展示(更灵活)

Notification Message的控制权在系统,而Data Message由APP自行处理通知展示,可完全控制音效开关,适配用户本地设置。

1. .NET服务器代码修改(发送Data Message)

去掉Notification字段,将通知内容放入Data中,可额外携带音效开关状态(或由APP读取本地设置):

var messaging = FirebaseMessaging.DefaultInstance;
var message = new Message()
{
    Data = new Dictionary<string, string>
    {
        {"title", notificationToBeSent.Title ?? string.Empty},
        {"body", notificationToBeSent.Message ?? string.Empty},
        // 可选:从用户设置获取音效状态传递,或APP本地读取
        {"sound_enabled", userNotificationSettings.SoundEnabled.ToString()}
    },
    Token = notificationToBeSent.RecipientId
};
var result = await messaging.SendAsync(message);

2. React Native端处理(用本地通知库控制音效)

使用本地通知库(如react-native-push-notification),在后台和前台消息处理器中根据用户设置决定是否播放音效:

后台消息处理:

import messaging from '@react-native-firebase/messaging';
import PushNotification from 'react-native-push-notification';
import AsyncStorage from '@react-native-async-storage/async-storage';

messaging().setBackgroundMessageHandler(async remoteMessage => {
    // 读取本地用户设置(替代服务器传递的sound_enabled)
    const soundEnabled = await AsyncStorage.getItem('notification_sound_enabled') === 'true';
    
    PushNotification.localNotification({
        title: remoteMessage.data.title,
        message: remoteMessage.data.body,
        // Android配置
        playSound: soundEnabled,
        soundName: soundEnabled ? 'default' : undefined,
        // iOS配置
        ios: {
            sound: soundEnabled ? 'default' : null,
        },
    });
});

前台消息处理:

messaging().onMessage(async remoteMessage => {
    const soundEnabled = await AsyncStorage.getItem('notification_sound_enabled') === 'true';
    
    PushNotification.localNotification({
        title: remoteMessage.data.title,
        message: remoteMessage.data.body,
        playSound: soundEnabled,
        soundName: soundEnabled ? 'default' : undefined,
        ios: {
            sound: soundEnabled ? 'default' : null,
        },
    });
});

关键说明

  • Android 8.0+需注意通知渠道:若使用Notification Message方式,需确保通知渠道的音效设置可被消息级配置覆盖;若使用Data Message方式,本地通知库会自动处理渠道配置。
  • iOS需开启Background Modes中的Remote notifications权限,否则后台无法处理Data Message。

内容的提问来源于stack exchange,提问作者Alex Karamanis

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 08:00:33