I need to play a custom sound ./android/app/src/main/res/raw/s4.mp3
when a notification is received to a react native application on android device from the server.
I am using following command to run my react-native application on android device, that is connect my pc with a USB cable.
npx react-native start --reset-cache
It works all fine except playing required sound on receiving a message from server.
In ./android/app/src/main/java/com/channel92news/push/MainActivity.java
I have following code to set the sound for channel down_alerts
NotificationChannel ntc = new NotificationChannel("down_alerts", "main", NotificationManager.IMPORTANCE_HIGH);
AudioAttributes att = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).
setContentType(AudioAttributes.CONTENT_TYPE_SPEECH).build();
ntc.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/raw/s4"), att);
At server (using python exponent_server_sdk) I have simple line to send alerts which works
from exponent_server_sdk import PushMessage, PushClient
PushClient().publish_multiple([push_message1, push_message2])
The detail of push_message1 & push_message2 is
push_message1 = PushMessage(
title="Server side notification", body="Message is pushed from server using PushClient().publish_multiple",
channel_id="down_alters",
to=token_value1
)
push_message2 = PushMessage(
title="Server side notification", body="Message is pushed from server using PushClient().publish_multiple",
channel_id="down_alters",
to=token_value2
)
My main code in react-native App.js is
//get token
let expo_token = (await Notifications.getExpoPushTokenAsync()).data;
if(expo_token){
if (Platform.OS === 'android') {
console.log('Setting notification channel');
//set channel
Notifications.setNotificationChannelAsync('down_alerts', {
name: 'main',
sound: 's4.mp3',
importance: Notifications.AndroidImportance.MAX,
});
}
}
//register receive notification listener
Notifications.addNotificationReceivedListener(notification => {
console.log(notification);
});
This console.log results as following when the notification pushed by server and received at client.
{
"date": 1688637867600,
"request": {
"content": {
"autoDismiss": true,
"badge": null,
"body": "Message is pushed from server using PushClient().publish_multiple",
"data": [Object],
"sound": "default",
"sticky": false,
"subtitle": null,
"title": "Server side notification"
},
"identifier": "0:1688637867628864%40fad706f9fd7ecd",
"trigger": {
"channelId": "down_alters",
"remoteMessage": [Object],
"type": "push"
}
}
}
Unfortunately it does not play s4.mp3, but only default sound
Then I tried to schedule/create a local notification for test as
Notifications.scheduleNotificationAsync({
content: {
title: "Client side test notification",
body: 'This notification is sent using Notifications.scheduleNotificationAsync',
},
trigger: {
seconds: 1,
channelId: 'down_alerts',
},
});
This client side test notification plays the required sound when received, console.log(notification) produces following result for this one.
{
"date": 1688635365006,
"request": {
"content": {
"autoDismiss": true,
"badge": null,
"body": "This notification is sent using Notifications.scheduleNotificationAsync",
"data": null,
"sound": "default",
"sticky": false,
"subtitle": null,
"title": "Client side test notification"
},
"identifier": "c1e8cfab-d0d9-44cb-9f4d-54a78d55c8e9",
"trigger": {
"channelId": "down_alerts",
"repeats": false,
"seconds": 1,
"type": "timeInterval"
}
}
}
What different I can do to play my custom sounds on receiving notification from server?