微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在 Xamarin.Android 中更新前台通知文本

我想更新前台通知的 ContentText,以便 ContentText 显示最新产品的日期。

我使用两个全局变量来保持对原始 Builder 和 notificationmanager 的引用。 UpdateNotificationContent() 是从另一个调用的,我已将更新的文本提供给 SetContentText 并调用 notificationmanager.notify 以尝试更新构建器。

以下行(UpdateNotificationContent 的最后一行)出现错误notificationmanager.Notify(NOTIFICATION_SERVICE_ID,notificationBuilder.Build());

java.lang.NullPointerException: '尝试在空对象引用上调用虚拟方法 'android.content.res.Resources android.content.Context.getResources()'

完整代码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,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(NotificationCompat.Builder) 的相同 NOTIFICATION_SERVICE_ID 设置来覆盖。

变化:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。