changes
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.example.scanwich
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
||||
class MealReminderReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
|
||||
NotificationHelper.scheduleReminders(context)
|
||||
return
|
||||
}
|
||||
|
||||
val mealType = intent.getStringExtra("meal_type") ?: "repas"
|
||||
showNotification(context, mealType)
|
||||
}
|
||||
|
||||
private fun showNotification(context: Context, mealType: String) {
|
||||
val channelId = "meal_reminders"
|
||||
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val channel = NotificationChannel(
|
||||
channelId,
|
||||
"Rappels de repas",
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
).apply {
|
||||
description = "Notifications pour ne pas oublier d'entrer vos repas"
|
||||
}
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
val activityIntent = Intent(context, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
}
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
context, 0, activityIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
val notification = NotificationCompat.Builder(context, channelId)
|
||||
.setSmallIcon(android.R.drawable.ic_dialog_info) // À remplacer par l'icône de l'app si dispo
|
||||
.setContentTitle("N'oubliez pas votre $mealType !")
|
||||
.setContentText("Prenez un moment pour enregistrer ce que vous avez mangé.")
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setAutoCancel(true)
|
||||
.build()
|
||||
|
||||
val notificationId = when(mealType.lowercase()) {
|
||||
"déjeuner" -> 1
|
||||
"dîner" -> 2
|
||||
"souper" -> 3
|
||||
else -> 0
|
||||
}
|
||||
notificationManager.notify(notificationId, notification)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user