the notification doesn't show up

0

Issue

In my app I am using a time picker where the user chooses the time he wants and then when the time comes the app supposed to send the notification to the user. I did everything but the app doesn’t send the notification to the user. Please help

P.C. I added the receiver on manifest

It is ok to use java or kotlin..

Alarm receiver

class AlarmReceiver : BroadcastReceiver() {


override fun onReceive(context: Context?, intent: Intent?) {

    val i = Intent(context,MainActivity::class.java)
    intent!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    val pendingActivity = PendingIntent.getActivity(context,0,i,0)

    val builder = NotificationCompat.Builder(context!!," AdhanNotifacations")
        .setSmallIcon(R.drawable.ic_fajr_icon)
        .setContentTitle("موعد الصلاة حل")
        .setContentText("ان الصلاة كانت على المؤمنين كتابا موقوتا")
        .setAutoCancel(true)
        .setDefaults(NotificationCompat.DEFAULT_ALL)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(pendingActivity)

    val noifacationManagar = NotificationManagerCompat.from(context)
    noifacationManagar.notify(123,builder.build())
}
}

Setting Activity
fajrSalah id is a switch button

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()
        }else{
            //cancelAlarm()
        }
    }
    adhanNotifcationBtn.setOnClickListener {

        setAlarm()

    }

}

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 setAlarm(){
    alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
    val intent = Intent(this, AlarmReceiver::class.java)
    pendingIntent = PendingIntent.getBroadcast(this, 0, intent,0)

    alarmManager.setRepeating(

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

    toast("تم تفعيل المنبه")
}

private fun cancelAlarm(){

    alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
    val intent = Intent(this, AlarmReceiver::class.java)
    pendingIntent = PendingIntent.getBroadcast(this, 0, intent,0)

    alarmManager.cancel(pendingIntent)

    toast("تم الغاء المنبه")

}
private fun createNoificationChannel(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

        val name : CharSequence = " almuslimChannel"
        val description = "Channel for Alarm Manager"
        val importance = NotificationManager.IMPORTANCE_HIGH
        val channel = NotificationChannel("AdhanNotifacations",name,importance)
        channel.description = description
        val notifcationManager = getSystemService(
            NotificationManager::class.java
        )

        notifcationManager.createNotificationChannel(channel)
    }
}

Solution

I found out that I made a space on my channel id that was on my receiver, which causes the receiver to not send the notifications to the users.

now this is the solution to my issue

class AlarmReceiver : BroadcastReceiver() {


override fun onReceive(context: Context?, intent: Intent?) {
    val i = Intent(context,MainActivity::class.java)
    intent!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    val pendingActivity = PendingIntent.getActivity(context,0,i,0)

    val builder = NotificationCompat.Builder(context!!,"AdhanNotifacations")
        .setSmallIcon(R.drawable.ic_fajr_icon)
        .setContentTitle("موعد الصلاة حل")
        .setContentText("ان الصلاة كانت على المؤمنين كتابا موقوتا")
        .setAutoCancel(true)
        .setDefaults(NotificationCompat.DEFAULT_ALL)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(pendingActivity)

    val noifacationManagar = NotificationManagerCompat.from(context)
    noifacationManagar.notify(123,builder.build())
}
}

Answered By – Abood

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More