diff --git a/platform/ios/app_delegate.mm b/platform/ios/app_delegate.mm index 37d269643488..42a3fd32eaca 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 35b87ea64703..774a02ad71bc 100644 --- a/platform/ios/os_ios.mm +++ b/platform/ios/os_ios.mm @@ -651,4 +651,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