TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
220 stars 49 forks source link

CloudMessaging Android notification sound #334

Open GorillaSpring opened 3 months ago

GorillaSpring commented 3 months ago

I am porting over a Xamarin project (native iOS and Android).

I cannot get the Android to play the custom sound file. iOS is working fine.

I have an mp3 file (notify2.mp3) in:

Platforms->Android->Resources=>raw folder.

Tried build actions:

` var channelId = $"{PackageName}.general"; var notificationManager = (NotificationManager)GetSystemService(NotificationService); var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default);

var alarmAttributes = new AudioAttributes.Builder() .SetContentType(AudioContentType.Music) .SetUsage(AudioUsageKind.Notification).Build();

var soundNameWithoutExtension = "notify2"; var soundFilePath = $"{ContentResolver.SchemeAndroidResource}://{Android.App.Application.Context.PackageName}/raw/{soundNameWithoutExtension}"; var soundFileUri = Android.Net.Uri.Parse(soundFilePath);

channel.SetSound(soundFileUri, alarmAttributes);

notificationManager.CreateNotificationChannel(channel); FirebaseCloudMessagingImplementation.ChannelId = channelId; `

GorillaSpring commented 2 months ago

Still waiting for a solution, however here is an update.

The notification sound play correct iff (if and only if) the app is in the foreground when the notification arrives.

This seems like a bug.

The mp3 file is in: Platforms->Android->Resources=>raw folder. The Build Action = "AndroidResourse" Copy To output .. = Copy Always Custom Tool= "MSBuild:UpdateGeneratedFiles"