Push Notifications
Push notifications for your cross-platform Tauri app.
Supported Platforms
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| linux | | No push functionality available |
| windows | | Experimental |
| macos | ||
| android | ||
| ios |
Setup
First, enable the push-notifications feature in your Cargo.toml file:
[dependencies]tauri = { version = "1", features = ["push-notifications"] }Then, install the push-notifications plugin:
Use your project’s package manager to add the dependency:
npm run tauri add push-notificationsyarn run tauri add push-notificationspnpm tauri add push-notificationsbun tauri add push-notificationscargo tauri add push-notifications-
Run the following command in the
src-taurifolder to add the plugin to the project’s dependencies inCargo.toml:cargo add tauri-plugin-push-notifications -
Modify
lib.rsto initialize the plugin:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(desktop)]app.handle().plugin(tauri_plugin_push_notifications::init());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm install tauri-plugin-push-notificationsyarn add tauri-plugin-push-notificationspnpm add tauri-plugin-push-notificationsdeno add npm:tauri-plugin-push-notificationsbun add tauri-plugin-push-notifications
Configuration
Each platform requires specific configuration to enable push notifications. Each push provider (Apple, Google, Microsoft, etc.) typically requires credentials and/or entitlements to obtain and use a push token.
Apple Platforms
To access the Apple Push Notification Service (APNS) on macOS and iOS, you need to create an APNS certificate or token, and you must add an entitlement to your application.
iOS Configuration
Add the following entitlement to your iOS app:
<key>aps-environment</key> <string>production</string>Here’s an example of a full entitlements file (yours may vary, ... values are placeholders):
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict> <key>com.apple.application-identifier</key> <string>...</string> <key>com.apple.developer.team-identifier</key> <string>...</string> <key>aps-environment</key> <string>production</string></dict></plist>macOS Configuration
Mac is similar to iOS. You still need an APNS certificate, which can be obtained from the Apple Developer portal. You also need to adjust your
Info.plist and entitlements as shown below.
Add the following to your entitlements:
<key>com.apple.developer.aps-environment</key> <string>production</string>Here’s an example of a full entitlements file (yours may vary, ... values are placeholders):
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict> <key>com.apple.application-identifier</key> <string>...</string> <key>com.apple.developer.team-identifier</key> <string>...</string> <key>com.apple.developer.aps-environment</key> <string>production</string></dict></plist>Android
Android uses Firebase Cloud Messaging (FCM) to send push notifications. To configure FCM, you need to create a Firebase project and add the
google-services.json file to your project. You can find more information in the Firebase documentation.
Once these steps are complete, you can send push notifications to your Android app.
(TBD)
Windows
Microsoft provides Windows Notification System (WNS) for pushing notifications to apps on Windows devices. WNS is newer than APNS and FCM, so it is not as widely used or supported, but it works in much the same way.
Support for WNS in Tauri is experimental.
Applications are automatically entitled for WNS on Windows. To obtain a push token, simply enable the push-notifications feature and use the plugin as described.
Usage
The Push Notifications plugin is available in both JavaScript and Rust, allowing you to obtain a push token (where supported).
Obtaining the push token
When the push-notifications feature is enabled within Tauri, the application will automatically request a push token from the underlying
platform APIs when your app starts.
Then, from JavaScript or Rust guest code, you can obtain this token, and do whatever you need with it (usually send it to a server for later use when pushing notifications).
import { pushToken } from 'tauri-plugin-push-notifications';
// ... later ...
const token = await pushToken();
// `token` will be://// - `null` if the platform does not support push notifications, or if// push is not configured, entitled, or enabled for the app; or//// - a base64-encoded string expressing the raw bytes of the push token.// coming soonPermissions
By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.
See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "permissions": [ ..., "push-notifications:default", ]}Default Permission
Default permissions for the plugin
allow-push-token
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the push_token command without any pre-configured scope. |
|
|
Denies the push_token command without any pre-configured scope. |
© 2024 Tauri Contributors. CC-BY / MIT