首页 > 解决方案 > NotificationChannel 声音需要重启手机

问题描述

我正在尝试使用我的自定义声音向我的应用程序提供本地通知。

我在 Visual Studio 中创建了简单的应用程序,我想在 FAB Click 上显示通知。我的问题是,通知正在显示,但没有声音。

但是......手机重启后 - 通知正常工作!

我的代码 - 调用的创建通道方法OnCreate()

    string channelId = "1";
    string channelName = "a";
    int notificationId = 0;

    private void createNotificationChannel()
    {
        var notMgr = (NotificationManager)GetSystemService(NotificationService);
        var uri = new Android.Net.Uri.Builder()
                .Scheme(ContentResolver.SchemeAndroidResource)
                .Authority(Resources.GetResourcePackageName(Resource.Raw.neworder4))
                .AppendPath(Resources.GetResourceTypeName(Resource.Raw.neworder4))
                .AppendPath(Resources.GetResourceEntryName(Resource.Raw.neworder4))
                .Build();
        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {

            if (notMgr.GetNotificationChannel(channelId) == null)
            {
                var channel = new NotificationChannel(channelId, channelName, NotificationImportance.Default);
                channel.Description = channelName;

                var aa = new AudioAttributes.Builder()
                        .SetContentType(AudioContentType.Sonification)
                        .SetUsage(AudioUsageKind.Notification).Build();
                channel.SetSound(uri, aa);

                notMgr.CreateNotificationChannel(channel);
            }
        }
    }

在 FAB 点击显示通知:

private void FabOnClick(object sender, EventArgs eventArgs)
    {
        var notMgr = (NotificationManager)GetSystemService(NotificationService);
        notificationId = notificationId + 1;


        var not = new NotificationCompat.Builder(this, channelId)
            .SetSmallIcon(Resource.Mipmap.ic_launcher)
            .SetContentText("test")
            .SetContentTitle("test")

            .Build();

        notMgr.Notify(notificationId, not);
    }

我做错了什么?

我不想要求我的用户在安装应用程序后重启手机。

Resource.Raw.neworder4 是 mp3 文件,设置为 Android Resource。

标签: androidxamarinxamarin.androidnotifications

解决方案


我使用以下代码为本地通知播放自定义声音,效果很好,你可以看看:

创建频道:

 string channelId = "location_notification";
    string channelName = "localnotification";
    int notificationId = 1000;
void CreateNotificationChannel()
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            // Notification channels are new in API 26 (and not a part of the
            // support library). There is no need to create a notification
            // channel on older versions of Android.
            return;
        }

        Android.Net.Uri sound = Android.Net.Uri.Parse("android.resource://" + Application.Context.PackageName + "/" + Resource.Raw.MyAudio);
        var aa = new AudioAttributes.Builder()
                .SetContentType(AudioContentType.Sonification)
                .SetUsage(AudioUsageKind.Notification).Build();
        var channelDescription = "local notification!!!!";
        var channel = new NotificationChannel(channelId, channelName, NotificationImportance.Max)
        {
            Description = channelDescription
        };
        channel.SetSound(sound,aa);
        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(channel);
    }

构建通知:

 private void Button3_Click(object sender, System.EventArgs e)
    {

        // Build the notification:
        var builder = new NotificationCompat.Builder(this, channelId)
                      .SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
                      .SetDefaults((int)NotificationDefaults.All)
                      .SetContentTitle("Button Clicked") // Set the title
                                                         //.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Alarm))
                      .SetSmallIcon(Resource.Drawable.addreminder); // This is the icon to display

        // Finally, publish the notification:
        var notificationManager = NotificationManagerCompat.From(this);
        notificationManager.Notify(notificationId, builder.Build());          
    }

推荐阅读