I have a problem with notifications (NotificationManager class) in my android app. I made channel and notification. I read about the problem with 26 build and made a channel for notifications, but it doesn't solve my problem. But I have the next error in logcat.
E/NotificationService: No Channel found for pkg=com.gmail.mtswetkov.ocrraces, channelId=com.google.MTsvetkov.CHANNEL_ID, id=103, tag=null, opPkg=com.gmail.mtswetkov.ocrraces, callingUid=10087, userId=0, incomingUserId=0, notificationUid=10087, notification=Notification(channel=com.google.MTsvetkov.CHANNEL_ID pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE)
class NotificationsJobScheduler : JobService() {
private lateinit var mNotification: Notification
private lateinit var alarmIntent: PendingIntent
private lateinit var prefs: SharedPreferences
val gson = Gson()
private var notificationManager: NotificationManager? = null
lateinit var channel : NotificationChannel
private lateinit var notifList: MutableList<LocalNotification> //= mutableListOf(LocalNotification(0, Date(2017, 6, 11), "", ""))
companion object {
var started: Boolean = false
const val CHANNEL_ID = "com.google.MTsvetkov.CHANNEL_ID"
var notificationID = 101
const val CHANNEL_NAME = "Sample Notification"
const val CHANNEL_DISC = "Example News Channel"
}
override fun onStopJob(params: JobParameters?): Boolean {
return false
}
override fun onStartJob(params: JobParameters?): Boolean {
createNotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
CHANNEL_DISC)
sendNotification()
jobFinished(params, false)
return true
}
fun sendNotification() {
val intent = Intent(this, MainActivity::class.java)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
//Get Notification list from SharedPreferences
prefs = this.getSharedPreferences(ShowSingleRaceActivity().PREFS_FILENAME, 0)
val jsonPerf: String = prefs.getString(ShowSingleRaceActivity().NOTIFICATION_OBJECTS, "")
if (jsonPerf != "") notifList = gson.fromJson(jsonPerf, object : TypeToken<MutableList<LocalNotification>>() {}.type)
//Check the date
val today = Calendar.getInstance()
for (notif in notifList) {
if (today.equals(notif.notifDate)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.d("JSON", Build.VERSION.SDK_INT.toString())
mNotification = Notification.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.yelbell)
.setContentTitle(notif.raceName)
.setContentText(notif.message)
.build()
notificationID++
} else {
mNotification = Notification.Builder(this)
.setSmallIcon(R.drawable.yelbell)
.setAutoCancel(true)
.setContentTitle(notif.raceName)
.setContentText(notif.message)
.build()
}
notificationManager?.notify(notificationID, mNotification)
}
}
}
private fun createNotificationChannel(id: String, name: String,
description: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_LOW
channel = NotificationChannel(id, name, importance)
channel.description = description
channel.enableLights(true)
channel.lightColor = Color.RED
channel.enableVibration(true)
channel.vibrationPattern =
longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
notificationManager?.createNotificationChannel(channel)
}
}
}
it was necessary to move channel creation block into the cycle (where I created notification)
createNotificationChannel(CHANNEL_ID, CHANNEL_NAME,CHANNEL_DISC) --->
---->or (notif in notifList) {
if (today.equals(notif.notifDate)) {
**createNotificationChannel(CHANNEL_ID, CHANNEL_NAME,CHANNEL_DISC)**
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.d("JSON", Build.VERSION.SDK_INT.toString())
mNotification = Notification.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.yelbell)
.setContentTitle(notif.raceName)
.setContentText(notif.message)
.build()
notificationID++
} else {
mNotification = Notification.Builder(this)
.setSmallIcon(R.drawable.yelbell)
.setAutoCancel(true)
.setContentTitle(notif.raceName)
.setContentText(notif.message)
.build()
}
User contributions licensed under CC BY-SA 3.0