lingxia 0.5.1

LingXia - Cross-platform LxApp (lightweight application) framework for Android, iOS, and HarmonyOS
//! Unified push notification APIs for platform FFI layers.

use lingxia_provider::ProviderError;
use lxapp::provider;

fn normalize_token(token: String) -> Result<String, ProviderError> {
    let token = token.trim().to_string();
    if token.is_empty() {
        return Err(ProviderError::invalid_request("push token is empty"));
    }
    Ok(token)
}

/// Bind push token through the registered provider.
pub async fn bind_push_token(token: String) -> Result<(), ProviderError> {
    let token = normalize_token(token)?;
    provider::bind_push_token(token).await
}

/// FFI-friendly push token entrypoint.
///
/// Returns:
/// - `0` when task queued successfully
/// - `1` when validation failed
#[cfg_attr(
    not(any(target_os = "ios", target_os = "macos", target_env = "ohos")),
    allow(dead_code)
)]
pub(crate) fn bind_push_token_for_ffi(token: String) -> i32 {
    let token = match normalize_token(token) {
        Ok(token) => token,
        Err(err) => {
            log::warn!("[Push] Invalid token: {}", err);
            return 1;
        }
    };

    let _ = rong::RongExecutor::global().spawn(async move {
        if let Err(err) = provider::bind_push_token(token).await {
            log::warn!("[Push] bind_push_token failed: {}", err);
        }
    });
    0
}