通知没有显示
the notification doesn't show up
在我的应用程序中,我使用了一个时间选择器,用户可以在其中选择他想要的时间,然后在时间到来时应用程序应该向用户发送通知。我做了一切,但应用程序没有向用户发送通知。请帮助
P.C。我在清单上添加了接收器
可以用java或者kotlin..
报警接收器
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())
}
}
设置Activity
fajrSalah id 是一个切换按钮
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)
}
}
我发现我在接收器上的频道 ID 上设置了 space,导致接收器无法向用户发送通知。
现在这是我的问题的解决方案
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())
}
}
在我的应用程序中,我使用了一个时间选择器,用户可以在其中选择他想要的时间,然后在时间到来时应用程序应该向用户发送通知。我做了一切,但应用程序没有向用户发送通知。请帮助
P.C。我在清单上添加了接收器
可以用java或者kotlin..
报警接收器
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())
}
}
设置Activity fajrSalah id 是一个切换按钮
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)
}
}
我发现我在接收器上的频道 ID 上设置了 space,导致接收器无法向用户发送通知。
现在这是我的问题的解决方案
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())
}
}