From 7bd28266f908f1e340c0dd456125cbe1b91fddb9 Mon Sep 17 00:00:00 2001 From: Fabian Date: Sat, 12 Sep 2026 01:57:34 +0100 Subject: [PATCH] fix: support notification builder and permission check on android 8 and below --- android_notify/core.py | 8 +++++++- android_notify/internal/permissions.py | 8 ++++++++ android_notify/sword.py | 8 +++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/android_notify/core.py b/android_notify/core.py index 29b287d..ac4d444 100644 --- a/android_notify/core.py +++ b/android_notify/core.py @@ -126,7 +126,13 @@ def send_notification( notification_manager.createNotificationChannel(channel) # Build the notification - builder = NotificationCompatBuilder(context, channel_id) + if BuildVersion.SDK_INT >= 26: + builder = NotificationCompatBuilder(context, channel_id) + else: + # Before Android 8.0 the platform Notification$Builder only has the + # (Context) constructor; the (Context, String channelId) overload + # requires API 26+. The androidx Builder accepts both forms on all APIs. + builder = NotificationCompatBuilder(context) builder.setContentTitle(title) builder.setContentText(message) insert_app_icon(builder, custom_app_icon_path) diff --git a/android_notify/internal/permissions.py b/android_notify/internal/permissions.py index 3e3c74f..be85651 100644 --- a/android_notify/internal/permissions.py +++ b/android_notify/internal/permissions.py @@ -13,11 +13,19 @@ def check_notification_permission_legacy_android12_below(): # Below Android 13 there is no POST_NOTIFICATIONS runtime permission # so check the per-app notification enabled state instead. + if BuildVersion.SDK_INT < 24: + # NotificationManager.areNotificationsEnabled() only exists on API 24+ + # (before that there is no per-app notification toggle -> always on). + return True context = get_python_activity_context() notification_service = context.getSystemService(Context.NOTIFICATION_SERVICE) return notification_service.areNotificationsEnabled() def check_notification_permission_androidx_android12_below(): + if BuildVersion.SDK_INT < 24: + # NotificationManagerCompat.areNotificationsEnabled() delegates to the + # platform API-24+ method; before that notifications are always enabled. + return True context = get_python_activity_context() func_from = getattr(NotificationManagerCompat, "from") compat_manager = func_from(context) diff --git a/android_notify/sword.py b/android_notify/sword.py index 7852615..2957813 100644 --- a/android_notify/sword.py +++ b/android_notify/sword.py @@ -109,7 +109,13 @@ def __init__(self, **kwargs): # @dataclass already does work self.notification_manager = get_notification_manager() context = get_python_activity_context() - self.builder = NotificationCompatBuilder(context, self.channel_id) + if BuildVersion.SDK_INT >= 26: + self.builder = NotificationCompatBuilder(context, self.channel_id) + else: + # Before Android 8.0 the platform Notification$Builder only has the + # (Context) constructor; the (Context, String channelId) overload + # requires API 26+. + self.builder = NotificationCompatBuilder(context) def setData(self, data_object:dict): """