Home > Software engineering >  Notification runs randomly rather than at set time
Notification runs randomly rather than at set time

Time:11-14

I think I have set my alarm manager to run at 7am then at 24 hour intervals after that. It should change a image view and then send a notification. Instead it sends a notification a minute or 2 after closing or opening the app and occasionally changes the image. Can someone please explain where I went wrong? or how I can fix this?

main activity -

        val mIntent = Intent(this, MyReceiver::class.java)

        val calendar: Calendar = Calendar.getInstance()
        calendar.setTimeInMillis(System.currentTimeMillis())
        calendar.set(Calendar.HOUR_OF_DAY, 7)
        calendar.set(Calendar.MINUTE, 0)


        val mPendingIntent = PendingIntent.getBroadcast(this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT)
        val mAlarmManager = this
            .getSystemService(Context.ALARM_SERVICE) as AlarmManager
        mAlarmManager.setRepeating(
            AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),86400000, mPendingIntent,
        )

MyReciver -

class MyReceiver : BroadcastReceiver() {


@RequiresApi(Build.VERSION_CODES.O)
override fun onReceive(context: Context, intent: Intent) {

    val titles = arrayOf("Become inspired!", "Check out this quote!", "A new quote appeared!", "Daily quote available!")
    val title = titles.random()


        val notificationChannel =
            NotificationChannel("My Channel", "New Quote", NotificationManager.IMPORTANCE_DEFAULT).apply {
                description = "Alerts when A new daily quote is set!"
            }

    val builder = NotificationCompat.Builder(context!!, "My Channel")
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle(title)
            .setContentText("A new daily quote is available for viewing")

    with(NotificationManagerCompat.from(context)){
        createNotificationChannel(notificationChannel)
        notify(1, builder.build())
    }

    val quotes = arrayOf(R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i5, 
R.drawable.i6, R.drawable.i7, R.drawable.i8, R.drawable.i9, R.drawable.i10, R.drawable.i11, R.drawable.i12)
    val quote = quotes.random()

    val prefs = PreferenceManager.getDefaultSharedPreferences(context)
        with(prefs.edit()) {
            putInt("paintings", quote)
            apply()
        }
}
}

Other -

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.other)


        val imageView = findViewById<ImageView>(R.id.paininass)
        val prefs = PreferenceManager.getDefaultSharedPreferences(this)
        val quote = prefs.getInt("paintings", R.drawable.i5)
        imageView.setImageResource(quote)

CodePudding user response:

Like CommonsWare says in the comments, using the current day and setting the time to 7AM is usually going to put that in the past, and as the AlarmManager#setRepeating docs say:

If the stated trigger time is in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval.

So yeah, you need to add a day if it's in the past. You can do that by comparing currentTimeMillis to Calendar#getTimeInMillis, or using its before function, or however you want to do it. Then just call add(Calendar.DAY_OF_MONTH, 1)

  • Related