use base64::{engine::general_purpose, Engine as _}; use serde::de::DeserializeOwned; use std::sync::Mutex; use tauri::{ plugin::{PluginApi, PluginHandle}, AppHandle, Runtime, State, }; use crate::models::*; use crate::PushTokenState; #[cfg(target_os = "ios")] tauri::ios_plugin_binding!(init_plugin_push_notifications); // initializes the Kotlin or Swift plugin classes pub fn init( _app: &AppHandle, api: PluginApi, ) -> crate::Result> { #[cfg(target_os = "android")] let handle = api.register_android_plugin("", "PushNotificationsPlugin")?; #[cfg(target_os = "ios")] let handle = api.register_ios_plugin(init_plugin_push_notifications)?; Ok(PushNotifications(handle)) } /// Access to the push-notifications APIs. pub struct PushNotifications(PluginHandle); impl PushNotifications { pub fn get_push_token( &self, state: State>, _payload: PushTokenRequest, ) -> crate::Result { let state = state.lock().unwrap(); match &state.token { Some(token) => { let encoded = general_purpose::STANDARD.encode(&token); Ok(PushTokenResponse { value: Some(encoded.clone()), }) } None => Ok(PushTokenResponse { value: None }), } } }