-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(reminders): request notification permissions #19167
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
Draft
ericli3690
wants to merge
5
commits into
ankidroid:main
Choose a base branch
from
ericli3690:ericli3690-review-reminders-permission-request
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+311
−50
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b2b306
feat(permissions): create PermissionsBottomSheet
ericli3690 643ce3d
feat(reminders): NotificationsPermissionFragment
ericli3690 d0acf10
feat(reminders): request persistence prefs
ericli3690 9a2b75b
feat(reminders): replace old notification permission request logic
ericli3690 8a3a21d
feat(reminders): request notification permissions from review reminders
ericli3690 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...id/src/main/java/com/ichi2/anki/ui/windows/permissions/NotificationsPermissionFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2025 Eric Li <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ichi2.anki.ui.windows.permissions | ||
|
||
import android.os.Build | ||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.annotation.RequiresApi | ||
import com.ichi2.anki.R | ||
import com.ichi2.utils.Permissions | ||
import timber.log.Timber | ||
|
||
/** | ||
* Permissions fragment shown on the [PermissionsBottomSheet] for requesting notification permissions | ||
* from the user. This permission only needs to be requested at or above API 33. | ||
* | ||
* Requested permissions: | ||
* 1. Notifications: [Permissions.postNotification]. | ||
* Used to view and cancel sync progress. | ||
* Used for review reminder notifications. | ||
*/ | ||
@RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
class NotificationsPermissionFragment : PermissionsFragment(R.layout.notifications_permission) { | ||
/** | ||
* Launches the OS dialog for requesting notification permissions. | ||
*/ | ||
private val notificationPermissionLauncher = | ||
registerForActivityResult( | ||
ActivityResultContracts.RequestMultiplePermissions(), | ||
) { requestedPermissions -> | ||
Timber.i("Notification permission result: $requestedPermissions") | ||
if (!requestedPermissions.all { it.value }) { | ||
showToastAndOpenAppSettingsScreen(R.string.manually_grant_permissions) | ||
} | ||
} | ||
|
||
override fun onViewCreated( | ||
view: View, | ||
savedInstanceState: Bundle?, | ||
) { | ||
val notificationPermission = view.findViewById<PermissionsItem>(R.id.notification_permission) | ||
Permissions.postNotification?.let { | ||
notificationPermission.offerToGrantOrRevokeOnClick( | ||
notificationPermissionLauncher, | ||
arrayOf(Permissions.postNotification), | ||
) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/permissions/PermissionsBottomSheet.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) 2025 Eric Li <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.ichi2.anki.ui.windows.permissions | ||
|
||
import android.os.Build | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.annotation.RequiresApi | ||
import androidx.core.os.BundleCompat | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.fragment.app.commit | ||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment | ||
import com.google.android.material.button.MaterialButton | ||
import com.ichi2.anki.PermissionSet | ||
import com.ichi2.anki.R | ||
|
||
/** | ||
* BottomSheet that requests permissions from the user. | ||
* | ||
* The full-screen [PermissionsActivity] which launches on initial app installation should be used to request | ||
* mandatory permissions from the user that AnkiDroid cannot run without. This more relaxed BottomSheet | ||
* should be used to request optional permissions from the user, and can be launched as the user gradually | ||
* encounters features that require permissions rather than being shoved in the face of every first-time user. | ||
*/ | ||
@RequiresApi(Build.VERSION_CODES.TIRAMISU) | ||
class PermissionsBottomSheet : BottomSheetDialogFragment() { | ||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View? = inflater.inflate(R.layout.permissions_bottom_sheet, container, false) | ||
|
||
override fun onViewCreated( | ||
view: View, | ||
savedInstanceState: Bundle?, | ||
) { | ||
super.onViewCreated(view, savedInstanceState) | ||
val closeButton = view.findViewById<MaterialButton>(R.id.close_button) | ||
closeButton.setOnClickListener { dismiss() } | ||
|
||
val permissionSet = | ||
requireNotNull(BundleCompat.getParcelable(requireArguments(), PERMISSION_SET_ARGUMENT_KEY, PermissionSet::class.java)) { | ||
"Permission set cannot be null" | ||
} | ||
val permissionsFragment = | ||
requireNotNull(permissionSet.permissionsFragment?.getDeclaredConstructor()?.newInstance()) { | ||
"invalid permissionsFragment" | ||
} | ||
view.post { | ||
childFragmentManager.commit { | ||
replace(R.id.bottom_sheet_fragment_container, permissionsFragment) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
/** | ||
* Unique fragment tag for launching this bottom sheet. | ||
*/ | ||
private const val FRAGMENT_TAG = "notifications_bottom_sheet" | ||
|
||
/** | ||
* Arguments key for the [PermissionSet] to launch this BottomSheet with. | ||
*/ | ||
private const val PERMISSION_SET_ARGUMENT_KEY = "permission_set" | ||
|
||
/** | ||
* Starts this BottomSheet with the provided [PermissionSet]. | ||
*/ | ||
fun launch( | ||
fragmentManager: FragmentManager, | ||
permissionsSet: PermissionSet, | ||
) { | ||
val bottomSheet = | ||
PermissionsBottomSheet().apply { | ||
arguments = | ||
Bundle().apply { | ||
putParcelable(PERMISSION_SET_ARGUMENT_KEY, permissionsSet) | ||
} | ||
} | ||
bottomSheet.show(fragmentManager, FRAGMENT_TAG) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should likely be using
shouldShowRequestPermissionRationale
https://developer.android.com/training/permissions/requesting#request-permission
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@david-allison Yep, I've thought about that before. From the PR description:
Basically, the internal workings of
shouldShowRequestPermissionRationale
are kind of counterintuitive and the name isn't really self-documenting. Google really should have designed it to return an enum instead of a boolean. See the top answer here and the top answer here for more information. The function only returns true if the app has requested the permission before and the user has clicked "deny" without clicking "don't ask again". It returns false when it's requesting a permission for the first time. I'd like to show the AnkiDroid notification request when the user performs an action that requires the permission for the first time, soshouldShowRequestPermissionRationale
doesn't work for this use case. That's why I created a Pref.I've also considered adding another case to trigger the permission request BottomSheet if the user has clicked "deny" without clicking "don't ask again", which we could use
shouldShowRequestPermissionRationale
for (i.e. we would show the BottomSheet both if the user is opening the app for the first time, via the Pref, OR if the user has clicked "deny" without clicking "don't ask again", viashouldShowRequestPermissionRationale
). The problem with that is I don't see it happening very often. With this PR, the only times we request notification permissions from the user, we do it through the BottomSheet above. For a user to be logged as having clicked "deny" without clicking "don't ask again", they would have to 1. trigger the BottomSheet, 2. read the message, 3. click on the enabling toggle, and then 4. click deny. I don't see many users changing their minds after clicking the toggle to grant notification permissions (which is why, in that scenario, the BottomSheet currently opens up the OS settings to allow the user to grant notification permissions directly, assuming they performed a misclick or that something went wrong with launching the OS permission request dialog).I will concede that the fact that we only show the BottomSheet at most two times to users (once on the first sync and once on the first review reminder created) is a bit weird. Perhaps we should have the BottomSheet have a chance to show up again if a user who hasn't granted notification permissions performs an action that requires notification permissions. For example, we currently only request notification permissions at most once when the user first performs a sync. Maybe we can request it again on the 5th sync and the 10th sync, and only then fully give up? This has the potential to be annoying to the user, though. Similarly with requesting notification permissions when the user creates review reminders: we currently only request permissions when the user creates a review reminder for the first time, and can potentially change it so that it requests it again on the 5th and 10th review reminder created, and never again after that. This change would mean editing the
Prefs.syncNotifsRequestShown
to be an Int instead of a Boolean, and incrementing it based on how many times the permission request BottomSheet has been shown.Thoughts?