TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CloudMessaging Android notification sound

GorillaSpring opened this issue · comments

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

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"