Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[push notification plugin] init + WIP android part #1531

Draft
wants to merge 1 commit into
base: v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| [clipboard-manager](plugins/clipboard-manager) | Read and write to the system clipboard. | ✅ | ✅ | ✅ | ✅ | ✅ |
| [deep-link](plugins/deep-link) | Set your Tauri application as the default handler for an URL. | ✅ | ✅ | ✅ | ✅ | ✅ |
| [dialog](plugins/dialog) | Native system dialogs for opening and saving files along with message dialogs. | ✅ | ✅ | ✅ | ✅ | ✅ |
| [fcm](plugins/fcm) | Push notification via Firebase. | ? | ? | ? | ✅ | ✅ |
| [fs](plugins/fs) | Access the file system. | ✅ | ✅ | ✅ | ? | ? |
| [global-shortcut](plugins/global-shortcut) | Register global shortcuts. | ✅ | ✅ | ✅ | ? | ? |
| [http](plugins/http) | Access the HTTP client written in Rust. | ✅ | ✅ | ✅ | ✅ | ✅ |
Expand Down
16 changes: 15 additions & 1 deletion examples/api/src-tauri/gen/schemas/desktop-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4830,7 +4830,7 @@
]
},
{
"description": "global-shortcut:default -> No features are enabled by default, as we believe\nthe shortcuts can be inherently dangerous and it is \napplication specific if specific shortcuts should be\nregistered or unregistered.\n",
"description": "global-shortcut:default -> No features are enabled by default, as we believe\nthe shortcuts can be inherently dangerous and it is\napplication specific if specific shortcuts should be\nregistered or unregistered.\n",
"type": "string",
"enum": [
"global-shortcut:default"
Expand All @@ -4850,6 +4850,13 @@
"global-shortcut:allow-register"
]
},
{
"description": "global-shortcut:allow-register-all -> Enables the register_all command without any pre-configured scope.",
"type": "string",
"enum": [
"global-shortcut:allow-register-all"
]
},
{
"description": "global-shortcut:allow-unregister -> Enables the unregister command without any pre-configured scope.",
"type": "string",
Expand Down Expand Up @@ -4878,6 +4885,13 @@
"global-shortcut:deny-register"
]
},
{
"description": "global-shortcut:deny-register-all -> Denies the register_all command without any pre-configured scope.",
"type": "string",
"enum": [
"global-shortcut:deny-register-all"
]
},
{
"description": "global-shortcut:deny-unregister -> Denies the unregister command without any pre-configured scope.",
"type": "string",
Expand Down
17 changes: 17 additions & 0 deletions plugins/fcm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/.vs
.DS_Store
.Thumbs.db
*.sublime*
.idea/
debug.log
package-lock.json
.vscode/settings.json
yarn.lock

/.tauri
/target
Cargo.lock
node_modules/

dist-js
dist
25 changes: 25 additions & 0 deletions plugins/fcm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "tauri-plugin-fcm"
version = "2.0.0-beta.0"
description = "Get push notification capabilities on your Tauri app."
authors = { workspace = true }
license = { workspace = true }
edition = { workspace = true }
rust-version = { workspace = true }
repository = { workspace = true }
links = "tauri-plugin-fcm"

[package.metadata.docs.rs]
rustc-args = [ "--cfg", "docsrs" ]
rustdoc-args = [ "--cfg", "docsrs" ]

[build-dependencies]
tauri-plugin = { workspace = true, features = [ "build" ] }

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
tauri = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }

3 changes: 3 additions & 0 deletions plugins/fcm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Tauri Plugin fcm

# TODO
2 changes: 2 additions & 0 deletions plugins/fcm/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build
/.tauri
46 changes: 46 additions & 0 deletions plugins/fcm/android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "app.tauri.fcm"
compileSdk = 34

defaultConfig {
minSdk = 21
targetSdk = 34

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation("com.google.code.gson:gson:2.11.0")
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.appcompat:appcompat:1.7.0")
implementation("com.google.android.material:material:1.12.0")
implementation("com.google.firebase:firebase-installations:18.0.0")
implementation("com.google.firebase:firebase-messaging:24.0.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
implementation(project(":tauri-android"))
}
21 changes: 21 additions & 0 deletions plugins/fcm/android/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions plugins/fcm/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include ':tauri-android'
project(':tauri-android').projectDir = new File('./.tauri/tauri-api')
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app.tauri.fcm

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("app.tauri.fcm", appContext.packageName)
}
}
27 changes: 27 additions & 0 deletions plugins/fcm/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<receiver
android:name=".NotificationHandler"
android:directBootAware="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service
android:name=".FCMService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
</manifest>
21 changes: 21 additions & 0 deletions plugins/fcm/android/src/main/java/NotificationHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app.tauri.fcm

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import kotlin.time.Duration.Companion.milliseconds

class NotificationHandler : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val mainActivityClass = Class.forName("${context.packageName}.MainActivity")
val newIntent = Intent(context, mainActivityClass).apply {
flags =
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
}
newIntent.putExtra("data", intent.getStringExtra("data"))
newIntent.putExtra("sent_at", intent.getLongExtra("sent_at", 0))
newIntent.putExtra("opened_at", System.currentTimeMillis())

context.startActivity(newIntent)
}
}
94 changes: 94 additions & 0 deletions plugins/fcm/android/src/main/java/Plugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package app.tauri.fcm

import android.app.Activity
import android.content.Intent
import android.webkit.WebView
import app.tauri.annotation.Command
import app.tauri.annotation.InvokeArg
import app.tauri.annotation.TauriPlugin
import app.tauri.plugin.JSObject
import app.tauri.plugin.Plugin
import app.tauri.plugin.Invoke

import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.installations.FirebaseInstallations
import com.google.firebase.messaging.FirebaseMessaging

@InvokeArg
class PluginConfig {
lateinit var androidAppId: String
lateinit var apiKey: String
lateinit var projectId: String
}

@InvokeArg
class SubscribeToTopicArgs {
lateinit var topic: String
}

@TauriPlugin
class FCMPlugin(private val activity: Activity) : Plugin(activity) {
private var latestData = JSObject()

override fun load(webView: WebView) {
val config = this.getConfig(PluginConfig::class.java)
val options = FirebaseOptions.Builder().setApiKey(config.apiKey)
.setProjectId(config.projectId)
.setApplicationId(config.androidAppId).build()

FirebaseApp.initializeApp(activity, options)

activity.intent?.let {
handleIntent(it)
}
}

override fun onNewIntent(newIntent: Intent) {
handleIntent(newIntent)
}

private fun handleIntent(newIntent: Intent) {
newIntent.extras?.let {
val data = it.getString("data")
val sentAt = it.getLong("sent_at")
val openedAt = it.getLong("opened_at")
if (data != null) {
val result = JSObject().apply {
put("data", JSObject(data))
put("sentAt", sentAt)
put("openedAt", openedAt)
}

this.latestData = result
trigger("pushNotificationOpened", result)
}
}
}

@Command
fun getLatestNotificationData(invoke: Invoke) {
invoke.resolve(this.latestData)
}

@Command
fun getToken(invoke: Invoke) {
FirebaseInstallations.getInstance().getToken(true).addOnSuccessListener { result ->
invoke.resolve(JSObject().put("token", result.token))
}.addOnFailureListener { e ->
invoke.reject("Cannot get token", e)
}
}

@Command
fun subscribeToTopic(invoke: Invoke) {
val args = invoke.parseArgs(SubscribeToTopicArgs::class.java)

FirebaseMessaging.getInstance().subscribeToTopic(args.topic).addOnSuccessListener {
invoke.resolve(JSObject())
}.addOnFailureListener { e ->
invoke.reject("Cannot subscribe to topic", e)
}
}

}
Loading
Loading