Reputation: 301
Hello guys i'm making app for tracking insulin injections for my mom. I want to make app that will create notification automatically every 7 p.m. Can you advice me how to do it? I tried to google it but i'm only found videos with notifications by clicking button but it will not work for me because i want my app to create notifications even when app is closed so i need something like Services that will always by alive even app is closed
Upvotes: 0
Views: 50
Reputation: 389
You firstly need to instantiate a Broadcast Receiver class and override their onReceive function.
Don't forget to add this to your manifest.
<receiver android:name=".AlertReceiver" />
Override the function as talked.
class AlertReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Glide.with(context).asBitmap().load(R.drawable.ic_diagnosis_24dp).into(object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) {}
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
val notificationHelper = NotificationHelper(context,resource,intent.extras!!.getInt("requestCode"))
val nb = notificationHelper.channelNotification
notificationHelper.manager?.notify(1, nb.build())
}
})
}
}
You can prepare a Notification Helper just in case you want to deal with things in a better format.
class NotificationHelper(base: Context?, healthReportIcon: Bitmap, requestCode: Int) : ContextWrapper(base) {
private val healthIcon = healthReportIcon
private var mManager: NotificationManager? = null
private lateinit var beforeTime: String
private var intentActivity: Class<*>
@TargetApi(Build.VERSION_CODES.O)
private fun createChannel() {
val channel = NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)
manager!!.createNotificationChannel(channel)
}
val manager: NotificationManager? get() {
if (mManager == null) {
mManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
return mManager
}
val channelNotification: NotificationCompat.Builder get() = NotificationCompat.Builder(applicationContext, channelID)
.setContentTitle("Reminder!")
.setContentText("Update medical records before $beforeTime")
.setSmallIcon(R.drawable.ic_round_local_hospital_24)
.setColor(ContextCompat.getColor(this,R.color.blue_diff))
.setLargeIcon(healthIcon)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setTimeoutAfter(1800000)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(baseContext, 0, Intent(baseContext, intentActivity), 0))
companion object {
const val channelID = "phoneId"
const val channelName = "phoneChannel"
}
init {
when (requestCode) {
0 -> {
beforeTime = "12:30 AM"
}
1 -> {
beforeTime = "6:30 AM"
}
2 -> {
beforeTime = "12:30 PM"
}
3 -> {
beforeTime = "6:30 PM"
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel()
}
intentActivity = if(FirebaseAuth.getInstance().currentUser!=null) {
HomeActivity::class.java
} else{
Splash::class.java
}
}
}
Finally, set or cancel your alarms in your codebase.
private fun startAlarms() {
val cal0 = Calendar.getInstance()
cal0[Calendar.HOUR_OF_DAY]=24
cal0[Calendar.MINUTE]=0
cal0[Calendar.SECOND]=0
val cal1 = Calendar.getInstance()
cal1[Calendar.HOUR_OF_DAY]=6
cal1[Calendar.MINUTE]=0
cal1[Calendar.SECOND]=0
val cal2 = Calendar.getInstance()
cal2[Calendar.HOUR_OF_DAY]=12
cal2[Calendar.MINUTE]=0
cal2[Calendar.SECOND]=0
val cal3 = Calendar.getInstance()
cal3[Calendar.HOUR_OF_DAY]=18
cal3[Calendar.MINUTE]=0
cal3[Calendar.SECOND]=0
if (cal0.before(Calendar.getInstance())) {
cal0.add(Calendar.DATE, 1)
}
if (cal1.before(Calendar.getInstance())) {
cal1.add(Calendar.DATE, 1)
}
if (cal2.before(Calendar.getInstance())) {
cal2.add(Calendar.DATE, 1)
}
if (cal3.before(Calendar.getInstance())) {
cal3.add(Calendar.DATE, 1)
}
val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlertReceiver::class.java)
intent.putExtra("requestCode",0)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal0.timeInMillis, PendingIntent.getBroadcast(this, 0,intent , PendingIntent.FLAG_UPDATE_CURRENT))
intent.putExtra("requestCode",1)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal1.timeInMillis, PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT))
intent.putExtra("requestCode",2)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal2.timeInMillis, PendingIntent.getBroadcast(this, 2,intent, PendingIntent.FLAG_UPDATE_CURRENT))
intent.putExtra("requestCode",3)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal3.timeInMillis, PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_UPDATE_CURRENT))
}
private fun cancelAlarms() {
val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlertReceiver::class.java)
alarmManager.cancel(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT))
alarmManager.cancel(PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT))
alarmManager.cancel(PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT))
alarmManager.cancel(PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT))
PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT).cancel()
PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT).cancel()
PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT).cancel()
PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT).cancel()
}
Hope this helped you out. Happy Coding! :)
Upvotes: 1