C# 在Xamarin.Android中更新前台通知文本
我想更新前台通知的ContentText,以便ContentText显示最新产品的日期 我使用两个全局变量来保持对原始生成器和notificationManager的引用。UpdateNotificationContent()是从另一个类调用的,我已将更新的文本提供给SetContentText,并调用notificationManager.notify尝试更新生成器 以下行(UpdateNotificationContent的最后一行)有错误:C# 在Xamarin.Android中更新前台通知文本,c#,android,xamarin,xamarin.android,android-notifications,C#,Android,Xamarin,Xamarin.android,Android Notifications,我想更新前台通知的ContentText,以便ContentText显示最新产品的日期 我使用两个全局变量来保持对原始生成器和notificationManager的引用。UpdateNotificationContent()是从另一个类调用的,我已将更新的文本提供给SetContentText,并调用notificationManager.notify尝试更新生成器 以下行(UpdateNotificationContent的最后一行)有错误:notificationManager.Notif
notificationManager.Notify(NOTIFICATION\u SERVICE\u ID,notificationBuilder.Build())代码>
Java.Lang.NullPointerException:“尝试在空对象引用上调用虚拟方法”android.content.res.Resources android.content.Context.getResources()
完整代码
不要创建新的NotificationCompat.Builder。如果您只想更新内容,请使用相同id(通知服务id
)的相同通知compat.Builder
页面覆盖
更改:
string contextText = App.latestProduct.ProductSaveTime.ToString();
notificationBuilder = new NotificationCompat.Builder(this, "default")
.SetContentText(contextText);
notificationManager.Notify(NOTIFICATION_SERVICE_ID, notificationBuilder.Build());
致:
非常感谢你的回答。我遇到了以下错误:notificationBuilder的“对象引用未设置为对象的实例”。SetContentText(contextText)代码>。notificationBuilder为空。你知道如何解决这个问题吗?你在方法之外声明的notificationBuilder
。推送通知后,需要调用此UpdateNotificationContent
方法。如果您仍然无法执行此操作,请使用updateing contextText创建一个相同的通知\u服务\u ID
通知。再次感谢。我尝试调用UpdateNotificationContent(),但不起作用。我对如何通过创建相同的通知来更新contextText有点困惑。我已在此处更新了UpdateNotificationContent()方法:。但是,我有以下错误:Java.Lang.NullPointerException:“尝试调用空对象引用上的虚拟方法”Java.Lang.String android.content.Context.getPackageName()”
行Intent Intent=new Intent(this,typeof(CustomReceiver));CustomReceiver代码:使用相同的id,通知将被覆盖。我不确定是什么导致了这个错误。UpdateNotificationContent
中没有Intent
。
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());