首页 > 解决方案 > 自定义通知声音不会在 android 中播放

问题描述

我创建了一个依赖服务,但在 android 中自定义通知声音不播放。

什么尝试:

我的音频文件位置是资源/原始文件夹

以前有人遇到过这个问题吗?

这是我的代码:

public class AndroidNotificationManager : INotificationManager
    {
        const string channelId = "default";
        const string channelName = "Default";
        const string channelDescription = "The default channel for notifications.";
        const int pendingIntentId = 0;

        public const string TitleKey = "title";
        public const string MessageKey = "message";

        bool channelInitialized = false;
        NotificationManager manager;
        private string soundFileName;

        public event EventHandler NotificationReceived;

        public void Initialize()
        {
            CreateNotificationChannel();
        }

        public int ScheduleNotification(string title, string message, int messageId, string soundFileName = "")
        {
            if (!channelInitialized)
            {
                CreateNotificationChannel();
            }

            this.soundFileName = soundFileName;
            Intent intent = new Intent(AndroidApp.Context, typeof(MainActivity));
            intent.PutExtra(TitleKey, title);
            intent.PutExtra(MessageKey, message);

            PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntentId, intent, PendingIntentFlags.OneShot);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId)
                .SetContentIntent(pendingIntent)
                .SetContentTitle(title)
                .SetContentText(message)
                .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.ic_launcher))
                .SetSmallIcon(Resource.Drawable.ic_launcher)
                .SetVibrate(null);

            if (!string.IsNullOrEmpty(soundFileName))
            {
                builder.SetDefaults((int)NotificationDefaults.Lights);
                var uri = Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{AndroidApp.Context.PackageName}/raw/alarm.wav");
                builder.SetSound(uri);
            }
            else
            {
                builder.SetDefaults((int) NotificationDefaults.Lights);
                builder.SetSound(null);
            }

            Notification notification = builder.Build();
            manager.Notify(messageId, notification);

            return messageId;
        }

        public void ReceiveNotification(string title, string message)
        {
            var args = new NotificationEventArgs()
            {
                Title = title,
                Message = message,
            };
            NotificationReceived?.Invoke(null, args);
        }

        void CreateNotificationChannel()
        {
            manager = (NotificationManager)AndroidApp.Context.GetSystemService(AndroidApp.NotificationService);

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                var channelNameJava = new Java.Lang.String(channelName);
                var channel = new NotificationChannel(channelId, channelNameJava, NotificationImportance.Default)
                {
                    Description = channelDescription
                };
                channel.SetVibrationPattern(new long[] { 0 });
                channel.EnableVibration(true);
                channel.EnableLights(true);

                // Creating an Audio Attribute
                var alarmAttributes = new AudioAttributes.Builder()
                    .SetContentType(AudioContentType.Sonification)
                    .SetUsage(AudioUsageKind.Notification).Build();

                if (!string.IsNullOrEmpty(soundFileName))
                {
                    var uri = Uri.Parse($"{ContentResolver.SchemeAndroidResource}://{AndroidApp.Context.PackageName}/raw/alarm.wav");
                    channel.SetSound(uri, alarmAttributes);
                }
                else
                {
                    channel.SetSound(null, null);
                }

                manager.CreateNotificationChannel(channel);
            }

            channelInitialized = true;
        }
    }

标签: androidxamarinxamarin.android

解决方案


您创建的 URI 是错误的,这就是它没有被拾取的原因。它需要看起来像:android.resource://<packagename>/<resourceId>

所以在你的情况下,这样的事情应该有效:

var uri = Uri.Parse($"android.resource://{AndroidApp.Context.PackageName}/{Resources.Raw.alarm}");

鉴于文件警报在Resources/raw/


推荐阅读