Unlock device before running notication action

ScaleScale Member ✭✭
edited December 2018 in Xamarin.Android

Hi Everyone,

I have a notification with two buttons (actions).
Which i handle in:

    public class NotificationActionHandler : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)

I want the user to unlock the device before being allowed to press the buttons.
I tried setting the notification to private but it will allow the button press on the lock screen.

How can i solve this?

Thanks!

Best Answer

Answers

  • ScaleScale Member ✭✭

    Hi YorkGo,

    Thanks for your reply. I had also found that thread and tried implementing the API >=26.
    But got stuck on two problems.

    KeyguardManager km = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    km.requestDismissKeyguard(this, null); // you may add callback listener here

    I can't find any examples anywhere on to make the callback for the requestDismissKeyguard function.
    Also the first parameter requires an Activity which i don't know how to get in the broadcast receiver.

    Code for the notification:

            Intent cancelIntent = new Intent(this, new NotificationActionHandler().Class);
            cancelIntent.SetAction("DENY");
            cancelIntent.PutExtra("IntentID", intentID);
            cancelIntent.PutExtra("Type", body);
            cancelIntent.PutExtra("Title", title);
            PendingIntent intentRead = PendingIntent.GetBroadcast(this, 0, cancelIntent, PendingIntentFlags.UpdateCurrent);
    
    
    
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            notificationBuilder
                .SetAutoCancel(true)
                .SetVisibility(NotificationCompat.VisibilitySecret)
                .SetPriority(NotificationCompat.PriorityMax)
                .SetDefaults(-1)
                .SetWhen(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds())
                .SetContentTitle(title)
                .SetContentText(body)
                .SetSmallIcon(Resource.Drawable.ic_logo_128)
                .SetContentInfo("info")
                .AddAction(Resource.Drawable.ic_logo_128, "Yes", intentImportant)
                .AddAction(Resource.Drawable.ic_logo_128, "No", intentRead);    
    
    
    [BroadcastReceiver(Enabled = true, Name = "OTP.OTP.NotificationActionHandler")]
    public class NotificationActionHandler : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
            var action = intent.Action;
    
  • ScaleScale Member ✭✭

    Thanks so much YorkGo, got it all working exactly as i wanted.

  • ScaleScale Member ✭✭

    @YorkGo said:

    I can't find any examples anywhere on to make the callback for the requestDismissKeyguard function.

    Create your own KeyguardDismissCallback, here is a simple demo:

    km.RequestDismissKeyguard(this, new MyKeyguardDismissCallback());
    
    
    public class MyKeyguardDismissCallback : KeyguardDismissCallback
            {
                public override void OnDismissError()
                {
                    base.OnDismissError();
                }
    
                public override void OnDismissSucceeded()
                {
                    base.OnDismissSucceeded();
                }
    
                public override void OnDismissCancelled()
                {
                    base.OnDismissCancelled();
                }
            }
    
    

    Also the first parameter requires an Activity which i don't know how to get in the broadcast receiver.

    You could use Xamarin.Android plugin to get the Android Application’s current Activity that is being displayed.

    var activity = Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity;
    

    Hi YorkGo,

    Today i noticed that when i am not debug and thus the application is not in the foreground:
    Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity returns NULL

    And with NULL km.RequestDismissKeyguard(NULL, new MyKeyguardDismissCallback());
    Doesn't do anything.. Do you have any ideas how i could solve this?

Sign In or Register to comment.