【发布时间】:2019-01-15 19:30:00
【问题描述】:
我使用正常启动的前台服务,显示我的第一个通知,但是虽然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();
}
【问题讨论】:
标签: c# xamarin service notifications foreground