通知未在选定时间显示

时间:2021-07-03 21:43:02

标签: java android kotlin push-notification

我的应用中有一个时间选择器、一个 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())
    }
}

1 个答案:

答案 0 :(得分:1)

假设您正在使用 DatePickerDialogTimePickerDialog,请使用 setOnClickListener 检索值并将其存储在某处(通过变量或 SharedPreferences )。然后,使用这些值构造 content 并将其作为 sendNotification() 的第二个参数传入。