Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudMessaging Android notification sound #334

Open
GorillaSpring opened this issue Aug 17, 2024 · 1 comment
Open

CloudMessaging Android notification sound #334

GorillaSpring opened this issue Aug 17, 2024 · 1 comment

Comments

@GorillaSpring
Copy link

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:

  • AndroidAsset
  • Resource
  • AndroidResource

`
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
Copy link
Author

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant