Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Android): Adding full screen intent support #2112

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

mateyFromTheBlock
Copy link

@mateyFromTheBlock mateyFromTheBlock commented Jul 31, 2021

Adding support for full screen intent, for high priority notifications such as incoming phone call or alarm clock.
For more elaborate description on the uses of full screen intents you can view the following links.

https://developer.android.com/training/notify-user/time-sensitive

https://developer.android.com/reference/android/app/Notification.Builder#setFullScreenIntent(android.app.PendingIntent,%20boolean)

Motivation:
Allowing the app to launch and display an activity when device is locked.

Expected behavior:
When the user is using the device the notification will be displayed persistently (will not get "minimized" after a few seconds).
When the device is locked the app will be launched and device will get unlocked.

How to test:
After making the requested changes according to the instructions in the README file you can test it in two cases.

  1. When the app is in the background.
  • In this case the expected behavior will be a persistent notification which will not get "minimized" after a few seconds
  1. When the device is locked.
  • In this case the expected behavior will be that the device will get unlocked and app will be launched.

@mateyFromTheBlock mateyFromTheBlock changed the title Adding full screen intent support feat(Android): Adding full screen intent support Jul 31, 2021
@hariks789
Copy link

Its working as per the mentioned expectations on oxygen OS(Android 11).
@Dallas62 @mateyFromTheBlock
Any plans for releasing this?

@mateyFromTheBlock
Copy link
Author

@hariks789 I'm waiting for @Dallas62 to review this PR.
If you have any suggestions on how I can promote this PR let me know please.

@hariks789
Copy link

@mateyFromTheBlock Update after testing this on Xiaomi Redmi Note 3 ( Android 6.0.1), Full-screen Intent was not working on the lock screen.
Had to add FLAG_TURN_SCREEN_ON & FLAG_SHOW_WHEN_LOCKED to MainActivity to make it work


@Override
  protected void onStart() {
    super.onStart();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
      setShowWhenLocked(true);
      setTurnScreenOn(true);
    } else {
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    }
}

@mateyFromTheBlock
Copy link
Author

@hariks789 Thanks for the feedback, will validate and update PR accordingly.

@mateyFromTheBlock
Copy link
Author

@hariks789 I've tested it on Android 6.0 and it failed to turn on the screen, so I added the flags as you mentioned and its working but only when the app is in the background before turning the screen off.
When the app is in the foreground before turning off the screen, the screen won't turn on.
I've managed to turn on the screen with the PowerManager API like so.

  @Override
  protected void onStart() {
    super.onStart();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
      setShowWhenLocked(true);
      setTurnScreenOn(true);
    } else {
      PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
      PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myapp:wakeLock");
      wl.acquire();

      getWindow().addFlags(
              WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
              | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
              | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
              | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
      );
    }
  }

Do you experience the same behavior?

@adigeo
Copy link

adigeo commented Oct 18, 2021

This works like a charm, what an excellent contribution!

@hariks789
Copy link

hariks789 commented Oct 19, 2021

@hariks789 I've tested it on Android 6.0 and it failed to turn on the screen, so I added the flags as you mentioned and its working but only when the app is in the background before turning the screen off. When the app is in the foreground before turning off the screen, the screen won't turn on. I've managed to turn on the screen with the PowerManager API like so.

  @Override
  protected void onStart() {
    super.onStart();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
      setShowWhenLocked(true);
      setTurnScreenOn(true);
    } else {
      PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
      PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myapp:wakeLock");
      wl.acquire();

      getWindow().addFlags(
              WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
              | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
              | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
              | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
      );
    }
  }

Do you experience the same behavior?

Hey, @mateyFromTheBlock I didn't test that scenario, will test with these changes and post the update.

Apart from this, I found another issue, having the FLAG_SHOW_WHEN_LOCKED or setShowWhenLocked(true);
has a downside. If the user keeps the app in the foreground and locks the screen, One can use the app directly without unlocking the phone after turning on the screen. This is probably out of the current scope, it seems we can only set these flags while creating an activity and cannot update it dynamically.

The only solution I could find so far is to have a separate activity to handle lock screen UI with FLAG_SHOW_WHEN_LOCKED flags and mainactivity without FLAG_SHOW_WHEN_LOCKED flag. I haven't implemented that, I am looking other options as this is complex. Let me know your thoughts

@mateyFromTheBlock
Copy link
Author

Hey @hariks789, one solution I can think of to the issue you presented could be to create a dedicated screen to navigate to once we are showing a full screen intent and from that screen we can disable the navigation in such a way that the user could not browse the app which means the user will be "stuck" on that screen.

@Dallas62 Dallas62 changed the base branch from master to dev November 8, 2021 08:52
@Dallas62
Copy link
Collaborator

Dallas62 commented Nov 8, 2021

Hi,

Just find out that Notifee is now free and open-source.
For this kind of feature, feel free to look at: https://notifee.app/react-native/docs/android/behaviour

I will not have time to maintain this kind of behaviour. If you want to be a part of maintainers, feel free to ask.

Regards

@mawais786
Copy link

@mateyFromTheBlock excellent job. But I'm confused how do I show the custom full screen when notification arrive?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants