我的Android应用中的通知(NotificationManager类)有问题。我做了频道和通知。我阅读了有关26 build的问题,并创建了一个通知通道,但这并不能解决我的问题。但是我在logcat中遇到了下一个错误。
E / NotificationService:找不到pkg = com.gmail.mtswetkov.ocrraces的频道, channelId = com.google.MTsvetkov.CHANNEL_ID,id = 103,标签=空, opPkg = com.gmail.mtswetkov.ocrraces,调用Uid = 10087,userId = 0, 入网用户ID = 0,notificationUid = 10087, notification = Notification(channel = com.google.MTsvetkov.CHANNEL_ID pri = 0 contentView = null振动= null声音= null默认值= 0x0标志= 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)
}
}
}
答案 0 :(得分:0)
有必要将通道创建块移入周期(我在其中创建通知)
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()
}