通知不会在选定的时间显示

bvjveswy  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(314)

我的应用程序中有一个时间选择器、一个createnoificationchannel函数和一个sendnotifications函数。一切正常,但当我按下按钮时,通知会在几秒钟后生效,因为按下按钮的时间不在时间选择器选择的时间内!!。我不知道如何将时间选择器与sendnotifications函数链接。任何帮助都将不胜感激。
仅限kotlin或java。。
我的设置活动

private val CHNNEL_ID = "adthan_channel_id"
private val notificationId = 101

private lateinit var picker : MaterialTimePicker
private lateinit var calender : Calendar
private lateinit var alarmManager: AlarmManager
private lateinit var pendingIntent: PendingIntent

我的通知按钮

//صلاة الفجر

      fajrSalah.setOnCheckedChangeListener { buttonView, isChecked ->

            if (isChecked){
                showTimePicker()

                editor.putBoolean("fajrSalahChecked", isChecked)
                editor.apply()

            }else{
                editor.remove("fajrSalahChecked")
                editor.apply()

              //  cancelAlarm()
                toast("تم حذف المنبة")
            }
        }
        zuherSalah.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked){
                showTimePicker()

                editor.putBoolean("zuherSalahChecked", isChecked)
                editor.apply()

            }else{
                editor.remove("zuherSalahChecked")
                editor.apply()

             //   cancelAlarm()
                toast("تم حذف المنبة")
            }

 }
    adhanNotifcationBtn.setOnClickListener {

        sendNotifications("موعد صلاة العصر","بسم الله الرحمن الرحيم")

 }

我的通知功能

private fun showTimePicker(){

    picker = MaterialTimePicker.Builder()
        .setTimeFormat(TimeFormat.CLOCK_12H)
        .setHour(12)
        .setMinute(0)
        .setTitleText("حدد الموعد الذي تريدة")
        .build()

    picker.show(supportFragmentManager, "AdhanNotifacations")

    picker.addOnPositiveButtonClickListener {

        calender = Calendar.getInstance()
        calender[Calendar.HOUR_OF_DAY] = picker.hour
        calender[Calendar.MINUTE] = picker.minute
        calender[Calendar.SECOND] = 0
        calender[Calendar.MILLISECOND] = 0

    }
}
  private fun createNoificationChannel(){

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

        val name : CharSequence = " أشعارات الصلاوات"
        val descriptionText = "منبة لأشعارات الصلاوات"
        val importance = NotificationManager.IMPORTANCE_HIGH
        val channel = NotificationChannel(CHNNEL_ID,name,importance).apply {
            description = descriptionText
        }
        val notificationManager : NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
      //  val notificationManager :NotificationManager = getSystemService( NotificationManager::class.java)
        notificationManager.createNotificationChannel(channel)
    }

}
private fun sendNotifications( title : String, content: String){

    alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager

    val intent = Intent(this, MainActivity::class.java).apply {
       flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
     pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

    alarmManager.setInexactRepeating(

        AlarmManager.RTC_WAKEUP,calender.timeInMillis,
        AlarmManager.INTERVAL_DAY, pendingIntent
    )

    val builder = NotificationCompat.Builder(this, CHNNEL_ID)
        .setSmallIcon(R.drawable.ic_fajr_icon)
        .setContentTitle(title)
        .setContentText(content)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)
        .setDefaults(NotificationCompat.DEFAULT_ALL)
        .setPriority(NotificationCompat.PRIORITY_HIGH)

    with(NotificationManagerCompat.from(this)){
        notify(notificationId, builder.build())
    }
}
yyyllmsg

yyyllmsg1#

假设您正在使用datepickerdialog和timepickerdialog,请使用 setOnClickListener 检索值并将其存储在某个位置(通过变量或 SharedPreferences ). 然后,用这些值构造内容,并将其作为 sendNotification() .

相关问题