Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add remote notification registration app delegates #3650

Draft
wants to merge 1 commit into
base: v0.29.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ features = [
"dispatch",
"Foundation",
"Foundation_NSArray",
"Foundation_NSData",
"Foundation_NSError",
"Foundation_NSString",
"Foundation_NSProcessInfo",
"Foundation_NSThread",
Expand Down
14 changes: 14 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ use crate::{
window::{ActivationToken, Theme, WindowId},
};

/// iOS specific event sent when UIApplication delegate receives the response upon registering for remote notifications
#[derive(Debug, Clone, PartialEq)]
pub enum IosRemoteRegistration {
Failed {
code: isize,
localized_description: String,
},
DeviceToken(Vec<u8>),
}

/// Describes a generic event.
///
/// See the module-level docs for more information on the event loop manages each event.
Expand Down Expand Up @@ -251,6 +261,9 @@ pub enum Event<T: 'static> {
///
/// - **macOS / Wayland / Windows / Orbital:** Unsupported.
MemoryWarning,

/// TODO
IosRemoteRegistration(IosRemoteRegistration),
}

impl<T> Event<T> {
Expand All @@ -267,6 +280,7 @@ impl<T> Event<T> {
Suspended => Ok(Suspended),
Resumed => Ok(Resumed),
MemoryWarning => Ok(MemoryWarning),
IosRemoteRegistration(v) => Ok(IosRemoteRegistration(v)),
}
}
}
Expand Down
51 changes: 49 additions & 2 deletions src/platform_impl/ios/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use std::cell::Cell;
use std::ptr::NonNull;

use icrate::Foundation::{CGFloat, CGRect, MainThreadMarker, NSObject, NSObjectProtocol, NSSet};
use icrate::Foundation::{
CGFloat, CGRect, MainThreadMarker, NSData, NSError, NSObject, NSObjectProtocol, NSSet,
};
use objc2::declare::{Ivar, IvarDrop};
use objc2::rc::Id;
use objc2::runtime::AnyClass;
Expand All @@ -15,9 +17,13 @@ use super::uikit::{
UIViewController, UIWindow,
};
use super::window::WindowId;

use crate::{
dpi::PhysicalPosition,
event::{DeviceId as RootDeviceId, Event, Force, Touch, TouchPhase, WindowEvent},
event::{
DeviceId as RootDeviceId, Event, Force, IosRemoteRegistration, Touch, TouchPhase,
WindowEvent,
},
platform::ios::ValidOrientations,
platform_impl::platform::{
ffi::{UIRectEdge, UIUserInterfaceIdiom},
Expand Down Expand Up @@ -526,6 +532,47 @@ declare_class!(
true
}

#[method(application:didRegisterForRemoteNotificationsWithDeviceToken:)]
fn did_register_for_remote_notifications_with_device_token(
&self,
_application: &UIApplication,
token_data: *mut NSData,
) {
let slice: &[u8] = unsafe { token_data.as_ref() }.unwrap().bytes();

let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_event(
mtm,
EventWrapper::StaticEvent(Event::IosRemoteRegistration(
IosRemoteRegistration::DeviceToken(slice.to_vec()),
)),
)
}

#[method(application:didFailToRegisterForRemoteNotificationsWithError:)]
fn did_fail_to_register_for_remote_notifications(
&self,
_application: &UIApplication,
error: *mut NSError,
) {
let code = unsafe { error.as_ref() }.unwrap().code();
let localized_description: String = unsafe { error.as_ref() }
.unwrap()
.localizedDescription()
.to_string();

let mtm = MainThreadMarker::new().unwrap();
app_state::handle_nonuser_event(
mtm,
EventWrapper::StaticEvent(Event::IosRemoteRegistration(
IosRemoteRegistration::Failed {
code,
localized_description,
},
)),
)
}

#[method(applicationDidBecomeActive:)]
fn did_become_active(&self, _application: &UIApplication) {
let mtm = MainThreadMarker::new().unwrap();
Expand Down