-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathnotifications.rs
79 lines (72 loc) · 2.55 KB
/
notifications.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use tauri::Manager;
use serde::{ Serialize, Deserialize };
#[cfg(not(target_os = "macos"))]
static mut APP_IDENTIFIER: Option<String> = Option::None;
#[derive(Clone, Serialize)]
struct Payload {
url: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NotificationPayload {
body: String,
callback: String,
}
pub struct Instance {}
impl Instance {
pub fn init(app_identifier: &str) {
#[cfg(target_os = "macos")]
mac_notification_sys::set_application(&app_identifier).unwrap();
#[cfg(not(target_os = "macos"))]
unsafe {
APP_IDENTIFIER = Some(app_identifier.to_string());
}
}
pub fn notification(app_handle: tauri::AppHandle, payload: NotificationPayload) {
#[cfg(target_os = "macos")]
let result = mac_notification_sys::send_notification(
"Linen",
None,
&payload.body,
Some(
mac_notification_sys::Notification
::new()
.main_button(mac_notification_sys::MainButton::SingleAction("Open"))
.close_button("Close")
)
);
#[cfg(not(target_os = "macos"))]
let result = unsafe {
tauri::api::notification::Notification
::new(APP_IDENTIFIER.as_ref().unwrap())
.title("Linen")
.body(payload.body)
.show()
};
match result {
Ok(response) => {
match response {
mac_notification_sys::NotificationResponse::ActionButton(action_name) => {
if action_name == "Open" {
println!("Clicked on open");
app_handle
.emit_all("scheme-request-received", Payload {
url: payload.callback.into(),
})
.unwrap();
}
}
mac_notification_sys::NotificationResponse::Click => {
println!("Clicked on the notification itself");
app_handle
.emit_all("scheme-request-received", Payload {
url: payload.callback.into(),
})
.unwrap();
}
_ => {}
}
}
Err(e) => println!("Could not show notification: {}", e),
}
}
}