thudugala / Plugin.LocalNotification

The local notification plugin provides a way to show local notifications from .Net MAUI and Xamarin Forms apps .
MIT License
425 stars 68 forks source link

Android : Issue having multiple sounds #448

Open Bebz0r opened 1 year ago

Bebz0r commented 1 year ago

Hi there,

First, thanks a lot about your fantastic work, @thudugala, the functionnality and usage is really simple and straightforward.

However, I can't make multiple sounds to work on Android. One works fine, but if I want to add more, it does not work : only the first one I declare in .AddAndroid is played :

My files (legit_01.mp3 and legit_02.mp3) are located in Platforms/Android/Resources/raw

I did some tests (uninstalling / reinstalling the app before each test).

Here is how I call my notification. I want to be able to change the sound just by changing "legit_01" to "legit_02" :

// Create the request itself
var request = new NotificationRequest
{
    NotificationId = Convert.ToInt32(NotificationId),
    Title = $"{theNotif.Description} coming at {theNotif.TimeEvent:HH:mm}",
    Subtitle = "", // Appear on the right side of the title (optionnal)
    Description = theNotif.Category,
    // CategoryType, defined in MauiProgram.cs' config is the list of buttons available for actions
    CategoryType = NotificationCategoryType.Status,
    Sound = DeviceInfo.Platform == DevicePlatform.Android ? "legit_01" : "legit_01.mp3", // or legit_02
    Schedule = new NotificationRequestSchedule
    {
        // Set it based on the week day
        NotifyTime = TargetDate,
        RepeatType = NotificationRepeat.TimeInterval,
        NotifyRepeatInterval = TimeSpan.FromDays(7)
    }
};
LocalNotificationCenter.Current.Show(request);

And how I create my two channels :

.UseLocalNotification(config =>
{
    config.AddCategory(new NotificationCategory(NotificationCategoryType.Status)
    {
        ActionList = new HashSet<NotificationAction>(new List<NotificationAction>()
                {
                    new NotificationAction(1337)
                    {
                        Title = "MUTE",
                        Android = { LaunchAppWhenTapped = true }
                    }
                })
    })
    .AddAndroid(android =>
    {
        android.AddChannel(new NotificationChannelRequest { Sound = "legit_01" });
        android.AddChannel(new NotificationChannelRequest { Sound = "legit_02" });
    });
})

After multiple tests, it's always the first one defined in .AddAndroid that is played, whatever the value of the "Sound" parameter I give to the NotificationRequest. If I switch the AddChannel (e.g. : legit_02 first), it will play legit_02 first, no matter which legit_XX I put in the "Sound" parameter. If I give a random "Sound" parameter though, no sound is played - as if it didn't find the file (which is correct and expected).

Based on the docs, I tried to add Id and Names to the Channels, like so :

.UseLocalNotification(config =>
{
    config.AddCategory(new NotificationCategory(NotificationCategoryType.Status)
    {
        ActionList = new HashSet<NotificationAction>(new List<NotificationAction>()
                {
                    new NotificationAction(1337)
                    {
                        Title = "MUTE",
                        Android = { LaunchAppWhenTapped = true }
                    }
                })
    })
    .AddAndroid(android =>
    {
        android.AddChannel(new NotificationChannelRequest { Id=$"legit_01", Name="legit_01", Description="legit_01", Sound = "legit_01" });
        android.AddChannel(new NotificationChannelRequest { Id=$"legit_02", Name="legit_02", Description="legit_02", Sound = "legit_02" });
    });
})

But this behaves even worse : the default sound of my device (which is already a custom one) is played.

thudugala commented 1 year ago

@Bebz0r Can you please attach a sample project?

Bebz0r commented 1 year ago

Sure, let me create one from scratch, I'll keep you posted

Bebz0r commented 1 year ago

Here you go, I created a sample project reproducing the issue : https://github.com/Bebz0r/TestSoundNotification

Bebz0r commented 1 year ago

By the way, from my tests, it seems that (at least on Android), the mp3 files :

thudugala commented 1 year ago

@Bebz0r Is your issue fixed after fixing bit rate and name of the file ?

Documentation updated

Bebz0r commented 1 year ago

Hi @thudugala , No, the issue still happens, my remark was just a random finding :) Thanks for updating the docs.

thudugala commented 1 year ago

@Bebz0r Have you specified the ChannelId in NotificationRequest?

https://github.com/thudugala/Plugin.LocalNotification/wiki/Notification-with-a-Sound-File

image

pelka-mariusz commented 11 months ago

Hi, I have the same problem with multiple audio files. Only the first file works.

thudugala commented 11 months ago

@pelka-mariusz can you please attach a sample project?

pelka-mariusz commented 11 months ago

Hi, MauiApp1.zip

An example project is attached. Not any sound is played. Tested on Pixel 5 emulator - API 34 (Android 14.0 - API 34).

pelka-mariusz commented 11 months ago

Hi,

did you manage to solve the problem?