1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

PushNotifications: Update Android implementation to properly request permissions

This commit is contained in:
reuk 2024-11-25 13:31:51 +00:00
parent ed0092a8bc
commit 6d10eb536f
No known key found for this signature in database

View file

@ -282,6 +282,9 @@ struct PushNotifications::Pimpl
bool areNotificationsEnabled() const bool areNotificationsEnabled() const
{ {
if (getAndroidSDKVersion() >= 33 && ! RuntimePermissions::isGranted (RuntimePermissions::postNotification))
return false;
if (getAndroidSDKVersion() >= 24) if (getAndroidSDKVersion() >= 24)
{ {
auto* env = getEnv(); 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) void sendLocalNotification (const Notification& n)
{ {
// All required fields have to be setup! // All required fields have to be setup!
@ -651,6 +676,8 @@ struct PushNotifications::Pimpl
return LocalRef<jobject> (env->NewObject (builderClass, builderConstructor, context.get())); return LocalRef<jobject> (env->NewObject (builderClass, builderConstructor, context.get()));
} }
static constexpr auto FLAG_IMMUTABLE = 0x04000000;
static LocalRef<jobject> setupRequiredFields (const Notification& n, LocalRef<jobject> notificationBuilder) static LocalRef<jobject> setupRequiredFields (const Notification& n, LocalRef<jobject> notificationBuilder)
{ {
if (notificationBuilder == nullptr) if (notificationBuilder == nullptr)
@ -675,7 +702,7 @@ struct PushNotifications::Pimpl
context.get(), context.get(),
1002, 1002,
notifyIntent.get(), notifyIntent.get(),
0)); FLAG_IMMUTABLE));
env->CallObjectMethod (notificationBuilder, NotificationBuilderBase.setContentTitle, javaString (n.title).get()); env->CallObjectMethod (notificationBuilder, NotificationBuilderBase.setContentTitle, javaString (n.title).get());
env->CallObjectMethod (notificationBuilder, NotificationBuilderBase.setContentText, javaString (n.body).get()); env->CallObjectMethod (notificationBuilder, NotificationBuilderBase.setContentText, javaString (n.body).get());
@ -920,7 +947,7 @@ struct PushNotifications::Pimpl
context.get(), context.get(),
1002, 1002,
deleteIntent.get(), deleteIntent.get(),
0)); FLAG_IMMUTABLE));
env->CallObjectMethod (notificationBuilder, NotificationBuilderBase.setDeleteIntent, deletePendingIntent.get()); env->CallObjectMethod (notificationBuilder, NotificationBuilderBase.setDeleteIntent, deletePendingIntent.get());
@ -956,7 +983,7 @@ struct PushNotifications::Pimpl
context.get(), context.get(),
1002, 1002,
notifyIntent.get(), notifyIntent.get(),
0)); FLAG_IMMUTABLE));
auto resources = LocalRef<jobject> (env->CallObjectMethod (context.get(), AndroidContext.getResources)); auto resources = LocalRef<jobject> (env->CallObjectMethod (context.get(), AndroidContext.getResources));
int iconId = env->CallIntMethod (resources, AndroidResources.getIdentifier, javaString (action.icon).get(), int iconId = env->CallIntMethod (resources, AndroidResources.getIdentifier, javaString (action.icon).get(),