Skip to content

Commit

Permalink
🚸 Ask for notification permission
Browse files Browse the repository at this point in the history
  • Loading branch information
Importantus committed Sep 27, 2024
1 parent ca3b0db commit 0a6ec6e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
10 changes: 8 additions & 2 deletions app/src/main/java/digital/fischers/coinsaw/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ class MainActivity : ComponentActivity() {
}
}

shortcutHelper.groupObserver(this)
notificationHelper.clearGroupNotifications(this)
try {
notificationHelper.askForNotificationPermission(this)

shortcutHelper.groupObserver(this)
notificationHelper.clearGroupNotifications(this)
} catch (e: Exception) {
Log.e("MainActivity", "Error setting up notifications", e)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package digital.fischers.coinsaw.notifications

import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.util.Log
import androidx.core.app.ActivityCompat.requestPermissions
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.asLiveData
Expand Down Expand Up @@ -46,21 +47,28 @@ class NotificationHelper(
}
}

fun askForNotificationPermission(activity: Activity) {
if (notificationManager.areNotificationsEnabled()) return
requestPermissions(
activity,
arrayOf("android.permission.POST_NOTIFICATIONS"),
1
)
}

private fun dismissNotification(id: Int) {
notificationManager.cancel(id)
}

suspend fun showChangelogNotification(entry: Entry) {
// Show a notification for the changelog entry
if (!notificationManager.areNotificationsEnabled()) return
val notification = entry.toNotification(context, groupRepository, userRepository) ?: return
Log.d("NotificationHelper", "Showing notification for entry ${entry.id.hashCode()}")
notificationManager.notify(entry.id.hashCode(), notification)
}

fun clearGroupNotifications(lifecycleOwner: LifecycleOwner) {
groupRepository.getAllGroupsStream().asLiveData().observe(lifecycleOwner) { groups ->
groups.forEach { group ->
Log.d("NotificationHelper", "Checking group ${group.name} ${group.open}")
if (group.open == true) {
val liveData =
changelogRepository.getChangelogByGroupAddedLocallyAfterTimestampSynced(
Expand All @@ -71,15 +79,13 @@ class NotificationHelper(

liveData.observe(lifecycleOwner) { changelog ->
changelog.forEach { entry ->
Log.d("NotificationHelper", "Dismissing notification for entry ${entry.id.hashCode()}")
dismissNotification(entry.id.hashCode())
}
}

groupObservers[group.id] = liveData
} else {
// Cancel observer
Log.d("NotificationHelper", "Removing observer for group ${group.id}")
groupObservers[group.id]?.removeObservers(lifecycleOwner)
}
}
Expand Down

0 comments on commit 0a6ec6e

Please sign in to comment.