-
Notifications
You must be signed in to change notification settings - Fork 63
feat: add notification for sync and download, closes #328 #396
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0a9a94d
feat: add notification for sync and download, closes #328
Heropowwa 540f408
feat: ask for notification permission
Heropowwa 486ba49
fix: actually fix permission
Heropowwa 1199442
Merge branch 'master' into feat/ProgressNotification
Heropowwa 43000cf
fix: remove ios implementation
Heropowwa 3842f8f
fix: remove delay
Heropowwa 5476410
Merge remote-tracking branch 'origin/master' into feat/ProgressNotifi…
Heropowwa 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
84 changes: 84 additions & 0 deletions
84
composeApp/src/androidMain/kotlin/paige/navic/domain/manager/NotificationManager.android.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,84 @@ | ||
| package paige.navic.domain.manager | ||
|
|
||
| import android.Manifest | ||
| import android.app.NotificationChannel | ||
| import android.app.NotificationManager as AndroidNotificationManager | ||
| import android.content.Context | ||
| import android.content.pm.PackageManager | ||
| import android.os.Build | ||
| import androidx.core.app.ActivityCompat | ||
| import androidx.core.app.NotificationCompat | ||
| import androidx.core.content.ContextCompat | ||
| import paige.navic.util.android.ActivityProvider | ||
| import paige.navic.util.core.ResourceProvider | ||
|
|
||
| actual class NotificationManager( | ||
| private val context: Context, | ||
| private val resourceProvider: ResourceProvider, | ||
| private val activityProvider: ActivityProvider | ||
| ) { | ||
| private val notificationManager = | ||
| context.getSystemService(Context.NOTIFICATION_SERVICE) as AndroidNotificationManager | ||
|
|
||
| init { | ||
| createNotificationChannel() | ||
| } | ||
|
|
||
| private fun createNotificationChannel() { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| val channel = NotificationChannel( | ||
| CHANNEL_ID, | ||
| "Library Updates", | ||
| AndroidNotificationManager.IMPORTANCE_LOW | ||
| ).apply { | ||
| description = "Notifications for library sync and downloads" | ||
| } | ||
| notificationManager.createNotificationChannel(channel) | ||
| } | ||
| } | ||
|
|
||
| actual fun showProgressNotification( | ||
| id: Int, | ||
| title: String, | ||
| message: String, | ||
| progress: Float, | ||
| indeterminate: Boolean | ||
| ) { | ||
| val builder = NotificationCompat.Builder(context, CHANNEL_ID) | ||
| .setSmallIcon(resourceProvider.icNavic) | ||
| .setContentTitle(title) | ||
| .setContentText(message) | ||
| .setPriority(NotificationCompat.PRIORITY_LOW) | ||
| .setOngoing(true) | ||
| .setOnlyAlertOnce(true) | ||
| .setProgress(100, (progress * 100).toInt(), indeterminate) | ||
|
|
||
| notificationManager.notify(id, builder.build()) | ||
| } | ||
|
|
||
| actual fun cancelNotification(id: Int) { | ||
| notificationManager.cancel(id) | ||
| } | ||
|
|
||
| actual fun requestPermissions() { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
| if (ContextCompat.checkSelfPermission( | ||
| context, | ||
| Manifest.permission.POST_NOTIFICATIONS | ||
| ) != PackageManager.PERMISSION_GRANTED | ||
| ) { | ||
| activityProvider.currentActivity?.let { activity -> | ||
| ActivityCompat.requestPermissions( | ||
| activity, | ||
| arrayOf(Manifest.permission.POST_NOTIFICATIONS), | ||
| 101 | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val CHANNEL_ID = "library_updates" | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
composeApp/src/androidMain/kotlin/paige/navic/util/android/ActivityProvider.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,42 @@ | ||
| package paige.navic.util.android | ||
|
|
||
| import android.app.Activity | ||
| import android.app.Application | ||
| import android.os.Bundle | ||
|
|
||
| class ActivityProvider : Application.ActivityLifecycleCallbacks { | ||
| var currentActivity: Activity? = null | ||
| private set | ||
|
|
||
| override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | ||
| currentActivity = activity | ||
| } | ||
|
|
||
| override fun onActivityStarted(activity: Activity) { | ||
| currentActivity = activity | ||
| } | ||
|
|
||
| override fun onActivityResumed(activity: Activity) { | ||
| currentActivity = activity | ||
| } | ||
|
|
||
| override fun onActivityPaused(activity: Activity) { | ||
| if (currentActivity == activity) { | ||
| currentActivity = null | ||
| } | ||
| } | ||
|
|
||
| override fun onActivityStopped(activity: Activity) { | ||
| if (currentActivity == activity) { | ||
| currentActivity = null | ||
| } | ||
| } | ||
|
|
||
| override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {} | ||
|
|
||
| override fun onActivityDestroyed(activity: Activity) { | ||
| if (currentActivity == activity) { | ||
| currentActivity = null | ||
| } | ||
| } | ||
| } |
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
20 changes: 20 additions & 0 deletions
20
composeApp/src/commonMain/kotlin/paige/navic/domain/manager/NotificationManager.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,20 @@ | ||
| package paige.navic.domain.manager | ||
|
|
||
| expect class NotificationManager { | ||
| fun showProgressNotification( | ||
| id: Int, | ||
| title: String, | ||
| message: String, | ||
| progress: Float, | ||
| indeterminate: Boolean = false | ||
| ) | ||
|
|
||
| fun cancelNotification(id: Int) | ||
|
|
||
| fun requestPermissions() | ||
| } | ||
|
|
||
| object NotificationIds { | ||
| const val DOWNLOAD_LIBRARY = 1001 | ||
| const val SYNC_LIBRARY = 1002 | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think you can somehow send download notifications without permission because i’ve seen apps do that, not sure if you did that here im too lazy to look, but I would definitely prefer that
For example, chrome or discord can download a file in the background, with progress being shown in a silent notification, and it will notify when its finished (or at least thats how it works for me on grapheneos, idk how it behaves on other roms)
Edit: you need to use
android.app.DownloadManagerto download the file, then dosetShowRunningNotification(true)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.
I tried not putting it and putting it and don't asking for permission but it doesn't work, idk honestly