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 11, 2024
1 parent db66bd3 commit 39d8d01
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 1 deletion.
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"]])
57 changes: 57 additions & 0 deletions modules/notifications/godot_notifications.h
Original file line number Diff line number Diff line change
@@ -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
83 changes: 83 additions & 0 deletions modules/notifications/godot_notifications.mm
Original file line number Diff line number Diff line change
@@ -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 <TargetConditionals.h>

#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#endif
#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::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")));
}
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 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
14 changes: 14 additions & 0 deletions platform/ios/app_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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]]) {
Expand Down
1 change: 0 additions & 1 deletion platform/ios/os_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -646,5 +646,4 @@ void register_dynamic_symbol(char *name, void *address) {
}
}
}

#endif // IOS_ENABLED

0 comments on commit 39d8d01

Please sign in to comment.