Skip to content

Notification with a Sound File

Elvin (Tharindu) Thudugala edited this page Apr 13, 2024 · 16 revisions

Show local notification

var notification = new NotificationRequest
{
    NotificationId = 100,
    Title = "Test",
    Description = "Test Description",
    //If not specified, the default sound will be played. In Android, this must be set at the channel level for new versions of Android. 
    Sound = DeviceInfo.Platform == DevicePlatform.Android ?  "<file name without extension>" : "file name with extension", 
};
LocalNotificationCenter.Current.Show(notification);

.Net MAUI

public static class MauiProgram
{
	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseMauiApp<App>()
			....
			.UseLocalNotification(config =>
			{
                                config.AddAndroid(android =>
				{
					android.AddChannel(new NotificationChannelRequest
					{
						Sound = "good_things_happen"
					});
				});
			});
               return builder.Build();
	}
}

Xamarin.Forms

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

Android

  1. Have to respect Android naming convention (everything in lowercase letters)
  2. Must be 128kps (320kps does not work)
  3. BuildAction must be set to AndroidResource
  4. Supported formats are mainly: mp3, m4a, wav, ogg based on doc
  5. Sound has to be smaller than 30s
  6. The Best sound format is WAV; it is supported by both platforms

.Net MAUI

  1. Add the sound file under Platforms\Android\Resources\raw (create the raw folder if it does not exist)
  2. The sound has to be in platform specific raw folder. If it is put into global Resources/Raw, it would be stored not as a resource but as an asset. And it is not possible to get the path to asses, only to the resource.

image

Xamarin.Forms

Add the sound file under Resources\raw (create the raw folder if does not exist)

image

When Android >= 26, Sound must be set in Channel level. (After Version 4.0.0).

Create different Notification Channels with different names if you want to play different sound files.

iOS

Add the sound file under Resources.

  1. Supported formats are: aiff, wav, caf based on doc
  2. The Best sound format is WAV, it is supported by both platforms

.Net MAUI

image

Also, make sure to add BundleResource link in csproj

  <ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">
    <BundleResource Include="Platforms\iOS\Resources\good_things_happen.aiff" Link="Resources\good_things_happen.aiff" />
  </ItemGroup>

Xamarin.Forms

image