通知不适用于变量 (Android)

Notification doesn't work with variables (Android)

这个愚蠢的错误很奇怪。我想在特定日期的特定时间发送通知。实际有效——当我不使用变量时。要理解我的意思,请看这段代码:

// 28.08.2019 at 00:01 AM
calendar.set(Calendar.DAY_OF_MONTH, 28)
calendar.set(Calendar.MONTH, 7)
calendar.set(Calendar.YEAR, 2019)
calendar.set(Calendar.HOUR_OF_DAY, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 1)

记住:月份以 0 开头,因此对于实际月份我需要使用 7。正如我所说,使用此代码可以正常工作。但是现在我为月份和日期声明了一个变量,所以:

// day = 28
val dayNoti = day
// month = 8
val monthNoti = month - 1
// 28.08.2019 at 00:01 AM
calendar.set(Calendar.DAY_OF_MONTH, dayNoti)
calendar.set(Calendar.MONTH, monthNoti)
calendar.set(Calendar.YEAR, 2019)
calendar.set(Calendar.HOUR_OF_DAY, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 1)

而且它不起作用!这太烦人了我不知道为什么会这样。顺便说一句:我需要声明这个变量,因为代码部分在一个函数中,我用不同的值多次调用这个函数。

编辑 (1):

这就是完整的功能:

// IMPORTANT: Before I'm calling the function, I did already 'month - 1'. It's the same result: it doesn't work
fun notification(hour: Int, minute: Int, day: Int, month: Int) {
    val titleNotification: String
    val descNotification: String

    val _intent = Intent(this, AlarmBroadcastReceiver::class.java)
    _intent.putExtra("dataNotification_title", titleNotification)
    _intent.putExtra("dataNotification_desc", descNotification)

    val randomCode = 7015

    val pendingIntent = PendingIntent.getBroadcast(this, randomCode, _intent, 0)
    val alarmManager = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = System.currentTimeMillis()

    calendar.set(Calendar.DAY_OF_MONTH, day)
    calendar.set(Calendar.MONTH, month)
    calendar.set(Calendar.YEAR, 2019)
    calendar.set(Calendar.HOUR_OF_DAY, hour)
    calendar.set(Calendar.MINUTE, minute)
    calendar.set(Calendar.SECOND, 0)

    if (System.currentTimeMillis() < calendar.timeInMillis){
        timeUntilAlert = calendar.timeInMillis
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeUntilAlert, pendingIntent)
    }
}

我找到了 "solution"。一切都很好,错误是我在 for 循环中调用了通知函数,但我忘记为每个通知更改 randomCode。每个函数都有特定的值,但是 AlarmBroadcastReceiverrandomCode 是相同的,这就是它不起作用的原因。

它在没有变量的情况下工作的原因是,因为我单独调用了函数,没有 for 循环。

不过还是谢谢大家。我的愚蠢错误。