Skip to content

Commit

Permalink
fix notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
aleqsanbr committed May 26, 2024
1 parent b908816 commit efa9c2c
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions app/src/main/java/com/aleqsanbr/confirmatio/screens/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.graphics.RectangleShape
import com.aleqsanbr.compose.md_theme_dark_outlineVariant
import com.aleqsanbr.compose.md_theme_dark_secondaryContainer
import com.aleqsanbr.confirmatio.MainActivity

object NotificationPreferences {

Expand Down Expand Up @@ -357,11 +358,19 @@ fun NotificationsContent(navController: NavHostController) {
val notificationId = 1
val context = LocalContext.current
val channelId = "channel_id"
val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

val notificationBuilder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.logo_icon)
.setContentTitle("Напоминание")
.setContentText("Вы нажали на кнопку!")
.setContentTitle("Как вы себя чувствуете?")
.setContentText("Сделайте запись в дневнике")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)

var showPermissionWarningDialog by remember { mutableStateOf(false) }

var notificationTime by remember { mutableStateOf(NotificationPreferences.getNotificationTime(context)) }
Expand All @@ -387,6 +396,9 @@ fun NotificationsContent(navController: NavHostController) {
}
}

Text(text = "ℹ️ Для Confirmatio должно быть выдано разрешение на показ уведомлений. " +
"Чтобы это сделать, нажмите на кнопку \"Показать тестовое уведомление\".")

Button(
onClick = {
if (ContextCompat.checkSelfPermission(
Expand Down Expand Up @@ -526,19 +538,34 @@ class NotificationReceiver : BroadcastReceiver() {
val channelId = "channel_id"
val notificationId = 1

val notificationBuilder = NotificationCompat.Builder(context!!, channelId)
.setSmallIcon(R.drawable.logo_icon)
.setContentTitle("Напоминание")
.setContentText("Вы нажали на кнопку!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

val notificationManager = NotificationManagerCompat.from(context)
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
val intent1 = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

val notificationBuilder = context?.let {
NotificationCompat.Builder(it, channelId)
.setSmallIcon(R.drawable.logo_icon)
.setContentTitle("Как вы себя чувствуете?")
.setContentText("Сделайте запись в дневнике")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
}

val notificationManager = context?.let { NotificationManagerCompat.from(it) }
if (context?.let {
ContextCompat.checkSelfPermission(
it,
Manifest.permission.POST_NOTIFICATIONS
)
} == PackageManager.PERMISSION_GRANTED
) {
notificationManager.notify(notificationId, notificationBuilder.build())
if (notificationManager != null) {
if (notificationBuilder != null) {
notificationManager.notify(notificationId, notificationBuilder.build())
}
}
}
}
}
Expand Down

0 comments on commit efa9c2c

Please sign in to comment.