How to ask permission for Bubble in my app?

856 Views Asked by At

I am using bubble feature in my app for Android 10. So I need to ask permission to enable Bubble feature. If users agree to the permission, then need to go through the exact path of enabling it. How do I achieve that. Thanks in advance.

1

There are 1 best solutions below

2
Will Harmon On

I can answer this for Android 11.

This first method will check if Bubbles are enabled or not.

/** Returns true if bubbles are enabled for this app */
private boolean canDisplayBubbles() {
  if (Build.VERSION.SDK_INT < MIN_SDK_BUBBLES) {
    return false;
  }

  // NotificationManager#areBubblesAllowed does not check if bubbles have been globally disabled,
  // so we use this check as well.
  boolean bubblesEnabledGlobally;
  try {
    bubblesEnabledGlobally = Settings.Global.getInt(getContentResolver(), "notification_bubbles") == 1;
  } catch (Settings.SettingNotFoundException e) {
    // If we're not able to read the system setting, just assume the best case.
    bubblesEnabledGlobally = true;
  }

  NotificationManager notificationManager = getSystemService(NotificationManager.class);
  return bubblesEnabledGlobally && notificationManager.areBubblesAllowed();
}

This second method will launch a settings page asking the user to give permission for your app to display Bubbles.

/** Launches a settings page requesting the user to enable Bubbles for this app */
private void requestBubblePermissions() {
  startActivityForResult(
    new Intent(Settings.ACTION_APP_NOTIFICATION_BUBBLE_SETTINGS)
      .putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()),
    REQUEST_CODE_BUBBLES_PERMISSION);
}