tauri-plugin-notifications 0.4.5

A Tauri v2 plugin for sending notifications on desktop and mobile platforms with support for system notifications and push delivery via FCM and APNs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package app.tauri.notification

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.webkit.WebView
import app.tauri.PermissionState
import app.tauri.annotation.Command
import app.tauri.annotation.InvokeArg
import app.tauri.annotation.Permission
import app.tauri.annotation.PermissionCallback
import app.tauri.annotation.TauriPlugin
import app.tauri.Logger
import app.tauri.plugin.Invoke
import app.tauri.plugin.JSArray
import app.tauri.plugin.JSObject
import app.tauri.plugin.Plugin
import com.google.firebase.messaging.FirebaseMessaging

const val LOCAL_NOTIFICATIONS = "permissionState"

@InvokeArg
class PluginConfig {
  var icon: String? = null
  var sound: String? = null
  var iconColor: String? = null
}

@InvokeArg
class BatchArgs {
  lateinit var notifications: List<Notification>
}

@InvokeArg
class CancelArgs {
  lateinit var notifications: List<Int>
}

@InvokeArg
class NotificationAction {
  lateinit var id: String
  var title: String? = null
  var input: Boolean? = null
}

@InvokeArg
class ActionType {
  lateinit var id: String
  lateinit var actions: List<NotificationAction>
}

@InvokeArg
class RegisterActionTypesArgs {
  lateinit var types: List<ActionType>
}

@InvokeArg
class SetClickListenerActiveArgs {
  var active: Boolean = false
}

@InvokeArg
class ActiveNotification {
  var id: Int = 0
  var tag: String? = null
}

@InvokeArg
class RemoveActiveArgs {
  var notifications: List<ActiveNotification> = listOf()
}

@TauriPlugin(
  permissions = [
    Permission(strings = [Manifest.permission.POST_NOTIFICATIONS], alias = "permissionState")
  ]
)
class NotificationPlugin(private val activity: Activity): Plugin(activity) {
  private var webView: WebView? = null
  private lateinit var manager: TauriNotificationManager
  private lateinit var notificationManager: NotificationManager
  private lateinit var notificationStorage: NotificationStorage
  private var channelManager = ChannelManager(activity)

  private var pendingTokenInvoke: Invoke? = null
  private var cachedToken: String? = null

  // Click listener tracking for cold-start support
  private var hasClickedListener = false
  private var pendingNotificationClick: JSObject? = null

  companion object {
    var instance: NotificationPlugin? = null

    fun triggerNotification(notification: Notification, source: String = "local") {
      val data = JSObject()
      data.put("source", source)
      data.put("id", notification.id)
      notification.title?.let { data.put("title", it) }
      notification.body?.let { data.put("body", it) }
      notification.largeBody?.let { data.put("largeBody", it) }
      notification.summary?.let { data.put("summary", it) }
      notification.sound?.let { data.put("sound", it) }
      notification.actionTypeId?.let { data.put("actionTypeId", it) }
      notification.group?.let { data.put("group", it) }
      notification.channelId?.let { data.put("channelId", it) }
      if (notification.isGroupSummary) data.put("groupSummary", true)
      if (notification.isOngoing) data.put("ongoing", true)
      if (notification.isAutoCancel) data.put("autoCancel", true)
      notification.silent?.let { data.put("silent", it) }
      notification.extra?.let { data.put("extra", it) }
      notification.inboxLines?.let { data.put("inboxLines", JSArray(it)) }
      notification.attachments?.let { attachments ->
        val arr = JSArray()
        for (att in attachments) {
          val obj = JSObject()
          att.id?.let { obj.put("id", it) }
          att.url?.let { obj.put("url", it) }
          arr.put(obj)
        }
        data.put("attachments", arr)
      }
      instance?.trigger("notification", data)
    }
  }

  override fun load(webView: WebView) {
    instance = this

    super.load(webView)
    this.webView = webView
    notificationStorage = NotificationStorage(activity, jsonMapper())
    
    val manager = TauriNotificationManager(
      notificationStorage,
      activity,
      activity,
      getConfig(PluginConfig::class.java)
    )
    manager.createNotificationChannel()
    
    this.manager = manager
    
    notificationManager = activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    val intent = activity.intent
    intent?.let {
      onIntent(it)
    }
  }

  override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    onIntent(intent)
  }

  fun onIntent(intent: Intent) {
    Logger.debug(Logger.tags(TAG), "onIntent called - action: ${intent.action}, extras: ${intent.extras?.keySet()}")

    // Handle local notification click (requires ACTION_MAIN)
    if (Intent.ACTION_MAIN == intent.action) {
      val dataJson = manager.handleNotificationActionPerformed(intent, notificationStorage)
      if (dataJson != null) {
        trigger("actionPerformed", dataJson)
        triggerNotificationClicked(
          intent.getIntExtra(NOTIFICATION_INTENT_KEY, -1),
          extractLocalNotificationData(intent)
        )
        return
      }
    }

    // Handle push notification click (Firebase background notification)
    // Firebase may use different actions, so check for push data regardless of action
    val pushData = extractPushNotificationData(intent)
    if (pushData != null) {
      Logger.debug(Logger.tags(TAG), "Push notification clicked with data: $pushData")
      triggerNotificationClicked(-1, pushData)
    }
  }

  private fun extractLocalNotificationData(intent: Intent): JSObject? {
    val notificationJson = intent.getStringExtra(NOTIFICATION_OBJ_INTENT_KEY) ?: return null
    return try {
      val notification = JSObject(notificationJson)
      if (notification.has("extra")) notification.getJSObject("extra") else null
    } catch (e: Exception) {
      Logger.error(Logger.tags(TAG), "Failed to extract local notification data: ${e.message}", e)
      null
    }
  }

  private fun extractPushNotificationData(intent: Intent): JSObject? {
    val extras = intent.extras ?: return null
    // Skip if no extras or if it's a regular app launch
    if (extras.isEmpty) return null

    Logger.debug(Logger.tags(TAG), "extractPushNotificationData - all extras: ${extras.keySet().map { "$it=${extras.getString(it)}" }}")

    // Filter out system/internal keys, keep user data
    val data = JSObject()
    for (key in extras.keySet()) {
      // Skip Android/Firebase internal keys
      if (key.startsWith("android.") || key.startsWith("google.") ||
          key.startsWith("gcm.") || key == "from" || key == "collapse_key") continue
      extras.getString(key)?.let { data.put(key, it) }
    }
    Logger.debug(Logger.tags(TAG), "extractPushNotificationData - filtered data length: ${data.length()}")
    return if (data.length() > 0) data else null
  }

  private fun triggerNotificationClicked(id: Int, data: JSObject?) {
    val clickedData = JSObject()
    clickedData.put("id", id)
    if (data != null) {
      clickedData.put("data", data)
    }

    Logger.debug(Logger.tags(TAG), "triggerNotificationClicked - id: $id, hasClickedListener: $hasClickedListener, data: $data")

    if (hasClickedListener) {
      trigger("notificationClicked", clickedData)
    } else {
      Logger.debug(Logger.tags(TAG), "No click listener, storing as pending")
      pendingNotificationClick = clickedData
    }
  }

  @Command
  fun show(invoke: Invoke) {
    val notification = invoke.parseArgs(Notification::class.java)
    notification.sourceJson = invoke.getRawArgs()

    val id = manager.schedule(notification)
    if (notification.schedule != null) {
      notificationStorage.appendNotifications(listOf(notification))
    }

    invoke.resolveObject(id)
  }

  @Command
  fun batch(invoke: Invoke) {
    val args = invoke.parseArgs(BatchArgs::class.java)
    val mapper = jsonMapper()
    for (notification in args.notifications) {
      notification.sourceJson = mapper.writeValueAsString(notification)
    }

    val ids = manager.schedule(args.notifications)
    notificationStorage.appendNotifications(args.notifications)

    invoke.resolveObject(ids)
  }

  @Command
  fun cancel(invoke: Invoke) {
    val args = invoke.parseArgs(CancelArgs::class.java)
    manager.cancel(args.notifications)
    invoke.resolve()
  }

  @Command
  fun cancelAll(invoke: Invoke) {
    val ids = notificationStorage.getSavedNotificationIds().mapNotNull { it.toIntOrNull() }
    manager.cancel(ids)
    invoke.resolve()
  }

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

    if (args.notifications.isEmpty()) {
      notificationManager.cancelAll()
      invoke.resolve()
    } else {
      for (notification in args.notifications) {
        if (notification.tag == null) {
          notificationManager.cancel(notification.id)
        } else {
          notificationManager.cancel(notification.tag, notification.id)
        }
      }
      invoke.resolve()
    }
  }

  @Command
  fun getPending(invoke: Invoke) {
    val notifications = notificationStorage.getSavedNotifications()
    val result = Notification.buildNotificationPendingList(notifications)
    invoke.resolveObject(result)
  }

  @Command
  fun registerActionTypes(invoke: Invoke) {
    val args = invoke.parseArgs(RegisterActionTypesArgs::class.java)
    notificationStorage.writeActionGroup(args.types)
    invoke.resolve()
  }

  @SuppressLint("ObsoleteSdkInt")
  @Command
  fun getActive(invoke: Invoke) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      val result = Notification.buildNotificationActiveList(notificationManager.activeNotifications)
      invoke.resolveObject(result)
    } else {
      invoke.resolveObject(emptyList<ActiveNotificationInfo>())
    }
  }

  @Command
  fun createChannel(invoke: Invoke) {
    channelManager.createChannel(invoke)
  }

  @Command
  fun deleteChannel(invoke: Invoke) {
    channelManager.deleteChannel(invoke)
  }

  @Command
  fun listChannels(invoke: Invoke) {
    channelManager.listChannels(invoke)
  }

  @Command
  override fun checkPermissions(invoke: Invoke) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
      val permissionsResultJSON = JSObject()
      permissionsResultJSON.put("permissionState", getPermissionState())
      invoke.resolve(permissionsResultJSON)
    } else {
      super.checkPermissions(invoke)
    }
  }

  @Command
  override fun requestPermissions(invoke: Invoke) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
      permissionState(invoke)
    } else {
      if (getPermissionState(LOCAL_NOTIFICATIONS) !== PermissionState.GRANTED) {
        requestPermissionForAlias(LOCAL_NOTIFICATIONS, invoke, "permissionsCallback")
      }
    }
  }

  @Command
  fun registerForPushNotifications(invoke: Invoke) {
    if (!BuildConfig.ENABLE_PUSH_NOTIFICATIONS) {
      invoke.reject("Push notifications are disabled in this build")
      return
    }

    // First check if notifications are enabled
    if (!manager.areNotificationsEnabled()) {
      // Request permissions first
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        if (getPermissionState(LOCAL_NOTIFICATIONS) !== PermissionState.GRANTED) {
          // Request permissions and then get token
          pendingTokenInvoke = invoke
          requestPermissionForAlias(LOCAL_NOTIFICATIONS, invoke, "pushPermissionsCallback")
          return
        }
      } else {
        invoke.reject("Notification permissions not granted")
        return
      }
    }

    // If we already have a cached token, return it immediately
    cachedToken?.let {
      val result = JSObject()
      result.put("deviceToken", it)
      invoke.resolve(result)
      return
    }

    // Store the invoke to respond later when we get the token
    pendingTokenInvoke = invoke

    // Request the FCM token
    getFirebaseToken()
  }

  @Command
  fun unregisterForPushNotifications(invoke: Invoke) {
    if (!BuildConfig.ENABLE_PUSH_NOTIFICATIONS) {
      invoke.reject("Push notifications are disabled in this build")
      return
    }

    FirebaseMessaging.getInstance().deleteToken().addOnCompleteListener { task ->
      if (!task.isSuccessful) {
        invoke.reject("Failed to delete FCM token: ${task.exception?.message}")
        return@addOnCompleteListener
      }
      cachedToken = null
      invoke.resolve()
    }
  }

  @PermissionCallback
  private fun pushPermissionsCallback(invoke: Invoke) {
    if (!manager.areNotificationsEnabled()) {
      invoke.reject("Notification permissions denied")
      pendingTokenInvoke = null
      return
    }

    // Permissions granted, now get the token
    getFirebaseToken()
  }

  private fun getFirebaseToken() {
    if (!BuildConfig.ENABLE_PUSH_NOTIFICATIONS) {
      pendingTokenInvoke?.reject("Push notifications are disabled in this build")
      pendingTokenInvoke = null
      return
    }

    FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
      if (!task.isSuccessful) {
        val errorMessage = "Failed to get FCM token: ${task.exception?.message}"
        val errorData = JSObject()
        errorData.put("message", errorMessage)
        trigger("push-error", errorData)
        pendingTokenInvoke?.reject(errorMessage)
        pendingTokenInvoke = null
        return@addOnCompleteListener
      }

      val token = task.result
      cachedToken = token
      val result = JSObject()
      result.put("deviceToken", token)
      pendingTokenInvoke?.resolve(result)
      pendingTokenInvoke = null
    }
  }

  // Called by TauriFirebaseMessagingService when a new token is received
  fun handleNewToken(token: String) {
    if (!BuildConfig.ENABLE_PUSH_NOTIFICATIONS) return

    cachedToken = token
    // Trigger push-token event to notify the frontend about the token
    val data = JSObject()
    data.put("token", token)
    trigger("push-token", data)
  }

  // Called by TauriFirebaseMessagingService when a push message is received
  fun triggerPushMessage(pushData: Map<String, Any>) {
    if (!BuildConfig.ENABLE_PUSH_NOTIFICATIONS) return

    val data = JSObject()
    for ((key, value) in pushData) {
      when (value) {
        is String -> data.put(key, value)
        is Int -> data.put(key, value)
        is Long -> data.put(key, value)
        is Double -> data.put(key, value)
        is Boolean -> data.put(key, value)
        is Map<*, *> -> {
          val nestedObj = JSObject()
          @Suppress("UNCHECKED_CAST")
          val map = value as Map<String, Any>
          for ((k, v) in map) {
            nestedObj.put(k, v.toString())
          }
          data.put(key, nestedObj)
        }
        else -> data.put(key, value.toString())
      }
    }
    trigger("push-message", data)
  }

  @Command
  fun permissionState(invoke: Invoke) {
    val permissionsResultJSON = JSObject()
    permissionsResultJSON.put("permissionState", getPermissionState())
    invoke.resolve(permissionsResultJSON)
  }

  @PermissionCallback
  private fun permissionsCallback(invoke: Invoke) {
    val permissionsResultJSON = JSObject()
    permissionsResultJSON.put("permissionState", getPermissionState())
    invoke.resolve(permissionsResultJSON)
  }

  private fun getPermissionState(): String {
    return if (manager.areNotificationsEnabled()) {
      "granted"
    } else {
      "denied"
    }
  }

  @Command
  fun setClickListenerActive(invoke: Invoke) {
    val args = invoke.parseArgs(SetClickListenerActiveArgs::class.java)
    hasClickedListener = args.active

    // If listener just became active and we have pending click, trigger it
    if (args.active && pendingNotificationClick != null) {
      trigger("notificationClicked", pendingNotificationClick!!)
      pendingNotificationClick = null
    }

    invoke.resolve()
  }
}