From 6d10eb536fc10da8981b25aa868c1714a6431453 Mon Sep 17 00:00:00 2001 From: reuk Date: Mon, 25 Nov 2024 13:31:51 +0000 Subject: [PATCH] PushNotifications: Update Android implementation to properly request permissions --- .../native/juce_PushNotifications_android.cpp | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/modules/juce_gui_extra/native/juce_PushNotifications_android.cpp b/modules/juce_gui_extra/native/juce_PushNotifications_android.cpp index fcbefabd1d..46c50123c8 100644 --- a/modules/juce_gui_extra/native/juce_PushNotifications_android.cpp +++ b/modules/juce_gui_extra/native/juce_PushNotifications_android.cpp @@ -282,6 +282,9 @@ struct PushNotifications::Pimpl bool areNotificationsEnabled() const { + if (getAndroidSDKVersion() >= 33 && ! RuntimePermissions::isGranted (RuntimePermissions::postNotification)) + return false; + if (getAndroidSDKVersion() >= 24) { auto* env = getEnv(); @@ -296,6 +299,28 @@ struct PushNotifications::Pimpl } //============================================================================== + void requestPermissionsWithSettings (const Settings&) + { + RuntimePermissions::request (RuntimePermissions::postNotification, [&] (bool) + { + const auto notifyListeners = [] + { + if (auto* instance = PushNotifications::getInstance()) + instance->listeners.call ([] (Listener& l) { l.notificationSettingsReceived ({}); }); + }; + + if (MessageManager::getInstance()->isThisTheMessageThread()) + notifyListeners(); + else + MessageManager::callAsync (notifyListeners); + }); + } + + void requestSettingsUsed() + { + owner.listeners.call ([] (Listener& l) { l.notificationSettingsReceived ({}); }); + } + void sendLocalNotification (const Notification& n) { // All required fields have to be setup! @@ -651,6 +676,8 @@ struct PushNotifications::Pimpl return LocalRef (env->NewObject (builderClass, builderConstructor, context.get())); } + static constexpr auto FLAG_IMMUTABLE = 0x04000000; + static LocalRef setupRequiredFields (const Notification& n, LocalRef notificationBuilder) { if (notificationBuilder == nullptr) @@ -675,7 +702,7 @@ struct PushNotifications::Pimpl context.get(), 1002, notifyIntent.get(), - 0)); + FLAG_IMMUTABLE)); env->CallObjectMethod (notificationBuilder, NotificationBuilderBase.setContentTitle, javaString (n.title).get()); env->CallObjectMethod (notificationBuilder, NotificationBuilderBase.setContentText, javaString (n.body).get()); @@ -920,7 +947,7 @@ struct PushNotifications::Pimpl context.get(), 1002, deleteIntent.get(), - 0)); + FLAG_IMMUTABLE)); env->CallObjectMethod (notificationBuilder, NotificationBuilderBase.setDeleteIntent, deletePendingIntent.get()); @@ -956,7 +983,7 @@ struct PushNotifications::Pimpl context.get(), 1002, notifyIntent.get(), - 0)); + FLAG_IMMUTABLE)); auto resources = LocalRef (env->CallObjectMethod (context.get(), AndroidContext.getResources)); int iconId = env->CallIntMethod (resources, AndroidResources.getIdentifier, javaString (action.icon).get(),