-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add notification permissions and bump min sdk to 24
- Loading branch information
Showing
9 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...e/src/main/kotlin/co/anitrend/core/android/helpers/notification/NotificationExtensions.kt
This file contains 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 co.anitrend.core.android.helpers.notification | ||
|
||
import android.Manifest | ||
import android.content.Context | ||
import android.os.Build | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import androidx.core.content.ContextCompat | ||
import androidx.core.content.PermissionChecker.PERMISSION_GRANTED | ||
import androidx.fragment.app.FragmentActivity | ||
import co.anitrend.core.android.helpers.notification.NotificationHelper.Companion.POST_NOTIFICATION_PERMISSION_REQUEST_CODE | ||
import co.anitrend.core.android.helpers.notification.config.NotificationConfig | ||
|
||
|
||
fun Context.hasNotificationPermissionFor(config: NotificationConfig): Boolean { | ||
val hasPermission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PERMISSION_GRANTED | ||
} else { | ||
NotificationManagerCompat.from(this).areNotificationsEnabled() | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && hasPermission) { | ||
val channel = NotificationManagerCompat.from(this).getNotificationChannel(config.name) | ||
if (channel != null && channel.importance == config.importance) { | ||
return false | ||
} | ||
} | ||
return hasPermission | ||
} | ||
|
||
fun FragmentActivity.requestPostNotificationPermission() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && ContextCompat.checkSelfPermission( | ||
/* context = */ this, | ||
/* permission = */ Manifest.permission.POST_NOTIFICATIONS, | ||
) != PERMISSION_GRANTED | ||
) { | ||
ActivityCompat.requestPermissions( | ||
/* activity = */ this, | ||
/* permissions = */ arrayOf(Manifest.permission.POST_NOTIFICATIONS), | ||
/* requestCode = */ POST_NOTIFICATION_PERMISSION_REQUEST_CODE, | ||
) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...-core/src/main/kotlin/co/anitrend/core/android/helpers/notification/NotificationHelper.kt
This file contains 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,33 @@ | ||
package co.anitrend.core.android.helpers.notification | ||
|
||
import android.app.NotificationChannel | ||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.NotificationManagerCompat | ||
import co.anitrend.core.android.helpers.notification.config.NotificationConfig | ||
|
||
class NotificationHelper( | ||
private val notificationManager: NotificationManagerCompat | ||
) { | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
fun createNotificationChannels() { | ||
val channels = NotificationConfig.entries.map { config -> | ||
return with (NotificationChannel(config.name, config.title, config.importance)) { | ||
description = config.description | ||
group = config.group | ||
setShowBadge(true) | ||
enableLights(false) | ||
} | ||
} | ||
notificationManager.createNotificationChannels(channels) | ||
} | ||
|
||
companion object { | ||
const val POST_NOTIFICATION_PERMISSION_REQUEST_CODE = 0x12 | ||
|
||
fun notificationVisibilityFor(isAdult: Boolean) = | ||
if (isAdult) NotificationCompat.VISIBILITY_SECRET else NotificationCompat.VISIBILITY_PUBLIC | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...rc/main/kotlin/co/anitrend/core/android/helpers/notification/config/NotificationConfig.kt
This file contains 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,29 @@ | ||
package co.anitrend.core.android.helpers.notification.config | ||
|
||
import android.app.NotificationManager | ||
|
||
enum class NotificationConfig( | ||
val title: String, | ||
val description: String, | ||
val importance: Int, | ||
val group: String, | ||
) { | ||
GENERAL( | ||
title = "General", | ||
description = "AniTrend specific notifications", | ||
importance = NotificationManager.IMPORTANCE_DEFAULT, | ||
group = "co.anitrend.notification.group.GENERAL", | ||
), | ||
ANILIST( | ||
title = "AniList", | ||
description = "AniList related notifications", | ||
importance = NotificationManager.IMPORTANCE_DEFAULT, | ||
group = "co.anitrend.notification.group.ANILIST", | ||
), | ||
ANNOUNCEMENT( | ||
title = "Announcements", | ||
description = "Announcements and other important information", | ||
importance = NotificationManager.IMPORTANCE_HIGH, | ||
group = "co.anitrend.notification.group.ANNOUNCEMENT", | ||
) | ||
} |
This file contains 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 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 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 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 |
---|---|---|
|
@@ -41,4 +41,4 @@ class App : AniTrendApplication() { | |
override fun restartDependencyInjection() { | ||
stopKoin() | ||
} | ||
} | ||
} |
This file contains 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 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