tauri-plugin-fcm
v0.1.9
Published
Tauri 2 plugin for Firebase Cloud Messaging (FCM) — iOS APNs→FCM token exchange + Android FCM
Maintainers
Readme
tauri-plugin-fcm
Firebase Cloud Messaging for Tauri 2 mobile apps.
The plugin returns FCM registration tokens on both iOS and Android. On iOS it handles the APNs to FCM exchange so your app uses one token type across both platforms.
| Platform | Supported | | --- | --- | | Android | Yes | | iOS | Yes | | macOS | No | | Windows | No | | Linux | No |
Install
Add the Rust crate to src-tauri/Cargo.toml:
[dependencies]
tauri-plugin-fcm = "0.1.0"Install the JavaScript guest bindings:
pnpm add tauri-plugin-fcm
# or
bun add tauri-plugin-fcm
# or
npm add tauri-plugin-fcm
# or
yarn add tauri-plugin-fcmRegister the plugin in your Tauri app:
fn main() {
let builder = tauri::Builder::default();
#[cfg(mobile)]
let builder = builder.plugin(tauri_plugin_fcm::init());
builder
.run(tauri::generate_context!())
.expect("error while running tauri application");
}Add the capability permission:
{
"permissions": [
"fcm:default"
]
}API
import {
checkPermissions,
createChannel,
deleteToken,
getToken,
onPushError,
onTokenRefresh,
register,
requestPermissions,
sendNotification,
} from "tauri-plugin-fcm";Functions
checkPermissions(): Promise<PermissionState>requestPermissions(): Promise<PermissionState>register(): Promise<void>getToken(): Promise<{ token: string }>deleteToken(): Promise<void>createChannel(options: CreateChannelOptions): Promise<void>sendNotification(options: SendNotificationOptions): Promise<void>onTokenRefresh(handler): Promise<PluginListener>onPushError(handler): Promise<PluginListener>
PermissionState comes from @tauri-apps/api/core and can be:
granteddeniedpromptprompt-with-rationale(Android only)
Types
CreateChannelOptions
interface CreateChannelOptions {
/** Unique identifier for the channel */
id: string;
/** Display name for the channel */
name: string;
/** Importance level for the channel (0-5) */
importance: number;
}SendNotificationOptions
interface SendNotificationOptions {
/** Notification title */
title: string;
/** Notification body text */
body?: string;
/** Icon identifier or URL */
icon?: string;
/** Notification ID */
id?: number;
/** Channel ID (Android) */
channelId?: string;
}Usage
Basic Setup
import {
checkPermissions,
getToken,
onPushError,
onTokenRefresh,
register,
requestPermissions,
} from "tauri-plugin-fcm";
let permission = await checkPermissions();
if (permission === "prompt" || permission === "prompt-with-rationale") {
permission = await requestPermissions();
}
if (permission === "granted") {
await register();
const { token } = await getToken();
console.log("FCM token:", token);
}
const tokenListener = await onTokenRefresh((event) => {
console.log("New FCM token:", event.token);
});
const errorListener = await onPushError((event) => {
console.error("Push error:", event.error);
});
// Later:
await tokenListener.unregister();
await errorListener.unregister();Sending Notifications
import { createChannel, sendNotification } from "tauri-plugin-fcm";
// Create a notification channel (Android only, no-op on iOS)
await createChannel({
id: "default",
name: "Default Notifications",
importance: 4,
});
// Send a notification
await sendNotification({
title: "Hello",
body: "This is a test notification",
channelId: "default",
});Platform setup
iOS
- Add
GoogleService-Info.plistto your generated iOS project undersrc-tauri/gen/apple/<app-name>_iOS/and make sure it is included in the iOS app target. - Enable the
aps-environmententitlement. - Enable Push Notifications and Background Modes with Remote notifications.
- If you disable
FirebaseAppDelegateProxyEnabled, make sure APNs callbacks still reach Firebase.
Android
- Add
google-services.jsontosrc-tauri/gen/android/app. - Apply the Google Services Gradle plugin in the Android app.
- Keep
POST_NOTIFICATIONSavailable for Android 13 and later.
Notes
- This is a mobile-only plugin. If you share Tauri setup code across desktop and mobile targets, gate registration with
#[cfg(mobile)]. - On Android,
register()is effectively a no-op because FCM registration is automatic. - On iOS simulators,
register()rejects with"Push notifications not available on simulator". Apush-errorevent also fires once at startup (fromload()) for listeners attached early. - On iOS, if APNs registration fails (missing entitlement, cert mismatch, no network), the error is buffered and surfaced by the next
getToken()call. Thepush-errorevent also fires for listeners. createChannel()is Android-only and is a no-op on iOS.- On Android API 26+, calling
sendNotification()beforecreateChannel()will silently drop the notification.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
