How to use custom sounds for notifications with default Expo's push notification service on(ios/android) and in (foreground/background)

Minha ideia, é poder usar sons customizados usando a propria ferramenta de disparo de de notificações http da expo, essa que não suporta passar sons customizados pela api, oque eu fiz para o android eu utilizei os canais para cada totar os sons customizados mas para ios o problema é maior pois não tem canais para poder configurar os sons, então quando o aplicativo esta em primeiro plano e em um sistema ios eu uso os handlers para quando receber uma notificação disparar apenas o alerta e o distintivo mas não o som pois seria o som padrão então mando o handler disparar uma notificação com o titulo de ghost e com o som customizado que eu quero, assim resilvi o probleme de sons customizados com o aplicatovo em primeiro plano, mas tinha que achar uma forma de fazer o mesmo para o segundo plano, ai que começou os problemas fiquei 15 horas seguidas com o mesmo erro o gatilho para notificações em segundo plano para ios não estava funcinando, até que hoje achei no comentário de um comit no git hub do expo feito a 2 meses atrás que poderia passar o atributo “content-available”: 1 dessa forma “_contentAvailable”: “true” no json da api padrão do Expo’s push notification service, dessa forma tudo voltou a funcionar e no gatilho de notificações no segundo plano eu vejo se o sistema é ios e então disparao uma noticação só com o som customizado que eu quero. assim consegui sons customizados nos 2 sistemas usando o Expo’s push notification service :grin:

My idea is to be able to use custom sounds using expo’s own http notification trigger tool, which doesn’t support passing custom sounds through the api, what I did for android I used the channels for each totar custom sounds but for ios the problem is bigger because it doesn’t have channels to configure the sounds, so when the app is in the foreground and in an ios system I use the handlers so when I receive a notification, it only triggers the alert and the badge but not the sound because it would be the default sound so I have the handler trigger a notification with the title of ghost and the custom sound I want, so I solved the problem of custom sounds with the app in the foreground, but I had to find a way to do the same for the second plan, that’s when the problems started I was 15 hours straight with the same error the trigger for notifications in the background for ios was not working, until today I found it in a comit comment on git hub of expo made 2 months ago that could pass the attribute “content-available”: 1 like this “_contentAvailable”: “true” in the json of the default api of Expo’s push notification service, that way everything works again and in the trigger of notifications in the background i see if the system is ios and then i trigger a notification with just the custom sound i want. so I got custom sounds on both systems using Expo’s push notification service :grin:

Foreground
Notifications.setNotificationHandler({
handleNotification: async (notification) => {
if(Platform.OS === “ios”){
if (notification.request.content.title === “ghost”){
return({
shouldShowAlert: false,
shouldPlaySound: true,
shouldSetBadge: false,
});
}else{
const resp = Notifications.scheduleNotificationAsync({
content: {
title: “ghost”,
sound: ‘sond.wav’,
},
trigger: { seconds: 1 },
});
return({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: true,
});
}
}else{
return({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
});
}

}

});

Background

const bc = ‘NOTIFICATION’;

TaskManager.defineTask(bc, ({ data, error,executionInfo}) => {
if(data){
console.log(data);
}
if (Platform.OS === “ios”){
Notifications.scheduleNotificationAsync({
content: {
sound: ‘sond.wav’,
},
trigger: { seconds: 1 },
});
}
const resposta = RegisterBackgroundTask();
console.log(“aaa”);
});
async function RegisterBackgroundTask() {
try {
const resp = await Notifications.registerTaskAsync(bc);
console.log(resp);
}catch (e) {
console.log(e);
}
}
const resposta = RegisterBackgroundTask();
LogBox.ignoreLogs([
‘Non-serializable values were found in the navigation state’,
]);

1 Like

Hi @iann_ortnau, thank you for sharing this workaround!

May I know the file format of your custom notification sound?
Is there any specific requirement and format for the sound file that you included?
I was trying to trigger my custom notification sound but have failed to play it so far.

i use “wav” format, and no forgot to add the sound in expo notifications configs in app.json like that image

1 Like

Thanks a lot @iann_ortnau! It is working on my side now.

1 Like