fix(scheduled-posts): explicit POST_NOTIFICATIONS check before notify
Lint flagged the notify() call as MissingPermission because there was no visible check that POST_NOTIFICATIONS was granted. Gate the call behind areNotificationsEnabled() — matching the pattern used in EventNotificationConsumer — and catch SecurityException for the narrow window where permission could be revoked between the check and the notify().
This commit is contained in:
+10
-2
@@ -98,6 +98,11 @@ object ScheduledPostNotifier {
|
|||||||
tapIntent,
|
tapIntent,
|
||||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
||||||
)
|
)
|
||||||
|
val notificationManager = NotificationManagerCompat.from(context)
|
||||||
|
// POST_NOTIFICATIONS is runtime-granted on Android 13+; bail out
|
||||||
|
// explicitly so lint doesn't flag the notify() call and so a denied
|
||||||
|
// user doesn't see a misleading no-op log.
|
||||||
|
if (!notificationManager.areNotificationsEnabled()) return
|
||||||
val builder =
|
val builder =
|
||||||
NotificationCompat
|
NotificationCompat
|
||||||
.Builder(context, channelId)
|
.Builder(context, channelId)
|
||||||
@@ -110,8 +115,11 @@ object ScheduledPostNotifier {
|
|||||||
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
||||||
.setAutoCancel(true)
|
.setAutoCancel(true)
|
||||||
.setWhen(System.currentTimeMillis())
|
.setWhen(System.currentTimeMillis())
|
||||||
// Silently no-ops on Android 13+ if POST_NOTIFICATIONS isn't granted.
|
try {
|
||||||
NotificationManagerCompat.from(context).notify(notId, builder.build())
|
notificationManager.notify(notId, builder.build())
|
||||||
|
} catch (_: SecurityException) {
|
||||||
|
// Race: permission revoked between the check above and notify().
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ensureChannel(context: Context) {
|
private fun ensureChannel(context: Context) {
|
||||||
|
|||||||
Reference in New Issue
Block a user