react native push notification sound

Viewed 22307

I am trying to add sound to my local push notification. I am using RN 0.45.1 and react-native-push-notifications 3.0.0

I manage to schedule notification in iOS and Android with default sound. I did not manage to add custom sound.

I have sounds file of type mp3. I tried the following:

  1. place the file in my project folder: '/src/assests/sounds/sound.mps' (a folder inside my project)

and than:

import notificationSound from '../src/assests/sounds/sound.mps';

PushNotification.localNotificationSchedule({
            message: 'Test message',
            date: new Date(Date.now() + (60 * 1000)),
            repeatType: 'time',
            repeatTime: 60 * 1000,
            sound: notificationSound,
        });
  1. another attempt was: putting the sound file under android folder: ..\android\app\src\main\res\raw\sound.mp3

and the notification was:

   PushNotification.localNotificationSchedule({
            message: 'Test message',
            date: new Date(Date.now() + (60 * 1000)),
            repeatType: 'time',
            repeatTime: 60 * 1000,
            sound: sound.mp3,
        });
3 Answers

Check the scheduleNotif(soundName) function from the following example from the react-native-push-notification docs. The previous example worked for me on my Android device. Just add the name of the sound file without writing its format "success", not "success.mp3" .. The sound file should be located under the raw folder [project_root]/android/app/src/main/res/raw

My modified method for my app needs:

var lastId = 0;
function scheduleNotif(soundName, date, title, message) {
  lastId++;
  PushNotification.localNotificationSchedule({
    date: new Date(date),
    title: title,
    message: message,
    color: 'blue', 
    invokeApp: false, 

    when: null,
    usesChronometer: false, 
    timeoutAfter: null, 

    
    category: '', // (optional) default: empty string

    id: lastId, 
    userInfo: { sceen: 'home' }, 
    playSound: !!soundName,                        //<-----
    soundName: soundName ? soundName : 'default',  //<-----
    number: 10,
  });
}

please use this payload, before that, need to create a channel. It works for both app is killed state and closed state.

{ "notification":{ "title": "test notification", "body": "test notification", "android_channel_id": "channelID" }, "data":{ "property1":"value1", "property2":42 } }

Related