[英]Xamarin foreground service notification won't change its text
我使用可以正常启动的前台服务,会显示第一个通知,但是尽管NotificationManager.Notify()
方法可以正常运行且没有错误,但通知文本不会更改。 我尝试调试,代码每1秒就可以完美地运行一个计时器,但是更新没有发生。 -必须更新为Datetime.Now
在我的服务类别中:
private void ActivityTimer_Elapsed(object sender, ElapsedEventArgs e)
{
updateNotification();
}
private NotificationChannel createNotificationChannel()
{
var channelId = Constants.NOTIFICATION_CHANNELID;
var channelName = "My Notification Service";
var Channel = new NotificationChannel(channelId, channelName, Android.App.NotificationImportance.None);
Channel.LightColor = Android.Resource.Color.HoloBlueBright;
Channel.LockscreenVisibility = NotificationVisibility.Public;
return Channel;
}
private void createForegroundService()
{
var mNotificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
mNotificationManager.CreateNotificationChannel(createNotificationChannel());
}
var notificationBuilder = new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNELID);
var notification = getNotification();
StartForeground(Constants.NOTIFICATION_ID, notification);
}
private void updateNotification()
{
var mNotificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
mNotificationManager.CreateNotificationChannel(createNotificationChannel());
}
var notification = getNotification();
mNotificationManager.Notify(Constants.NOTIFICATION_ID, notification);
}
private Notification getNotification()
{
PendingIntent pIntent = PendingIntent.GetActivity(this,0,new Intent(this,typeof(MainActivity)),0);
return new Notification.Builder(this,Constants.NOTIFICATION_CHANNELID)
.SetColor(Android.Resource.Color.DarkerGray)
.SetContentTitle("My service name")
.SetOngoing(true)
.SetContentText(DateTime.Now.ToString("T"))
.SetSmallIcon(Resource.Drawable.notification_icon_background)
.SetContentIntent(pIntent).Build();
}
解决了。 问题出在这里: var Channel = new NotificationChannel(channelId, channelName, Android.App.NotificationImportance.**None**);
<-保留给NotificationImportance.Default
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.