Faye
Faye

Reputation: 95

Custom Notification Sound Not Playing on iOS for react-native-push-notification

I am using the react-native-push-notification npm package. I am setting the path to a custom sound while creating the notification channel as below:

 PushNotification.createChannel(
    {
      channelId: 1, // (required)
      channelName: 'My Channel', // (required)
      channelDescription: 'A channel to categorise your notifications', // (optional) default: undefined.
      soundName: 'tone1', 
      playSound: true, 
      importance: 4,
      vibrate: true, 
    },
    (created) => console.log(`createChannel returned '${created}'`),
  );

For ios, I am adding the sound to the project root folder in XCode as follows:

sound file in XCode

sound file in copy bundle resources

But this does not work and the default sound is still played. On Android, the custom sound is working correctly. I even imported the sound file and played it manually to ensure that it is working properly:

import SoundPlayer from 'react-native-sound-player';

SoundPlayer.playSoundFile('tone1', 'mp3');

I even used the extension .mp3 in the soundName variable but it still does not work.

Library version: 7.3.2

Upvotes: 0

Views: 3164

Answers (1)

Faye
Faye

Reputation: 95

I was able to get it to work. Turns out I was not adding the sound file in the correct folder. We have to add it to the ios/ directory (same level as the Pods folder).

Additionally, I also had to pass the soundName variable when we are scheduling the local notification. Previously, I was only passing the variable when creating the channel.

PushNotification.localNotificationSchedule({
    title: 'Hello',
    message: 'Check Notification' + new Date(Date.now() + 1 * 1000),
    date: new Date(Date.now() + 1 * 1000), // in 1 secs
    allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
    channelId: 'checkNotification',
    soundName: 'tone5.mp3', // (optional) Sound to play
  });

Upvotes: 1

Related Questions