We're using a jobScheduler to run a periodic weather network call whose results are posted to an ongoing weather notification.
Here's how we create the notification inside the 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)
}
In our app's settings, the user can disable the ongoing notification. Here's how we're trying to cancel it:
val jobScheduler: JobScheduler? = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
jobScheduler?.cancel(OngoingNotificationJobService.ONGOING_JOB_ID)
NotificationManagerCompat.from(context).cancel(OngoingNotificationJobService.ONGOING_JOB_ID)
The problem:
The cancel call is not clearing the notification. What am I doing wrong?
You have to "NotificationManagerCompat.from(context).cancel()" your previous ONGOING_NOTIFY_ID and not your ONGOING_JOB_ID ;)