Skip to content

Notification with a Sound File

Elvin Thudugala edited this page Jul 28, 2025 · 18 revisions

Custom Notification Sounds Guide

Overview

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.

Implementation

1. Basic Notification with Sound

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);

2. .NET MAUI Configuration

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);
	}
}

Platform-Specific Setup

Android Configuration

Sound File Requirements

  1. File Naming

    • Use lowercase letters only
    • No spaces or special characters
    • Example: notification_sound.wav
  2. Technical Requirements

    • Audio bitrate: 128kbps (320kbps is not supported)
    • Duration: Maximum 30 seconds
    • Build Action: Must be set to AndroidResource
  3. Supported Formats

File Location

For .NET MAUI
  1. Create the raw folder:
    • Path: Platforms/Android/Resources/raw/
    • Example: Platforms/Android/Resources/raw/notification_sound.wav
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.

For Xamarin.Forms
  • Place sound files in Resources/raw/ folder
  • Create the folder if it doesn't exist

Channel Configuration

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
});

iOS Configuration

Sound File Requirements

  1. Supported Formats

  2. File Location

    • Add sound files to the Resources folder

.NET MAUI Setup

  1. File Structure
YourApp/
├── Platforms/
│   └── iOS/
│       └── Resources/
│           ├── notification_sound.wav
│           └── alert_sound.aiff
  1. 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>

Using Custom Sounds

// 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
    }
};

Best Practices

  1. Cross-Platform Compatibility

    • Use WAV format for best compatibility
    • Keep file sizes small
    • Test on both platforms
  2. Sound Selection

    • Use appropriate sounds for notification type
    • Keep sounds brief and professional
    • Consider accessibility implications
  3. Performance

    • Optimize sound file sizes
    • Cache sound resources appropriately
    • Test memory usage with multiple notifications
  4. User Experience

    • Respect system sound settings
    • Provide silent options
    • Consider time of day for notifications

Troubleshooting

  1. Sound Not Playing

    • Verify file exists in correct location
    • Check file name matches exactly
    • Confirm build action is correct
    • Verify channel settings (Android)
  2. Build Issues

    • Check file is included in build
    • Verify bundle resource configuration
    • Clean and rebuild solution
  3. Runtime Issues

    • Check device sound settings
    • Verify app permissions
    • Test with different sound files
Clone this wiki locally