Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Request and respect notification settings
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
  • Loading branch information
theimpulson committed Jan 15, 2024
1 parent 0ef4b9e commit 83df2e7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.content.SharedPreferences
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import com.google.firebase.messaging.FirebaseMessagingService
Expand Down Expand Up @@ -72,6 +73,7 @@ class PushNotificationService : FirebaseMessagingService(),
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
Log.i(TAG, "Got FCM push notification!")
if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) return

val pendingIntent = PendingIntent.getActivity(
this,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package io.aayush.relabs.ui.screens.alerts

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import android.util.Log
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
Expand All @@ -11,6 +20,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastAny
Expand All @@ -24,15 +34,29 @@ import io.aayush.relabs.ui.components.AlertItem
import io.aayush.relabs.ui.components.MainTopAppBar
import io.aayush.relabs.ui.navigation.Screen

private const val TAG = "AlertsScreen"

@Composable
fun AlertsScreen(
navHostController: NavHostController,
viewModel: AlertsViewModel = hiltViewModel()
) {
val context = LocalContext.current
val loading: Boolean by viewModel.loading.collectAsStateWithLifecycle()
val alerts: List<UserAlert>? by viewModel.alerts.collectAsStateWithLifecycle()
val postInfo: PostInfo by viewModel.postInfo.collectAsStateWithLifecycle()

val permissionRequestLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(),
onResult = { Log.i(TAG, "Notification permission: $it") }
)

LaunchedEffect(key1 = Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
permissionRequestLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}

LaunchedEffect(key1 = postInfo) {
if (postInfo.post.thread_id != 0) {
navHostController.navigate(Screen.Thread.withID(postInfo.post.thread_id))
Expand All @@ -44,6 +68,12 @@ fun AlertsScreen(
modifier = Modifier.fillMaxSize(),
topBar = {
MainTopAppBar(screen = Screen.Alerts, navHostController = navHostController) {
IconButton(onClick = { openNotificationSettings(context) }) {
Icon(
painter = painterResource(id = R.drawable.ic_notifications_edit),
contentDescription = ""
)
}
IconButton(
onClick = { viewModel.markAllAlerts(read = true) },
enabled = alerts?.fastAny { it.read_date == 0 } == true
Expand Down Expand Up @@ -81,3 +111,13 @@ fun AlertsScreen(
}
}
}

@SuppressLint("InlinedApi")
private fun openNotificationSettings(context: Context) {
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
putExtra("app_package", context.packageName)
putExtra("app_uid", context.applicationInfo.uid)
putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
}
context.startActivity(intent)
}
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_notifications_edit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M480,880Q447,880 423.5,856.5Q400,833 400,800L560,800Q560,833 536.5,856.5Q513,880 480,880ZM160,760L160,680L240,680L240,400Q240,317 290,252.5Q340,188 420,168L420,140Q420,115 437.5,97.5Q455,80 480,80Q505,80 522.5,97.5Q540,115 540,140L540,168Q551,171 561.5,174.5Q572,178 582,183L519,245Q509,243 500,241.5Q491,240 480,240Q414,240 367,287Q320,334 320,400L320,680L640,680L640,596L720,516L720,680L800,680L800,760L160,760ZM480,520L480,520Q480,520 480,520Q480,520 480,520Q480,520 480,520Q480,520 480,520L480,520L480,520L480,520L480,520L480,520L480,520ZM480,520L480,397L701,177Q710,168 721,164Q732,160 743,160Q755,160 766,164.5Q777,169 786,178L823,215Q831,224 835.5,235Q840,246 840,257Q840,268 836,279.5Q832,291 823,300L603,520L480,520ZM780,257L780,257L743,220L743,220L780,257ZM540,460L578,460L699,338L681,319L662,301L540,422L540,460ZM681,319L662,301L662,301L699,338L699,338L681,319Z"/>
</vector>

0 comments on commit 83df2e7

Please sign in to comment.