更新 Xamarin.Android 上的通知文本

问题描述

目前我有一个应用程序可以注册通知

        public void StartAppService()
        {
            ...

            using (var intent = new Intent(Application.Context,typeof(RunningAppService)))
            {
                Application.Context.StartForegroundService(intent);
            }
        }
        private const int ServiceRunningNotificationId = 10000;

        public override StartCommandResult OnStartCommand(Intent intent,StartCommandFlags flags,int startId)
        {
            CreateNotificationChannel();

            using (var notification = new Notification.Builder(this,"10111")
                .SetContentTitle("Artemis HZ")
                .SetContentText("90 HZ")
                .SetSmallIcon(Resource.Drawable.icon)
                .Setongoing(true)
                .Build())
            {
                StartForeground(ServiceRunningNotificationId,notification);
            }

            ...

            return StartCommandResult.Sticky;
        }
        private void CreateNotificationChannel()
        {
            const string channelName = "TopAppServiceChannel";
            const string channelDescription = "TopAppServiceChannel";
            using (var channel = new NotificationChannel("10111",channelName,NotificationImportance.Default)
            {
                Description = channelDescription
            })
            {
                using (var notificationmanager = (notificationmanager)GetSystemService(NotificationService))
                {
                    notificationmanager?.CreateNotificationChannel(channel);
                }
            }
        }

我想以编程方式更改它的 SetContentText。 我试着这样做:

                using (var notificationBuilder = new Notification.Builder(this,"10111")
                    .SetContentText($"{(int)hz} HZ"))
                {
                    using (var notificationmanager = (notificationmanager)GetSystemService(NotificationService))
                    {
                        notificationmanager?.Notify(10111,notificationBuilder.Build());
                    }
                }

然后什么也没有发生。它只在应用启动的前几秒有效,但之后它就保持原样,不会再次改变。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)