简体   繁体   English

更新 Xamarin.Android 中的前台通知文本

[英]Update Foreground Notification Text in Xamarin.Android

I would like to update the ContentText of a Foreground Notification so that the ContentText displays the date of the latest product.我想更新前台通知的 ContentText,以便 ContentText 显示最新产品的日期。

I am using two global variables to keep a reference to the original Builder and notificationManager.我使用两个全局变量来保留对原始 Builder 和 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.从另一个 class 调用 UpdateNotificationContent(),我已将更新的文本提供给 SetContentText 并调用 notificationManager.notify 以尝试更新构建器。

I have an error with the following line (last line of UpdateNotificationContent): notificationManager.Notify(NOTIFICATION_SERVICE_ID, notificationBuilder.Build());以下行(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' 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());
        }
}

Do not create a new NotificationCompat.Builder.不要创建新的 NotificationCompat.Builder。 If you just want to update the content, use the same NotificationCompat.Builder seetings with same id( NOTIFICATION_SERVICE_ID ) to cover.如果您只想更新内容,请使用具有相同 ID( NOTIFICATION_SERVICE_ID )的相同NotificationCompat.Builder设置来覆盖。

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());

notificationManager must also be declared only once notificationManager 也必须只声明一次

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.然后为您所做的每个通知更新使用相同的变量notification_Manager.notify()notificationBuilder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM