A comprehensive Firebase Cloud Messaging (FCM) plugin for Tauri applications, providing push notification capabilities for both Android and iOS platforms.
- 🔥 Firebase Cloud Messaging Integration: Full FCM support for Android and iOS
- 📱 Cross-Platform: Works on both Android and iOS
- 🎯 Topic Management: Subscribe and unsubscribe from FCM topics
- 🔐 Permission Handling: Built-in notification permission management
- 📨 Message Handling: Receive and process push notifications
- 🎧 Event System: Real-time events for token changes, messages, and errors
- 🛡️ Type Safety: Full TypeScript support with comprehensive type definitions
Add the plugin to your Cargo.toml:
[dependencies]
tauri-plugin-fcm = { path = "../tauri-plugin-fcm" }In your src-tauri/src/main.rs:
use tauri_plugin_fcm::Fcm;
fn main() {
tauri::Builder::default()
.plugin(Fcm::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}- Add the Google Services plugin to your
android/build.gradle:
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}
}- Apply the plugin in your
android/app/build.gradle:
apply plugin: 'com.google.gms.google-services'- Add Firebase dependencies:
dependencies {
implementation platform('com.google.firebase:firebase-bom:32.7.0')
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-analytics'
}- Add the
google-services.jsonfile to yourandroid/app/directory.
Add the required permissions and service:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application>
<service
android:name="app.tauri.plugin.fcm.FcmService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>- Add Firebase dependencies to your
ios/Package.swift:
dependencies: [
.package(url: "https://github.com/firebase/firebase-ios-sdk", from: "10.0.0")
]-
Add the
GoogleService-Info.plistfile to your iOS project. -
Initialize Firebase in your iOS app delegate.
import { createFcm } from '@tauri-apps/plugin-fcm';
const fcm = createFcm();
// Get FCM token
const token = await fcm.getToken();
console.log('FCM Token:', token);
// Subscribe to a topic
await fcm.subscribeToTopic('news');
// Unsubscribe from a topic
await fcm.unsubscribeFromTopic('news');
// Check notification permissions
const enabled = await fcm.areNotificationsEnabled();
console.log('Notifications enabled:', enabled);
// Request notification permission
const granted = await fcm.requestNotificationPermission();
console.log('Permission granted:', granted);
// Delete FCM token
await fcm.deleteToken();
// Get last received message
const lastMessage = await fcm.getLastMessage();
console.log('Last message:', lastMessage);// Listen for token received events
const tokenListener = await fcm.onTokenReceived((event) => {
console.log('Token received:', event.token);
console.log('Timestamp:', event.timestamp);
});
// Listen for message received events
const messageListener = await fcm.onMessageReceived((message) => {
console.log('Message received:', message);
console.log('Title:', message.notification?.title);
console.log('Body:', message.notification?.body);
console.log('Data:', message.data);
});
// Listen for topic change events
const topicListener = await fcm.onTopicChanged((event) => {
console.log(`Topic ${event.topic} ${event.action}`);
});
// Listen for token refresh events
const refreshListener = await fcm.onTokenRefresh((event) => {
console.log('Token refreshed:', event.token);
});
// Listen for errors
const errorListener = await fcm.onTokenError((event) => {
console.error('FCM Error:', event.error);
});
// Clean up listeners
tokenListener.unlisten();
messageListener.unlisten();
topicListener.unlisten();
refreshListener.unlisten();
errorListener.unlisten();use tauri_plugin_fcm::Fcm;
// Get FCM instance
let fcm = Fcm::new(handle);
// Get token
let token = fcm.get_token()?;
println!("FCM Token: {}", token);
// Subscribe to topic
fcm.subscribe_to_topic("news".to_string())?;
// Unsubscribe from topic
fcm.unsubscribe_from_topic("news".to_string())?;
// Check notifications
let enabled = fcm.are_notifications_enabled()?;
println!("Notifications enabled: {}", enabled);
// Request permission
let granted = fcm.request_notification_permission()?;
println!("Permission granted: {}", granted);
// Delete token
fcm.delete_token()?;
// Get last message
let last_message = fcm.get_last_message()?;
println!("Last message: {:?}", last_message);Gets the current FCM registration token.
Subscribes to an FCM topic.
Unsubscribes from an FCM topic.
Checks if notifications are enabled on the device.
Requests notification permission from the user.
Deletes the current FCM token.
Gets the last received FCM message.
Fired when a new FCM token is received.
{
token: string;
timestamp: number;
}Fired when the FCM token is refreshed.
{
token: string;
timestamp: number;
}Fired when a new FCM message is received.
{
messageId?: string;
from?: string;
to?: string;
messageType?: string;
sentTime?: number;
ttl?: number;
notification?: {
title?: string;
body?: string;
icon?: string;
color?: string;
sound?: string;
tag?: string;
clickAction?: string;
};
data?: Record<string, string>;
}Fired when topic subscription status changes.
{
topic: string;
action: 'subscribed' | 'unsubscribed';
}Fired when the FCM token is deleted.
{
action: string;
}Fired when an FCM error occurs.
{
error: string;
}The plugin automatically handles the following permissions:
POST_NOTIFICATIONS- Required for push notificationsINTERNET- Required for FCM communicationWAKE_LOCK- Required for background message processing
The plugin uses the standard Firebase configuration through google-services.json.
The plugin uses the standard Firebase configuration through GoogleService-Info.plist.
-
Token not received: Ensure Firebase is properly configured and the app has internet connectivity.
-
Messages not received: Check notification permissions and ensure the app is properly registered with FCM.
-
Build errors: Ensure all Firebase dependencies are correctly added and the configuration files are in place.
Enable debug logging by setting the log level in your Tauri configuration:
{
"tauri": {
"allowlist": {
"all": false,
"log": {
"all": true
}
}
}
}Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
For support, please open an issue on the GitHub repository or check the Tauri documentation.