After reading about 20 articles on this site, the official Android Developers site and elsewhere, it seems that I cannot find how to pass the exact date and time to fire a notification in Android and Kotlin
Does the notification builder has a method in which I can pass this time or should I keep trying with Alarm Receiver? I am not sure how to instantiate Alarm receiver in the ViewModel
AlarmReceiver
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.ALARM_SERVICE
import android.content.Intent
import com.example.spiritualvietnam.model.SpiritViewModel
import java.util.*
abstract class AlarmReceiver : BroadcastReceiver() {
abstract val viewModel: SpiritViewModel
override fun onReceive(context: Context, intent: Intent) {
println("dragos am ajuns la broadcast receiver si am transmis notificarea")
viewModel.showNotification(context)
// implement showing notification in this function
val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager
val alarmPendingIntent by lazy {
val intent = Intent(context, AlarmReceiver::class.java)
PendingIntent.getBroadcast(context, 0, intent, 0)
}
val HOUR_TO_SHOW_PUSH = viewModel.hourOfNotification.value
val MINUTE_TO_SHOW_PUSH = viewModel.minuteOfNotification.value
fun schedulePushNotifications() {
val calendar = GregorianCalendar.getInstance().apply {
if (get(Calendar.HOUR_OF_DAY) >= HOUR_TO_SHOW_PUSH!!) {
add(Calendar.DAY_OF_MONTH, 1)
}
set(Calendar.HOUR_OF_DAY, HOUR_TO_SHOW_PUSH)
if (MINUTE_TO_SHOW_PUSH != null) {
set(Calendar.MINUTE, MINUTE_TO_SHOW_PUSH)
}
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
AlarmManager.INTERVAL_DAY,
alarmPendingIntent
)
}
if (intent.action == "android.intent.action.BOOT_COMPLETED") {
schedulePushNotifications()
}
}
}
ViewModel
class SpiritViewModel: ViewModel() {
// at what hour should the notification appear
private var _hourOfNotification = MutableLiveData(18)
var hourOfNotification: LiveData<Int> = _hourOfNotification
// at what minute should the notification appear
private val _minuteOfNotification = MutableLiveData(30)
val minuteOfNotification: LiveData<Int> = _minuteOfNotification
// used for the notification
private companion object {
private const val CHANNEL_ID = "channel01"
}
// Push the notification here - mainly boilerplate copy-pastable code
fun showNotification(context: Context) {
createNotificationChannel(context)
val pattern = _hourOfNotification.value.toString() + _minuteOfNotification.value.toString() + "00"
val date = Date()
val notificationId = SimpleDateFormat(pattern).format(date).toInt()
println(" dragos notification id is "+ notificationId)
//handle notification click- should start SliderFragment Directly
val mainIntent = Intent(context, SlidingPhotosFragment::class.java)
mainIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
// here we use a NavDeepLinkBuilder from the Navigation subsection
val pendingIntent = NavDeepLinkBuilder(context)
.setComponentName(MainActivity::class.java)
.setGraph(R.navigation.mobile_navigation)
.setDestination(R.id.navigation_sliders)
.createPendingIntent()
// creating the notification builder
val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
notificationBuilder.setSmallIcon(R.drawable.ic_home_icon)
notificationBuilder.setContentTitle("Wait...")
notificationBuilder.setContentText("Give yourself a break by immersing in beautiful Vietnam")
notificationBuilder.priority = NotificationCompat.PRIORITY_HIGH
//cancel notification on click
notificationBuilder.setAutoCancel(true)
// add click intent
notificationBuilder.setContentIntent(pendingIntent)
//notification manager
val notificationManagerCompat = NotificationManagerCompat.from(context)
notificationManagerCompat.notify(notificationId, notificationBuilder.build())
}
// for api > 26 we need a notification channel
fun createNotificationChannel(context: Context){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name: CharSequence = "MyNotification"
val description = "The notification channel description"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val notificationChannel = NotificationChannel(CHANNEL_ID, name, importance)
notificationChannel.description = description
// NOTIFICATION_SERVICE
val notificationManager = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
}
}
}
The notification is started in a fragment when a button is clicked, but should be activated with the alarm receiver class
context?.let { viewModel.showNotification(it) }
AndroidManifest
<receiver android:name="com.example.spiritualvietnam.broadcast.AlarmReceiver" android:enabled="true"/>
<receiver android:name="com.example.spiritualvietnam.broadcast.BootReceiver" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
The notification appears only at the present time. I want to change somehow this time with a different one, to appear daily, even if the application is off. If I have to learn more about a specific topic please let me know!