-
Notifications
You must be signed in to change notification settings - Fork 72
Notification with a Sound File
Elvin Thudugala edited this page Jul 28, 2025
·
18 revisions
Add custom sounds to your notifications to make them more distinctive and engaging. This guide covers how to implement custom notification sounds for both Android and iOS platforms.
var notification = new NotificationRequest
{
NotificationId = 100,
Title = "Notification with Sound",
Description = "This notification will play a custom sound",
Sound = DeviceInfo.Platform == DevicePlatform.Android
? "notification_sound" // Android: filename without extension
: "notification_sound.wav" // iOS: filename with extension
};
await LocalNotificationCenter.Current.Show(notification);
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseLocalNotification(config =>
{
config.AddAndroid(android =>
{
// Channel with custom sound
android.AddChannel(new NotificationChannelRequest
{
Id = "notifications_with_sound",
Name = "Notifications with Sound",
Description = "Notifications that play custom sounds",
Sound = "custom_notification_sound",
ImportanceLevel = NotificationImportance.High
});
// Channel without sound
android.AddChannel(new NotificationChannelRequest
{
Id = "silent_notifications",
Name = "Silent Notifications",
Description = "Notifications without sound",
EnableSound = false
});
});
});
return builder.Build();
}
}
#### Xamarin.Forms
```csharp
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
.....
// Must create a Notification Channel when API >= 26
// you can create multiple Notification Channels with different names.
LocalNotificationCenter.CreateNotificationChannel(
new Plugin.LocalNotification.Platform.Droid.NotificationChannelRequest
{
Sound = Resource.Raw.<file name without extension>.ToString()
});
.....
LoadApplication(new App());
.....
LocalNotificationCenter.NotifyNotificationTapped(Intent);
}
protected override void OnNewIntent(Intent intent)
{
LocalNotificationCenter.NotifyNotificationTapped(intent);
base.OnNewIntent(intent);
}
}
-
File Naming
- Use lowercase letters only
- No spaces or special characters
- Example:
notification_sound.wav
-
Technical Requirements
- Audio bitrate: 128kbps (320kbps is not supported)
- Duration: Maximum 30 seconds
- Build Action: Must be set to
AndroidResource
-
Supported Formats
- WAV (recommended for cross-platform compatibility)
- MP3
- OGG
- M4A See Android documentation
- Create the
raw
folder:- Path:
Platforms/Android/Resources/raw/
- Example:
Platforms/Android/Resources/raw/notification_sound.wav
- Path:
YourApp/
├── Platforms/
│ └── Android/
│ └── Resources/
│ └── raw/
│ ├── notification_sound.wav
│ └── alert_sound.wav
⚠️ Important: Do not place sound files in the global Resources/Raw folder as they will be stored as assets, making them inaccessible to the notification system.
- Place sound files in
Resources/raw/
folder - Create the folder if it doesn't exist
For Android 8.0 (API 26) and above:
- Sounds must be configured at the channel level
- Create separate channels for different sound configurations
android.AddChannel(new NotificationChannelRequest
{
Id = "alerts_channel",
Name = "Alerts",
Description = "Important notifications with custom sound",
Sound = "notification_sound", // File name without extension
Importance = NotificationImportance.High,
EnableVibration = true
});
-
Supported Formats
- WAV (recommended for cross-platform compatibility)
- AIFF
- CAF See Apple documentation
-
File Location
- Add sound files to the Resources folder
- File Structure
YourApp/
├── Platforms/
│ └── iOS/
│ └── Resources/
│ ├── notification_sound.wav
│ └── alert_sound.aiff
-
Project Configuration
Add to your
.csproj
file:
<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">
<BundleResource Include="Platforms\iOS\Resources\notification_sound.wav"
Link="Resources\notification_sound.wav" />
</ItemGroup>
// iOS-specific notification with custom sound
var notification = new NotificationRequest
{
NotificationId = 100,
Title = "Important Alert",
Description = "This will play a custom sound",
Sound = "notification_sound.wav", // Include file extension for iOS
iOS =
{
// Configure how the notification appears when the app is in foreground
HideForegroundAlert = false, // Show alert when app is in foreground
PlayForegroundSound = true // Play sound when app is in foreground
}
};
-
Cross-Platform Compatibility
- Use WAV format for best compatibility
- Keep file sizes small
- Test on both platforms
-
Sound Selection
- Use appropriate sounds for notification type
- Keep sounds brief and professional
- Consider accessibility implications
-
Performance
- Optimize sound file sizes
- Cache sound resources appropriately
- Test memory usage with multiple notifications
-
User Experience
- Respect system sound settings
- Provide silent options
- Consider time of day for notifications
-
Sound Not Playing
- Verify file exists in correct location
- Check file name matches exactly
- Confirm build action is correct
- Verify channel settings (Android)
-
Build Issues
- Check file is included in build
- Verify bundle resource configuration
- Clean and rebuild solution
-
Runtime Issues
- Check device sound settings
- Verify app permissions
- Test with different sound files