From d78ef5648930361e37a8cdeee6d2f07717a59ea4 Mon Sep 17 00:00:00 2001 From: Matthieu <24-artectrex@users.noreply.shinice.net> Date: Thu, 3 Nov 2022 15:16:27 +0100 Subject: [PATCH] Update permissions --- app/src/main/AndroidManifest.xml | 11 ++-------- .../java/org/pixeldroid/app/MainActivity.kt | 21 +++++++++++++++++-- .../NotificationsWorker.kt | 13 ++++++++++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8334af4a..39ff12e9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ + @@ -20,13 +21,11 @@ + android:theme="@style/AppTheme"> - = Build.VERSION_CODES.TIRAMISU) { + notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) + } } } + private val notificationPermissionLauncher = registerForActivityResult( + ActivityResultContracts.RequestPermission() + ) { isGranted: Boolean -> + if (isGranted) enablePullNotifications(this) + } + // Checks if the activity was launched from a notification from another account than the // current active one, and if so switches to that account private fun notificationFromOtherUser(): Boolean { diff --git a/app/src/main/java/org/pixeldroid/app/utils/notificationsWorker/NotificationsWorker.kt b/app/src/main/java/org/pixeldroid/app/utils/notificationsWorker/NotificationsWorker.kt index af1d924e..0637f796 100644 --- a/app/src/main/java/org/pixeldroid/app/utils/notificationsWorker/NotificationsWorker.kt +++ b/app/src/main/java/org/pixeldroid/app/utils/notificationsWorker/NotificationsWorker.kt @@ -1,12 +1,15 @@ package org.pixeldroid.app.utils.notificationsWorker +import android.Manifest import android.app.NotificationChannel import android.app.NotificationChannelGroup import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent +import android.content.pm.PackageManager import android.os.Build +import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.work.CoroutineWorker @@ -143,7 +146,10 @@ class NotificationsWorker( ) with(NotificationManagerCompat.from(applicationContext)) { - notify(uniqueUserId.hashCode(), groupBuilder.build()) + if (ActivityCompat.checkSelfPermission(applicationContext, + Manifest.permission.POST_NOTIFICATIONS + ) == PackageManager.PERMISSION_GRANTED + ) notify(uniqueUserId.hashCode(), groupBuilder.build()) } } @@ -216,7 +222,10 @@ class NotificationsWorker( with(NotificationManagerCompat.from(applicationContext)) { // notificationId is a unique int for each notification - notify((uniqueUserId + notification.id).hashCode(), builder.build()) + if (ActivityCompat.checkSelfPermission(applicationContext, + Manifest.permission.POST_NOTIFICATIONS + ) == PackageManager.PERMISSION_GRANTED + ) notify((uniqueUserId + notification.id).hashCode(), builder.build()) } }