Skip to content

Commit

Permalink
apns token signal and variable
Browse files Browse the repository at this point in the history
  • Loading branch information
theromis committed Oct 10, 2024
1 parent db66bd3 commit 54ba391
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/notifications/SCsub
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions modules/notifications/config.py
Original file line number Diff line number Diff line change
@@ -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"]])
51 changes: 51 additions & 0 deletions modules/notifications/godot_notifications.h
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions modules/notifications/godot_notifications.mm
Original file line number Diff line number Diff line change
@@ -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 <TargetConditionals.h>

#import <UIKit/UIKit.h>
#import <UserNotifications/UserNotifications.h>

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")));
}
45 changes: 45 additions & 0 deletions modules/notifications/register_types.cpp
Original file line number Diff line number Diff line change
@@ -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) {
}
39 changes: 39 additions & 0 deletions modules/notifications/register_types.h
Original file line number Diff line number Diff line change
@@ -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 SHARE_REGISTER_TYPES_H
#define SHARE_REGISTER_TYPES_H

#include "modules/register_module_types.h"

void initialize_notifications_module(ModuleInitializationLevel p_level);
void uninitialize_notifications_module(ModuleInitializationLevel p_level);

#endif // SHARE_REGISTER_TYPES_H
11 changes: 11 additions & 0 deletions platform/ios/app_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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]]) {
Expand Down
4 changes: 4 additions & 0 deletions platform/ios/ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();
};

Expand Down
11 changes: 11 additions & 0 deletions platform/ios/ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {}
2 changes: 2 additions & 0 deletions platform/ios/os_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions platform/ios/os_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 54ba391

Please sign in to comment.