When using a custom notification sound for Android in a .NET MAUI project, the sound file must be treated as a native Android resource, not a general MAUI asset. The default sound will play if the file is incorrectly placed or its build action is wrong.
Follow these steps to correctly configure your custom sound:
1. Place the Sound File
Place your custom sound file (e.g., .mp3, .wav) inside the Android-specific resource directory. If this folder doesn't exist, you will need to create it.
- Correct Path:
YourProject/Platforms/Android/Resources/raw/ - Important: The folder name
rawmust be all lowercase.
2. Set the Build Properties
In your IDE (like Visual Studio), locate the sound file in the Solution Explorer and set its properties:
- Build Action: Set to
AndroidResource. - Copy to Output Directory: Set to
Copy if newerorCopy always. This ensures the file is included during the build process.
3. Specify the Sound in Your API Request
For Android 8.0 (API level 26) and higher, custom sounds are tied to notification channels. When you send a push notification using the createMessage API, you must specify the channel and the sound file.
Include the following parameters in your JSON payload:
{
"notifications": [
{
"android_sound": "your_sound_file_name",
"android_root_params": {
"pw_channel": "your_custom_channel_name"
}
}
]
}
- Replace
your_sound_file_namewith the name of your sound file without the file extension (e.g., if your file isalert.mp3, use"alert"). - Replace
your_custom_channel_namewith a unique name for your notification channel.
The first time a notification is sent with these parameters, a new channel will be created with your custom sound. For subsequent notifications, you only need to specify the same channel name.
Best Practices for Sound Files
- Filename: Use lowercase letters, numbers, and underscores only. Avoid spaces or special characters.
- Format: Supported formats include MP3, WAV, and OGG.
- Bitrate: For MP3 files, a maximum bitrate of 128 kbps is recommended.
Comments
0 comments
Please sign in to leave a comment.