【问题标题】:Update Foreground Notification Text in Xamarin.Android更新 Xamarin.Android 中的前台通知文本
【发布时间】:2021-03-16 12:33:41
【问题描述】:

我想更新前台通知的 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, 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());
        }
}

【问题讨论】:

    标签: c# android xamarin xamarin.android android-notifications


    【解决方案1】:

    不要创建新的 NotificationCompat.Builder。如果你只是想更新内容,使用相同的NotificationCompat.Builder 相同的 id(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());
    

    【讨论】:

    • 非常感谢您的回答。我遇到了这个错误:“对象引用未设置为对象的实例。”为notificationBuilder. SetContentText(contextText);。 notificationBuilder 为空。知道如何解决这个问题吗?
    • 您在方法外声明的notificationBuilder。推送通知后需要调用此UpdateNotificationContent 方法。如果您仍然无法这样做,请使用更新的 contextText 创建相同的NOTIFICATION_SERVICE_ID 通知。
    • 再次感谢。我尝试调用 UpdateNotificationContent() 但它不起作用。我对如何通过创建相同的通知来更新 contextText 感到有些困惑。我在这里更新了 UpdateNotificationContent() 方法:wtools.io/paste-code/b4lv。但是,我有这个错误:Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference' 在 UpdateNotificationContent() 中的 Intent intent = new Intent(this, typeof(CustomReceiver)); 行上;
    • CustomReceiver 代码:wtools.io/paste-code/b4lw
    • 使用相同的 id,通知将被覆盖。我不确定是什么导致了这个错误。 UpdateNotificationContent 中没有 Intent
    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多