[英]Update Foreground Notification Text in Xamarin.Android
我想更新前台通知的 ContentText,以便 ContentText 顯示最新產品的日期。
我使用兩個全局變量來保留對原始 Builder 和 notificationManager 的引用。 從另一個 class 調用 UpdateNotificationContent(),我已將更新的文本提供給 SetContentText 並調用 notificationManager.notify 以嘗試更新構建器。
以下行(UpdateNotificationContent 的最后一行)出現錯誤: notificationManager.Notify(NOTIFICATION_SERVICE_ID, notificationBuilder.Build());
Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference'
完整代碼在這里。
class MyService : Service
{
private Handler handler;
private Action runnable;
private bool isStarted;
private int DELAY_BETWEEN_LOG_MESSAGES = 5000;
private int NOTIFICATION_SERVICE_ID = 1001;
private int NOTIFICATION_AlARM_ID = 1002;
private string NOTIFICATION_CHANNEL_ID = "1003";
private string NOTIFICATION_CHANNEL_NAME = "Update Notifications";
private NotificationCompat.Builder notificationBuilder;
private NotificationManager notificationManager
private void DispatchNotificationThatServiceIsRunning()
{
Intent intent = new Intent(this, typeof(CustomReceiver));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(
this,
1,
intent,
PendingIntentFlags.UpdateCurrent
);
string contextText = App.latestProduct.ProductSaveTime.ToString();
}
var notification = new Notification.Builder(this, default)
.SetChannelId("location_notification")
.SetContentTitle("AppName")
.SetContentText(contextText)
.SetSmallIcon(Resource.Drawable.icon)
.SetVisibility(NotificationVisibility.Secret)
.SetContentIntent(pendingIntent)
.SetOngoing(true)
.Build();
// Enlist this instance of the service as a foreground service
StartForeground(NOTIFICATION_SERVICE_ID, notification);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(NOTIFICATION_SERVICE_ID, notification);
}
// every 5 seconds push a notificaition
private void DispatchNotificationThatAlarmIsGenerated()
{
Intent intent = new Intent(this, typeof(CustomReceiver));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(
this,
1,
intent,
PendingIntentFlags.UpdateCurrent
);
string contextText = App.latestProduct.ProductSaveTime.ToString();
notificationBuilder = new NotificationCompat.Builder(this, "default")
.SetAutoCancel(false)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentIntent(pendingIntent)
.SetContentTitle("AppName")
.SetContentText(contextText);
notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(NOTIFICATION_AlARM_ID, notificationBuilder.Build());
}
public void UpdateNotificationContent()
{
string contextText = App.latestProduct.ProductSaveTime.ToString();
notificationBuilder = new NotificationCompat.Builder(this, "default")
.SetContentText(contextText);
notificationManager.Notify(NOTIFICATION_SERVICE_ID, notificationBuilder.Build());
}
}
不要創建新的 NotificationCompat.Builder。 如果您只想更新內容,請使用具有相同 ID( NOTIFICATION_SERVICE_ID
)的相同NotificationCompat.Builder
設置來覆蓋。
改變:
string contextText = App.latestProduct.ProductSaveTime.ToString();
notificationBuilder = new NotificationCompat.Builder(this, "default")
.SetContentText(contextText);
notificationManager.Notify(NOTIFICATION_SERVICE_ID, notificationBuilder.Build());
至:
string contextText = "update";//I do not have you contextText, so i use string instead
notificationBuilder .SetContentText(contextText);
notificationManager.Notify(NOTIFICATION_SERVICE_ID, notificationBuilder .Build());
notificationManager 也必須只聲明一次
notification_Manager = (NotificationManager)GetSystemService(NotificationService);
if (Build.VERSION.SdkInt > BuildVersionCodes.O)
{
'
'
notification_Manager.CreateNotificationChannel(channel);
}
然后為您所做的每個通知更新使用相同的變量notification_Manager.notify()
和notificationBuilder
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.