How to Implement Custom Notification Sounds in React Native

Setting up custom notification sounds can make your React Native app more engaging and give users a unique, branded experience. In this guide, we’ll walk through how to set up custom notification sounds for both iOS and Android in a React Native application.
Why Custom Notification Sounds Matter
Custom notification sounds can improve the user experience by making your app’s alerts stand out and enhancing brand recognition. It’s especially useful for apps that need users’ immediate attention, such as messaging apps, financial notifications, or reminders.
Prerequisites
To implement custom notification sounds, make sure you:
Have a React Native project set up.
Are using a library to handle push notifications, like
@react-native-firebase/messagingorreact-native-push-notification.Have custom audio files in
.wavformat for iOS or.mp3format for Android.
Let’s dive into the steps for setting up custom notification sounds on both platforms.
Step 1: Adding Custom Sound Files to Your Project
Prepare Your Sound Files:
Ensure the sound file is in the right format. Use
.wavfor iOS and.mp3for Android.Keep the file name simple, without spaces or special characters.
Add Sound Files to Android:
- Place your
.mp3sound file in theandroid/app/src/main/res/raw/directory. If therawfolder doesn’t exist, create it.
- Place your
Add Sound Files to iOS:
In Xcode, add the
.wavsound file to your project. Right-click on theios/{YourProject}/folder, select “Add Files to {YourProject},” and choose your sound file.Ensure the file is included in the Build Phases under “Copy Bundle Resources.”
Step 2: Configuring Custom Sounds in React Native Code
To trigger custom notification sounds, you’ll need to configure the notification payload differently depending on whether you’re sending notifications locally (within the app) or from a server.
For Local Notifications
If you’re triggering notifications locally, you can use react-native-push-notification to set custom sounds.
import PushNotification from "react-native-push-notification";
// Configure push notification settings
PushNotification.configure({
onNotification: function(notification) {
console.log("Notification received:", notification);
},
});
// Trigger a local notification with a custom sound
PushNotification.localNotification({
title: "Custom Sound Notification",
message: "This is a notification with a custom sound.",
soundName: "custom_sound.mp3", // Match this with the filename in Android's raw folder or iOS project
});
In this example:
soundName is set to
custom_sound.mp3(make sure to exclude the extension on iOS if needed).Customize
titleandmessageas needed.
For Push Notifications (Server-Side)
If your app receives notifications from a server, set up custom sounds in the payload sent by the server.
Firebase Example: For Firebase Cloud Messaging (FCM), include
soundin the notification payload.{ "to": "device_token", "notification": { "title": "Custom Notification", "body": "This has a custom sound!", "sound": "custom_sound" } }In this payload:
sound: Set this to the name of the sound file (exclude
.wavor.mp3extensions).This setup will trigger
custom_sound.wavon iOS orcustom_sound.mp3on Android.
APNs Example (for iOS): If sending notifications via Apple Push Notification Service (APNs), format the payload like this:
{ "aps": { "alert": { "title": "Custom Notification", "body": "This notification uses a custom sound." }, "sound": "custom_sound.wav" } }The
soundproperty should match the file name of your custom sound in the iOS project.
Step 3: Testing Custom Notification Sounds
For Local Notifications: Run the app and trigger a local notification using your code. Ensure your device’s sound is enabled.
For Push Notifications: Send a test notification from your backend or a tool like Firebase to check if the custom sound plays as expected.
Troubleshooting Common Issues
Sound Not Playing: Ensure the sound file is correctly added to the Android
rawfolder or iOS project. Double-check that the file name in your code matches the actual file name.Notification Not Triggering on iOS: iOS requires user permission to show notifications. Make sure your app requests notification permissions on startup.
File Format Issues: Verify the format is
.wavfor iOS and.mp3for Android. If the sound still doesn’t play, try converting the file using an audio tool.
Wrapping Up
Adding custom notification sounds in React Native is a great way to enhance user engagement and brand recognition. By following the steps above, you can set up unique notification sounds on both iOS and Android, ensuring that your app’s notifications stand out.
With custom notifications now part of your app, you’re well on your way to providing a richer, more personalized experience for your users.
React Native Stories