Is it possible to wake up the device so that the user can see a push notification on the lock screen

mbullock976mbullock976 Member ✭✭
edited July 2 in Xamarin.Android

Hello,
I have a server that sends a push notification to my mobile phone. I'm able to receive + display the push notification on the lock screen however if the lock screen has timed out i.e sleep mode, then when the phone receives the notification, it does not wake up the phone, so the user can see the notification on the lock screen.

I want it to match what IOS does, because that will wake up the device, so the user can see the notification on the lock screen

I'm using NotificationNBUilder when overriding OnMessageReceived of FirebaseMessagingService

public override void OnMessageReceived(RemoteMessage message)
{
var notificationManager = (NotificationManager)Application.Context.GetSystemService(NotificationService);
if (notificationManager == null) return;

        var userContext = GetUserContext();
        if (userContext == null || !userContext.IsLoggedIn || string.IsNullOrWhiteSpace(userContext.SessionId)) return;

        var encrypted = GetDataBoolValue(message, "encrypted");
        if (!encrypted) return;
        var title = GetDataStringValue(message, "title", userContext.SessionId);
        var body = GetDataStringValue(message, "body", userContext.SessionId);
        var eventName = GetDataStringValue(message, "eventName", userContext.SessionId);
        var eventArgs = GetDataStringValue(message, "eventArgs", userContext.SessionId, false);

        var builder = new Notification.Builder(this);
        builder.SetContentTitle(body);
        builder.SetContentText(title);
        builder.SetAutoCancel(true);
        builder.SetLocalOnly(true);
        builder.SetSmallIcon(Resource.Drawable.tray_icon);
        builder.SetColor(ContextCompat.GetColor(this, Resource.Color.design_default_color_primary));
        builder.SetPriority(1);
        builder.SetDefaults(NotificationDefaults.All);

        var intent = new Intent(this, typeof(MainActivity));
        intent.PutExtra("eventName", eventName);
        intent.PutExtra("eventArgs", eventArgs);

        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);
        builder.SetContentIntent(pendingIntent);

        var messageId = Interlocked.Increment(ref _messageId);
        notificationManager.Notify(messageId, builder.Build());
    }

Thanks

Best Answer

  • LeonLuLeonLu Xamurai
    Accepted Answer

    You can try to following code to weak up your device.

         createNotification(); //your implementation
    
            PowerManager pm = (PowerManager)this.GetSystemService(Context.PowerService);
            bool isScreenOn =  pm.IsInteractive ; // check if screen is on
            if (!isScreenOn)
            {
                PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.ScreenDim  | WakeLockFlags.AcquireCausesWakeup, "myApp:notificationLock");
                wl.Acquire(3000); //set your time in milliseconds
            }
    

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    Accepted Answer

    You can try to following code to weak up your device.

         createNotification(); //your implementation
    
            PowerManager pm = (PowerManager)this.GetSystemService(Context.PowerService);
            bool isScreenOn =  pm.IsInteractive ; // check if screen is on
            if (!isScreenOn)
            {
                PowerManager.WakeLock wl = pm.NewWakeLock(WakeLockFlags.ScreenDim  | WakeLockFlags.AcquireCausesWakeup, "myApp:notificationLock");
                wl.Acquire(3000); //set your time in milliseconds
            }
    
Sign In or Register to comment.