From 2dc4833ffb9da3a2617293367502b416eaec2a02 Mon Sep 17 00:00:00 2001 From: Roman Vasilyev Date: Sat, 7 Sep 2024 10:11:24 -0700 Subject: [PATCH] apns token signal and variables --- modules/notifications/SCsub | 6 ++ modules/notifications/config.py | 9 +++ modules/notifications/godot_notifications.h | 51 ++++++++++++++ modules/notifications/godot_notifications.mm | 70 ++++++++++++++++++++ modules/notifications/register_types.cpp | 45 +++++++++++++ modules/notifications/register_types.h | 39 +++++++++++ platform/ios/app_delegate.mm | 11 +++ platform/ios/ios.h | 4 ++ platform/ios/ios.mm | 11 +++ platform/ios/os_ios.h | 2 + platform/ios/os_ios.mm | 4 ++ 11 files changed, 252 insertions(+) create mode 100644 modules/notifications/SCsub create mode 100644 modules/notifications/config.py create mode 100644 modules/notifications/godot_notifications.h create mode 100644 modules/notifications/godot_notifications.mm create mode 100644 modules/notifications/register_types.cpp create mode 100644 modules/notifications/register_types.h diff --git a/modules/notifications/SCsub b/modules/notifications/SCsub new file mode 100644 index 000000000000..f216028ffb07 --- /dev/null +++ b/modules/notifications/SCsub @@ -0,0 +1,6 @@ +Import("env") + +sources = ["register_types.cpp", "godot_notifications.mm"] + +if env["platform"] in ["ios", "macos"]: + env.add_source_files(env.modules_sources, sources) diff --git a/modules/notifications/config.py b/modules/notifications/config.py new file mode 100644 index 000000000000..5bf946f020ac --- /dev/null +++ b/modules/notifications/config.py @@ -0,0 +1,9 @@ +def can_build(env, platform): + return env["platform"] in ["ios", "macos"] + + +def configure(env): + if env["platform"] in ["ios", "macos"]: + env.Append(LINKFLAGS=["-ObjC"]) + env.Append(CPPPATH=["#core"]) + # env.Append(LINKFLAGS=["-lStoreKit.macos." + env["arch"]]) diff --git a/modules/notifications/godot_notifications.h b/modules/notifications/godot_notifications.h new file mode 100644 index 000000000000..b04c5c23bb19 --- /dev/null +++ b/modules/notifications/godot_notifications.h @@ -0,0 +1,51 @@ +/**************************************************************************/ +/* godot_notifications.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef GODOT_NOTIFICATIONS_H +#define GODOT_NOTIFICATIONS_H + +#include "core/object/ref_counted.h" + +class GodotNotifications : public RefCounted { + GDCLASS(GodotNotifications, RefCounted); + +protected: + static void _bind_methods(); + + static GodotNotifications *singleton; + +public: + void request_notifications(); + + GodotNotifications(); + ~GodotNotifications(); +}; + +#endif // GODOT_NOTIFICATIONS_H diff --git a/modules/notifications/godot_notifications.mm b/modules/notifications/godot_notifications.mm new file mode 100644 index 000000000000..44cbba2a1a9c --- /dev/null +++ b/modules/notifications/godot_notifications.mm @@ -0,0 +1,70 @@ +/**************************************************************************/ +/* godot_notifications.mm */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "godot_notifications.h" + +#include + +#import +#import + +GodotNotifications *GodotNotifications::singleton = nullptr; + +GodotNotifications::GodotNotifications() { + ERR_FAIL_COND(singleton != nullptr); + singleton = this; +} + +GodotNotifications::~GodotNotifications() { + singleton = nullptr; +} + +void GodotNotifications::request_notifications() { +#if TARGET_OS_IPHONE + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) + completionHandler:^(BOOL granted, NSError *_Nullable error) { + if (granted) { + [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *_Nonnull settings) { + if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) { + [[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification. + } + }]; + } + }]; +#elif TARGET_OS_OSX +#endif +} + +void GodotNotifications::_bind_methods() { + ClassDB::bind_method(D_METHOD("request_notifications"), &GodotNotifications::request_notifications); + ADD_SIGNAL(MethodInfo("rate_success")); + ADD_SIGNAL(MethodInfo("rate_failed", PropertyInfo(Variant::STRING, "message"))); +} diff --git a/modules/notifications/register_types.cpp b/modules/notifications/register_types.cpp new file mode 100644 index 000000000000..d63d4d4ff4c0 --- /dev/null +++ b/modules/notifications/register_types.cpp @@ -0,0 +1,45 @@ +/**************************************************************************/ +/* register_types.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "register_types.h" + +#include "core/config/engine.h" + +#include "godot_notifications.h" + +void initialize_notifications_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } + Engine::get_singleton()->add_singleton(Engine::Singleton("GodotNotifications", memnew(GodotNotifications))); +} + +void uninitialize_notifications_module(ModuleInitializationLevel p_level) { +} diff --git a/modules/notifications/register_types.h b/modules/notifications/register_types.h new file mode 100644 index 000000000000..1d05795086ca --- /dev/null +++ b/modules/notifications/register_types.h @@ -0,0 +1,39 @@ +/**************************************************************************/ +/* register_types.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef NOTIFICATIONS_REGISTER_TYPES_H +#define NOTIFICATIONS_REGISTER_TYPES_H + +#include "modules/register_module_types.h" + +void initialize_notifications_module(ModuleInitializationLevel p_level); +void uninitialize_notifications_module(ModuleInitializationLevel p_level); + +#endif // NOTIFICATIONS_REGISTER_TYPES_H diff --git a/platform/ios/app_delegate.mm b/platform/ios/app_delegate.mm index 37d269643488..7f9e67fe053c 100644 --- a/platform/ios/app_delegate.mm +++ b/platform/ios/app_delegate.mm @@ -131,6 +131,17 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( return YES; } +- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { + const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes]; + NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x", + ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]), + ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]), + ntohl(tokenBytes[6]), ntohl(tokenBytes[7])]; + String apnsToken = String(hexToken.UTF8String); + + OS_IOS::get_singleton()->set_apns_token(apnsToken); +} + - (void)onAudioInterruption:(NSNotification *)notification { if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) { if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) { diff --git a/platform/ios/ios.h b/platform/ios/ios.h index cb5be64cee81..c5813eab0a52 100644 --- a/platform/ios/ios.h +++ b/platform/ios/ios.h @@ -41,6 +41,7 @@ class iOS : public Object { static void _bind_methods(); private: + String _apns_token; CHHapticEngine *haptic_engine API_AVAILABLE(ios(13)) = nullptr; CHHapticEngine *get_haptic_engine_instance() API_AVAILABLE(ios(13)); @@ -56,6 +57,9 @@ class iOS : public Object { String get_model() const; String get_rate_url(int p_app_id) const; + void set_apns_token(const String &p_token); + String get_apns_token() const; + iOS(); }; diff --git a/platform/ios/ios.mm b/platform/ios/ios.mm index 6943de5ac889..7b0e197f3fe6 100644 --- a/platform/ios/ios.mm +++ b/platform/ios/ios.mm @@ -42,6 +42,8 @@ ClassDB::bind_method(D_METHOD("supports_haptic_engine"), &iOS::supports_haptic_engine); ClassDB::bind_method(D_METHOD("start_haptic_engine"), &iOS::start_haptic_engine); ClassDB::bind_method(D_METHOD("stop_haptic_engine"), &iOS::stop_haptic_engine); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "apns_token"), "set_apns_token", "get_apns_token"); + ADD_SIGNAL(MethodInfo("apns_token_changed", PropertyInfo(Variant::STRING, "token"))); }; bool iOS::supports_haptic_engine() { @@ -197,4 +199,13 @@ return ret; } +void iOS::set_apns_token(const String &p_token) { + _apns_token = p_token; + emit_signal(SNAME("apns_token_changed"), p_token); +} + +String iOS::get_apns_token() const { + return _apns_token; +} + iOS::iOS() {} diff --git a/platform/ios/os_ios.h b/platform/ios/os_ios.h index b7c5a7306560..614dc66b3ef3 100644 --- a/platform/ios/os_ios.h +++ b/platform/ios/os_ios.h @@ -127,6 +127,8 @@ class OS_IOS : public OS_Unix { virtual bool _check_internal_feature_support(const String &p_feature) override; + void set_apns_token(const String &p_token); + void on_focus_out(); void on_focus_in(); diff --git a/platform/ios/os_ios.mm b/platform/ios/os_ios.mm index 590238be77f7..d44b046a2e2f 100644 --- a/platform/ios/os_ios.mm +++ b/platform/ios/os_ios.mm @@ -647,4 +647,8 @@ void register_dynamic_symbol(char *name, void *address) { } } +void OS_IOS::set_apns_token(const String &p_token) { + if (ios) + ios->set_apns_token(p_token); +} #endif // IOS_ENABLED