Create a notification days before the event

Hello,

I am trying to create an application that prompt notification yearly and 5 days before the date.
for example in the next code I am trying to be notify when is between the day and five days early of a birthday. But does not work.
if (totalDiasjoseBirthday in 360L…365L) { also I have tried with ((totalDiasjoseBirthday >360L) and ((totalDiasjoseBirthday <=360L))

It is working with the conditional but when is the same day of the aniversary.
if (totalDiasjoseBirthday == 365L)

As you can see I am not an expert, just trying to do my own up and continue learning.

I appreciate a lot any help to understand the problem.

@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    val intent = Intent(this, LauncherActivity::class.java)
    val pendingIntent = PendingIntent.getActivities(this, 0, arrayOf(intent), PendingIntent.FLAG_UPDATE_CURRENT)

    //For test
  
   import android.app.*

import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.time.LocalDate
import java.time.Period
import java.time.temporal.ChronoUnit

@Suppress(“DEPRECATION”)
class MainActivity : AppCompatActivity() {

lateinit var notificationManager : NotificationManager
lateinit var notificationChannel : NotificationChannel
lateinit var builder : Notification.Builder
private val channelId = “com.example.dates.notificationexample”
private val description = “Test Notification”

val joseBirthday = LocalDate.parse(“2016-11-08”)

var nextjoseBirthday = joseBirthday.withYear(dateToday.getYear())
if (nextjoseBirthday.isBefore(dateToday) || nextjoseBirthday.isEqual(dateToday)) {
nextjoseBirthday = nextjoseBirthday.plusYears(1);
}

    var totalDiasjoseBirthday = ChronoUnit.DAYS.between(dateToday, nextjoseBirthday)

if (totalDiasjoseBirthday in 360L…365L) {

        TextMessage.text = "Today is bla bla!!!"

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationChannel = NotificationChannel(channelId, description,
                NotificationManager.IMPORTANCE_HIGH
            )
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.CYAN
            notificationChannel.enableVibration(true)
            notificationManager.createNotificationChannel(notificationChannel)


            builder = Notification.Builder(this,channelId)
                .setContentTitle("My example")
                .setContentText("Today is bla.. !!!")
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(BitmapFactory.decodeResource(this.resources,R.drawable.one))
                .setContentIntent(pendingIntent)
        }else{
            builder = Notification.Builder(this)
                .setContentTitle("My Honney")
                .setContentText("Today is .....bla bla !!!")
                .setSmallIcon(R.drawable.one)
                .setLargeIcon(BitmapFactory.decodeResource(this.resources,R.drawable.one))
                .setContentIntent(pendingIntent)
        }
        notificationManager.notify(1234,builder.build())

}
}

Careful that all years are not 365 days.

1 Like

Thanks Bjonnh,

That is not the problem I’m trying to resolve. But thanks.