繁体   English   中英

android app 26 api中的通知

[英]Notifications in android app 26 api

我的Android应用程序中的通知(NotificationManager类)有问题。 我做了频道和通知。 我阅读了有关26 build的问题,并创建了一个通知通道,但这并不能解决我的问题。 但是我在logcat中遇到了下一个错误。

E / NotificationService:未找到pkg = com.gmail.mtswetkov.ocrraces,channelId = com.google.MTsvetkov.CHANNEL_ID,id = 103,tag = null,opPkg = com.gmail.mtswetkov.ocrraces,调用Uid = 10087, userId = 0,incomingUserId = 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)
        }
    }
}

有必要将渠道创建模块移入周期(我在其中创建通知)

    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()
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM