I'm working on push notifications using Expo version 41 - bare workflow (in the documentation is written that custom sounds are working only on this workflow). I want to use custom sound. Right now it works for local notification:
async function schedulePushNotification() {
await Notifications.scheduleNotificationAsync({
content: {
title: "Test notification",
body: 'Test notification body',
sound: 'sound.wav', // Android 8.0-
},
trigger: {
seconds: 2,
channelId: 'sound', // Android 8.0+
},
});
}
As you can see I have created new channel with id: sound:
Notifications.setNotificationChannelAsync('sound', {
name: 'sound',
importance: Notifications.AndroidImportance.HIGH,
sound: 'sound.wav', // <- Android 8.0+
});
Almost identical like in the documentation:
https://docs.expo.io/versions/latest/sdk/notifications/#setting-custom-notification-sounds
I use setNotificationChannelAsync in useEffect hook after getExpoPushTokenAsync (actually copy paste from the documentation)
useEffect(() => {
registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
});
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log(response);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
async function registerForPushNotificationsAsync() {
let token;
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
Notifications.setNotificationChannelAsync('sound', {
name: 'sound',
importance: Notifications.AndroidImportance.HIGH,
sound: 'sound.wav', // Android 8.0+
});
return token;
}
The schedulePushNotification method is run on button press. After 2 seconds I get a notification with my sound. Cool. When I'm trying to do the same with the Push notifications tool
I received a notification with a default sound. It looks like the channel doesn't work.
I tried to send it using expo-server-sdk-java also.
ExpoPushMessage expoPushMessage = new ExpoPushMessage();
expoPushMessage.setTo(List.of(token));
expoPushMessage.setTitle("Test Notification API JAVA");
expoPushMessage.setBody("Body");
expoPushMessage.setChannelId("sound");
//expoPushMessage.setSound(new ExpoMessageSound(true, "default", 0L));
The setSound method throwing an exception when name is different than default.
I testing it on Android with version 6.1 and 5.0.
Do any of you have an idea how to do it?