-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
feat(push): push notifications #1015
Draft
sgammon
wants to merge
5
commits into
tauri-apps:dev
Choose a base branch
from
sgammon:feat/apns
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+273
−26
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
use std::path::PathBuf; | ||
use std::time::Instant; | ||
|
||
use crate::push::PushToken; | ||
use crate::{ | ||
dpi::{PhysicalPosition, PhysicalSize}, | ||
keyboard::{self, ModifiersState}, | ||
|
@@ -143,6 +144,17 @@ pub enum Event<'a, T: 'static> { | |
/// - **Other**: Unsupported. | ||
#[non_exhaustive] | ||
Reopen { has_visible_windows: bool }, | ||
|
||
/// ## Push Tokens | ||
/// | ||
/// Emitted when registration completes and an application push token is made available; on Apple | ||
/// platforms, this is the APNS token. On Android, this is the FCM token. | ||
PushRegistration(PushToken), | ||
|
||
/// ## Push Token Errors | ||
/// | ||
/// Emitted when push token registration fails. | ||
PushRegistrationError(String), | ||
} | ||
|
||
impl<T: Clone> Clone for Event<'static, T> { | ||
|
@@ -171,6 +183,8 @@ impl<T: Clone> Clone for Event<'static, T> { | |
} => Reopen { | ||
has_visible_windows: *has_visible_windows, | ||
}, | ||
PushRegistration(token) => PushRegistration(token.clone()), | ||
PushRegistrationError(error) => PushRegistrationError(error.clone()), | ||
} | ||
} | ||
} | ||
|
@@ -195,6 +209,8 @@ impl<'a, T> Event<'a, T> { | |
} => Ok(Reopen { | ||
has_visible_windows, | ||
}), | ||
PushRegistration(token) => Ok(PushRegistration(token)), | ||
PushRegistrationError(error) => Err(PushRegistrationError(error)), | ||
} | ||
} | ||
|
||
|
@@ -221,6 +237,8 @@ impl<'a, T> Event<'a, T> { | |
} => Some(Reopen { | ||
has_visible_windows, | ||
}), | ||
PushRegistration(token) => Some(PushRegistration(token)), | ||
PushRegistrationError(error) => Some(PushRegistrationError(error)), | ||
Comment on lines
+240
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these need guards as well |
||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,6 +180,7 @@ mod icon; | |
pub mod keyboard; | ||
pub mod monitor; | ||
mod platform_impl; | ||
pub mod push; | ||
|
||
pub mod window; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,11 @@ use cocoa::{ | |
foundation::NSString, | ||
}; | ||
use objc::{ | ||
class, | ||
declare::ClassDecl, | ||
msg_send, | ||
runtime::{Class, Object, Sel, BOOL}, | ||
sel, sel_impl, | ||
}; | ||
use std::{ | ||
cell::{RefCell, RefMut}, | ||
|
@@ -63,6 +66,14 @@ lazy_static! { | |
sel!(applicationSupportsSecureRestorableState:), | ||
application_supports_secure_restorable_state as extern "C" fn(&Object, Sel, id) -> BOOL, | ||
); | ||
decl.add_method( | ||
sel!(application:didRegisterForRemoteNotificationsWithDeviceToken:), | ||
did_register_for_apns as extern "C" fn(&Object, Sel, id, id), | ||
); | ||
decl.add_method( | ||
sel!(application:didFailToRegisterForRemoteNotificationsWithError:), | ||
did_fail_to_register_for_apns as extern "C" fn(&Object, _: Sel, id, id), | ||
); | ||
Comment on lines
+69
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. guards |
||
decl.add_ivar::<*mut c_void>(AUX_DELEGATE_STATE_NAME); | ||
|
||
AppDelegateClass(decl.register()) | ||
|
@@ -100,8 +111,26 @@ extern "C" fn dealloc(this: &Object, _: Sel) { | |
} | ||
} | ||
|
||
#[cfg(feature = "push-notifications")] | ||
fn register_push_notifications() { | ||
// register for push notifications. this call is inert on macOS unless the app is entitled to | ||
// access an APS environment. see: | ||
// https://developer.apple.com/documentation/usernotifications/registering-your-app-with-apns | ||
// and: | ||
// https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_aps-environment | ||
let shared_app = get_shared_application(); | ||
unsafe { | ||
// registerForRemoteNotifications() | ||
let _: () = msg_send![shared_app, registerForRemoteNotifications]; | ||
}; | ||
} | ||
|
||
extern "C" fn did_finish_launching(this: &Object, _: Sel, _: id) { | ||
trace!("Triggered `applicationDidFinishLaunching`"); | ||
|
||
#[cfg(feature = "push-notifications")] | ||
register_push_notifications(); | ||
|
||
AppState::launched(this); | ||
trace!("Completed `applicationDidFinishLaunching`"); | ||
} | ||
|
@@ -147,3 +176,62 @@ extern "C" fn application_supports_secure_restorable_state(_: &Object, _: Sel, _ | |
trace!("Completed `applicationSupportsSecureRestorableState`"); | ||
objc::runtime::YES | ||
} | ||
|
||
// application(_:didRegisterForRemoteNotificationsWithDeviceToken:) | ||
extern "C" fn did_register_for_apns(_: &Object, _: Sel, _: id, token_data: id) { | ||
trace!("Triggered `didRegisterForRemoteNotificationsWithDeviceToken`"); | ||
let token_bytes = unsafe { | ||
if token_data.is_null() { | ||
trace!("Token data is null; ignoring"); | ||
return; | ||
} | ||
let is_nsdata: bool = msg_send![token_data, isKindOfClass:class!(NSData)]; | ||
if !is_nsdata { | ||
trace!("Token data is not an NSData object"); | ||
return; | ||
} | ||
let bytes: *const u8 = msg_send![token_data, bytes]; | ||
let length: usize = msg_send![token_data, length]; | ||
std::slice::from_raw_parts(bytes, length).to_vec() | ||
}; | ||
AppState::did_register_push_token(token_bytes); | ||
trace!("Completed `didRegisterForRemoteNotificationsWithDeviceToken`"); | ||
} | ||
|
||
// application(_:didFailToRegisterForRemoteNotificationsWithError:) | ||
extern "C" fn did_fail_to_register_for_apns(_: &Object, _: Sel, _: id, err: *mut Object) { | ||
trace!("Triggered `didFailToRegisterForRemoteNotificationsWithError`"); | ||
|
||
let error_string = unsafe { | ||
if err.is_null() { | ||
"Unknown error (null error object)".to_string() | ||
} else { | ||
// Verify it's an NSError | ||
let is_error: bool = msg_send![err, isKindOfClass:class!(NSError)]; | ||
if !is_error { | ||
trace!("Invalid error object type for push registration failure"); | ||
return; | ||
} | ||
|
||
// Get the localizedDescription | ||
let description: *mut Object = msg_send![err, localizedDescription]; | ||
if description.is_null() { | ||
trace!("Error had no description"); | ||
return; | ||
} | ||
|
||
// Convert NSString to str | ||
let utf8: *const u8 = msg_send![description, UTF8String]; | ||
let len: usize = msg_send![description, lengthOfBytesUsingEncoding:4]; | ||
let bytes = std::slice::from_raw_parts(utf8, len); | ||
String::from_utf8_lossy(bytes).to_string() | ||
} | ||
}; | ||
|
||
AppState::did_fail_to_register_push_token(error_string); | ||
trace!("Completed `didFailToRegisterForRemoteNotificationsWithError`"); | ||
} | ||
|
||
fn get_shared_application() -> *mut Object { | ||
unsafe { msg_send![class!(NSApplication), sharedApplication] } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs guard
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure what you mean by "needs guard"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amrbashir these are my own comments to remind myself to add
#[cfg(feature = "push-notifications")
so that this code isn't included unless developers opt-in.