How to Implement Custom Notification Sounds in React Native

4 min read
Cover Image for 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/messaging or react-native-push-notification.

  • Have custom audio files in .wav format for iOS or .mp3 format 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

  1. Prepare Your Sound Files:

    • Ensure the sound file is in the right format. Use .wav for iOS and .mp3 for Android.

    • Keep the file name simple, without spaces or special characters.

  2. Add Sound Files to Android:

    • Place your .mp3 sound file in the android/app/src/main/res/raw/ directory. If the raw folder doesn’t exist, create it.
  3. Add Sound Files to iOS:

    • In Xcode, add the .wav sound file to your project. Right-click on the ios/{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 title and message as 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.

  1. Firebase Example: For Firebase Cloud Messaging (FCM), include sound in 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 .wav or .mp3 extensions).

    • This setup will trigger custom_sound.wav on iOS or custom_sound.mp3 on Android.

  2. 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 sound property 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

  1. Sound Not Playing: Ensure the sound file is correctly added to the Android raw folder or iOS project. Double-check that the file name in your code matches the actual file name.

  2. Notification Not Triggering on iOS: iOS requires user permission to show notifications. Make sure your app requests notification permissions on startup.

  3. File Format Issues: Verify the format is .wav for iOS and .mp3 for 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.