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..788740a42774 --- /dev/null +++ b/modules/notifications/godot_notifications.h @@ -0,0 +1,57 @@ +/**************************************************************************/ +/* 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); + +private: + String _apns_token; + +protected: + static void _bind_methods(); + + static GodotNotifications *singleton; + +public: + void request_notifications(); + + void set_apns_token(const String &p_token); + String get_apns_token() const; + + 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..1cd0625177a0 --- /dev/null +++ b/modules/notifications/godot_notifications.mm @@ -0,0 +1,83 @@ +/**************************************************************************/ +/* 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 + +#if TARGET_OS_IPHONE +#import +#endif +#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::set_apns_token(const String &p_token) { + _apns_token = p_token; + emit_signal(SNAME("apns_token_changed"), p_token); +} + +String GodotNotifications::get_apns_token() const { + return _apns_token; +} + +void GodotNotifications::_bind_methods() { + ClassDB::bind_method(D_METHOD("request_notifications"), &GodotNotifications::request_notifications); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "apns_token"), "set_apns_token", "get_apns_token"); + ADD_SIGNAL(MethodInfo("apns_token_changed", PropertyInfo(Variant::STRING, "token"))); + 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..4a8c9dde1d1a 100644 --- a/platform/ios/app_delegate.mm +++ b/platform/ios/app_delegate.mm @@ -131,6 +131,20 @@ - (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); + + Object *godot_notifications = Engine::get_singleton()->get_singleton_object("GodotNotifications"); + if (godot_notifications != nil) { + godot_notifications->call_deferred("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/os_ios.mm b/platform/ios/os_ios.mm index 590238be77f7..7319751b1077 100644 --- a/platform/ios/os_ios.mm +++ b/platform/ios/os_ios.mm @@ -646,5 +646,4 @@ void register_dynamic_symbol(char *name, void *address) { } } } - #endif // IOS_ENABLED