Skip to content

Add POST_NOTIFICATIONS permission check before NotificationManagerCompat.notify() #1

@driversti

Description

@driversti

Problem

SpeedMonitorService.kt:179 calls manager.notify(SpeedNotifications.NOTIFICATION_ID, notification) without first checking whether the runtime POST_NOTIFICATIONS permission is granted. On Android 13+ (API 33+), if the user revokes the permission while the service is running, the call throws SecurityException and crashes the service tick loop.

Detected by ./gradlew :app:lintDebug — one lint error in the project:

```
SpeedMonitorService.kt:179: Error: Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException [MissingPermission]
```

Suggested fix

Wrap the `manager.notify(...)` call in a `runCatching { ... }` (same pattern already used for `registerNetworkCallback` and `unregisterReceiver` in the same file) or pre-check with `NotificationManagerCompat.from(this).areNotificationsEnabled()` and skip the tick when false.

The `runCatching` option is the smaller change and keeps behaviour identical when permissions are granted.

Acceptance

  • `./gradlew :app:lintDebug` passes with zero errors.
  • Manually revoking `POST_NOTIFICATIONS` while the service is running does not crash the app — ticking silently pauses (or continues without updating the icon) until permission is restored.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions