React Native安卓应用推送通知自定义音效全场景生效问题咨询
React Native安卓应用推送通知自定义音效全场景生效问题咨询
嘿,我刚好折腾过这个需求,给你详细说下安卓端怎么实现全场景(前台、后台、应用被杀)播放自定义推送音效,亲测靠谱:
第一步:准备自定义音效文件
首先把你的自定义音效文件(比如custom_sound.mp3)放到安卓项目的android/app/src/main/res/raw目录下——如果没有raw文件夹就自己新建一个。这里要注意文件名必须是小写字母、数字、下划线的组合,不能有大写或者特殊字符,不然安卓会识别不到。
第二步:前台状态下的音效播放
当应用在前台时,推送事件会触发JS层的回调(比如用@react-native-firebase/messaging的话是onMessage,用react-native-push-notification是onNotification),这时候我们可以用react-native-sound库直接播放音效:
- 先安装依赖:
npm install react-native-sound --save - 在回调里写播放逻辑:
另外,如果前台也要弹出系统通知栏,记得在通知配置里指定音效,避免系统播放默认声音。import Sound from 'react-native-sound'; // 假设这是你的前台推送回调函数 const handleForegroundNotification = (notification) => { // 先设置音频类别,确保能正常播放 Sound.setCategory('Playback'); // 初始化音效文件 const customSound = new Sound('custom_sound.mp3', Sound.MAIN_BUNDLE, (error) => { if (error) { console.log('加载自定义音效失败:', error); return; } // 播放音效,播放完记得释放资源 customSound.play(() => customSound.release()); }); };
第三步:后台/应用被杀状态的音效处理
这两种情况JS层已经没法干预了,必须通过安卓原生代码配置通知的音效,不同推送库的处理方式略有不同:
如果你用的是Firebase Messaging
- 找到安卓项目里的
FirebaseMessagingService子类(如果没有就新建一个),在onMessageReceived方法中构建通知时添加音效配置:import android.media.RingtoneManager; import android.net.Uri; import androidx.core.app.NotificationCompat; import android.os.Build; import androidx.core.app.NotificationManagerCompat; public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String CHANNEL_ID = "custom_notification_channel"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); // 先创建通知渠道(Android O及以上必须) createNotificationChannel(); // 构建自定义音效的Uri Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_sound); // 构建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(remoteMessage.getNotification().getTitle()) .setContentText(remoteMessage.getNotification().getBody()) .setPriority(NotificationCompat.PRIORITY_HIGH) // 高优先级才能确保音效播放 .setSound(soundUri); // 设置自定义音效 // 显示通知 NotificationManagerCompat.from(this).notify((int) System.currentTimeMillis(), builder.build()); } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_sound); android.app.NotificationChannel channel = new android.app.NotificationChannel( CHANNEL_ID, "自定义通知渠道", android.app.NotificationManager.IMPORTANCE_HIGH ); // 给渠道设置音效 channel.setSound(soundUri, new android.media.AudioAttributes.Builder() .setUsage(android.media.AudioAttributes.USAGE_NOTIFICATION) .setContentType(android.media.AudioAttributes.CONTENT_TYPE_SONIFICATION) .build()); android.app.NotificationManager notificationManager = getSystemService(android.app.NotificationManager.class); notificationManager.createNotificationChannel(channel); } } }
如果你用的是react-native-push-notification
这个库更简单,直接在通知配置里指定soundName参数就行,不管是本地通知还是远程通知:
import PushNotification from 'react-native-push-notification'; // 初始化通知渠道(只需要做一次) PushNotification.createChannel( { channelId: "custom_channel", // 渠道ID channelName: "自定义通知渠道", channelDescription: "用于播放自定义音效的通知渠道", soundName: "custom_sound.mp3", // 指定raw目录下的音效文件名 importance: 4, // 对应安卓的IMPORTANCE_HIGH vibrate: true, }, (created) => console.log(`渠道创建状态: ${created}`) ); // 发送本地通知时指定音效 PushNotification.localNotification({ title: "测试通知", message: "这是带自定义音效的通知", channelId: "custom_channel", soundName: "custom_sound.mp3", });
远程通知的话,在推送 payload 里添加sound: "custom_sound.mp3"即可。
几个要踩的坑提醒
- 音效格式尽量用安卓原生支持的,比如MP3、WAV,文件大小别太大,不然加载慢甚至播放失败。
- Android 12及以上,通知必须设置高优先级(
PRIORITY_HIGH/IMPORTANCE_HIGH),不然系统可能会静音或者不播放音效。 - 应用被杀后,只有通过FCM这类系统级推送服务触发的通知才能生效,本地通知在应用被杀后是没法触发的。
内容来源于stack exchange
相关产品推荐
相关产品推荐

