Update notification disables action button

Olena_KramarenkoOlena_Kramarenko Member ✭✭
edited February 21 in Xamarin.Android

Hello guys! I need to create notification (in foreground service), which has two action buttons and updates content text every second. I faced with next problem: when notification is static - action buttons work well, but when notification is updated - action buttons don't do anything, and only sometimes click on it does action for notification and ripple effect goes on notification, but not on the action button

public Notification BuildNotification(Timer timerModel, bool isFullScreen = false)
        {
            _intRandom = new Random().Next() + 1000;

            var pendingIntent = CreatePendingIntentWithStack(TimerConst.ActionResumeTimer, timerModel);         

            var message = $"{timerModel.ProgressLeftTimeSpan:hh} : {timerModel.ProgressLeftTimeSpan:mm} : {timerModel.ProgressLeftTimeSpan:ss}";
            if ((int)timerModel.ProgressLeftTimeSpan.TotalSeconds < 0) message = $"- {message}";

            _builder.SetContentTitle(Shared.TimerTitle);
            _builder.SetContentText(message);

            _builder.SetSmallIcon(Resource.Drawable.ic_timer_black_24dp);

            _builder.SetPriority((int) NotificationImportance.High);    

            if (isFullScreen)
            {
                SetFullScreenNotification(pendingIntent);
            }
            else
            {
                _builder.SetContentIntent(pendingIntent);
            }

            _builder.SetOnlyAlertOnce(true);

            // Add Dismiss button intent in notification.
            var pendingDismissIntent = CreatePendingIntentWithStack(TimerConst.ActionDismissTimer, timerModel);
            _dismissAction = new NotificationCompat.Action(Resource.Drawable.ic_timer_black_24dp,
                                                                                             Shared.DismissButtonTitle, pendingDismissIntent);
            _builder.AddAction(_dismissAction);

            // Add Restart button intent in notification.
            var pendingRestartIntent = CreatePendingIntentWithStack(TimerConst.ActionRestartTimer, timerModel);
            _restartAction = new NotificationCompat.Action(Resource.Drawable.ic_timer_black_24dp,
                                                                                            Shared.RestartButtonTitle, pendingRestartIntent);
            _builder.AddAction(_restartAction);

            _notification = _builder.Build();
            _notification.Flags = NotificationFlags.AutoCancel & NotificationFlags.OngoingEvent;

            return _notification;
        }



    public void UpdateNotification(int notificationId, Timer timerModel)
        {           
            var message = $"{timerModel.ProgressLeftTimeSpan:hh} : {timerModel.ProgressLeftTimeSpan:mm} : {timerModel.ProgressLeftTimeSpan:ss}";
            if ((int)timerModel.ProgressLeftTimeSpan.TotalSeconds < 0) message = $"- {message}";

            if ((int)timerModel.ProgressLeftTimeSpan.TotalSeconds == 0)
            {
                var pendingIntent = CreatePendingIntentWithStack(TimerConst.ActionResumeTimer, timerModel);
                SetFullScreenNotification(pendingIntent);
            }
            _builder.SetOnlyAlertOnce(true);

            _builder.SetContentText(message).SetNumber((int)timerModel.ProgressLeftTimeSpan.TotalSeconds);
            _notification = _builder.Build();
            _notification.Flags = NotificationFlags.AutoCancel & NotificationFlags.OngoingEvent;

            // Publish the notification:
            _notificationManager.Notify(notificationId, _notification);
        }




    private void SetFullScreenNotification(PendingIntent pendingIntent)
        {
            _builder.SetFullScreenIntent(pendingIntent, true);
            _builder.SetVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
            _builder.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
        }

Can someone help me to fix it?

Best Answer

Answers

  • A mistake was in another place. This code works well

Sign In or Register to comment.