正在进行的通知未取消

时间:2018-12-04 17:01:37

标签: android android-notifications android-jobscheduler

我们正在使用jobScheduler运行定期的天气网络通话,其结果将发布到正在进行的天气通知中。

这是我们在jobService中创建通知的方式:

private fun createNotification(selectedLocation: City) {
        val resultIntent = Intent(context, SplashActivity::class.java)
            resultIntent.putExtra(AppConstants.IS_ONGOING, true)
            resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)

            val resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0)
            val notificationView = getComplexNotificationView(selectedLocation) ?: return

            val notification = NotificationCompat.Builder(context, context.packageName)
                    .setPriority(NotificationCompat.PRIORITY_LOW)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setSmallIcon(getSmallIconResource(context,
                            if (settings.isFahrenheitEnabled())
                                selectedLocation.currentObservation!!.tempF!!
                            else
                                selectedLocation.currentObservation!!.tempC!!))
                    .setVibrate(null)
                    .setWhen(System.currentTimeMillis())
                    .setCustomContentView(notificationView)
                    .setContentIntent(resultPendingIntent)
                    .setOngoing(true)
                    .setAutoCancel(false)
                    .setGroup(AppConstants.NOTIFICATION_GROUP_ONGOING)
                    .build()

            NotificationManagerCompat.from(context).notify(ONGOING_NOTIFY_ID, notification)
    }

在我们应用的设置中,用户可以禁用正在进行的通知。这是我们试图取消它的方法:

val jobScheduler: JobScheduler? = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
jobScheduler?.cancel(OngoingNotificationJobService.ONGOING_JOB_ID)

NotificationManagerCompat.from(context).cancel(OngoingNotificationJobService.ONGOING_JOB_ID)

问题:

取消呼叫没有清除通知。我在做什么错了?

1 个答案:

答案 0 :(得分:2)

您必须“ NotificationManagerCompat.from(context).cancel()”而不是先前的ONGOING_NOTIFY_ID,而不是您的ONGOING_JOB_ID;)