Reputation: 17
I would like to update the ContentText of a Foreground Notification so that the ContentText displays the date of the latest product.
I am using two global variables to keep a reference to the original Builder and notificationManager. UpdateNotificationContent() is called from another class, and I've supplied the updated text to SetContentText and called notificationManager.notify to try to update the builder.
I have an error with the following line (last line of 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'
Full code here.
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());
}
}
Upvotes: 1
Views: 1283
Reputation: 51
notificationManager must also be declared only once
notification_Manager = (NotificationManager)GetSystemService(NotificationService);
if (Build.VERSION.SdkInt > BuildVersionCodes.O)
{
'
'
notification_Manager.CreateNotificationChannel(channel);
}
then use the same variable notification_Manager.notify()
and notificationBuilder
for every notification update you make.
Upvotes: 0
Reputation: 10958
Do not create a new NotificationCompat.Builder. If you just want to update the content, use the same NotificationCompat.Builder
seetings with same id(NOTIFICATION_SERVICE_ID
) to cover.
Change:
string contextText = App.latestProduct.ProductSaveTime.ToString();
notificationBuilder = new NotificationCompat.Builder(this, "default")
.SetContentText(contextText);
notificationManager.Notify(NOTIFICATION_SERVICE_ID, notificationBuilder.Build());
To:
string contextText = "update";//I do not have you contextText, so i use string instead
notificationBuilder .SetContentText(contextText);
notificationManager.Notify(NOTIFICATION_SERVICE_ID, notificationBuilder .Build());
Upvotes: 1