diff --git a/plugins/notification/android/src/main/java/NotificationPlugin.kt b/plugins/notification/android/src/main/java/NotificationPlugin.kt index aa6a3531..1c7d22b4 100644 --- a/plugins/notification/android/src/main/java/NotificationPlugin.kt +++ b/plugins/notification/android/src/main/java/NotificationPlugin.kt @@ -5,6 +5,7 @@ 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 @@ -28,9 +29,9 @@ const val LOCAL_NOTIFICATIONS = "permissionState" ) class NotificationPlugin(private val activity: Activity): Plugin(activity) { private var webView: WebView? = null - private var manager: TauriNotificationManager? = null - var notificationManager: NotificationManager? = null - private var notificationStorage: NotificationStorage? = null + private lateinit var manager: TauriNotificationManager + private lateinit var notificationManager: NotificationManager + private lateinit var notificationStorage: NotificationStorage private var channelManager = ChannelManager(activity) override fun load(webView: WebView) { @@ -39,7 +40,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { notificationStorage = NotificationStorage(activity) val manager = TauriNotificationManager( - notificationStorage!!, + notificationStorage, activity, activity, getConfig() @@ -48,24 +49,24 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { this.manager = manager - notificationManager = activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager? + notificationManager = activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } - /*private fun handleOnNewIntent(data: Intent) { - super.handleOnNewIntent(data) - if (Intent.ACTION_MAIN != data.action) { + override fun onNewIntent(intent: Intent) { + super.onNewIntent(intent) + if (Intent.ACTION_MAIN != intent.action) { return } - val dataJson = manager.handleNotificationActionPerformed(data, notificationStorage) + val dataJson = manager.handleNotificationActionPerformed(intent, notificationStorage) if (dataJson != null) { - // notifyListeners("localNotificationActionPerformed", dataJson, true) + // TODO trigger("actionPerformed", dataJson, true) } - }*/ + } @Command fun show(invoke: Invoke) { val notification = Notification.fromJSObject(invoke.data) - val id = manager!!.schedule(notification) + val id = manager.schedule(notification) val returnVal = JSObject().put("id", id) invoke.resolve(returnVal) @@ -93,8 +94,8 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { notifications.add(notification) } - val ids = manager!!.schedule(notifications) - notificationStorage?.appendNotifications(notifications) + val ids = manager.schedule(notifications) + notificationStorage.appendNotifications(notifications) val result = JSObject() val jsArray = JSArray() @@ -113,7 +114,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { fun cancel(invoke: Invoke) { val notifications = invoke.getArray("notifications") if (notifications == null) { - manager?.cancel(invoke) + manager.cancel(invoke) } else { try { for (o in notifications.toList()) { @@ -122,9 +123,9 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { val tag = notification.getString("tag") val id = notification.getInteger("id") if (tag.isEmpty()) { - notificationManager!!.cancel(id!!) + notificationManager.cancel(id!!) } else { - notificationManager!!.cancel(tag, id!!) + notificationManager.cancel(tag, id!!) } } else { invoke.reject("Unexpected notification type") @@ -139,7 +140,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { @Command fun getPending(invoke: Invoke) { - val notifications= notificationStorage!!.getSavedNotifications() + val notifications= notificationStorage.getSavedNotifications() val result = Notification.buildNotificationPendingList(notifications) invoke.resolve(result) } @@ -148,7 +149,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { fun registerActionTypes(invoke: Invoke) { val types = invoke.getArray("types", JSArray()) val typesArray = NotificationAction.buildTypes(types) - notificationStorage?.writeActionGroup(typesArray) + notificationStorage.writeActionGroup(typesArray) invoke.resolve() } @@ -157,7 +158,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { fun getDeliveredNotifications(invoke: Invoke) { val notifications = JSArray() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - val activeNotifications = notificationManager!!.activeNotifications + val activeNotifications = notificationManager.activeNotifications for (activeNotification in activeNotifications) { val jsNotification = JSObject() jsNotification.put("id", activeNotification.id) @@ -187,7 +188,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { @Command fun cancelAll(invoke: Invoke) { - notificationManager!!.cancelAll() + notificationManager.cancelAll() invoke.resolve() } @@ -243,7 +244,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { } private fun getPermissionState(): String { - return if (manager!!.areNotificationsEnabled()) { + return if (manager.areNotificationsEnabled()) { "granted" } else { "denied" diff --git a/plugins/notification/guest-js/index.ts b/plugins/notification/guest-js/index.ts index 17a34d50..2cdc3c06 100644 --- a/plugins/notification/guest-js/index.ts +++ b/plugins/notification/guest-js/index.ts @@ -33,9 +33,7 @@ import { invoke } from '@tauri-apps/api/tauri' */ interface Options { /** - * The notification identifier to reference this object later. - * - * On Android, it is a 32 bit integer. + * The notification identifier to reference this object later. Must be a 32-bit integer. */ id?: number /** @@ -132,24 +130,24 @@ interface Options { } type ScheduleInterval = { - year?: number - month?: number - day?: number - /** - * 1 - Sunday - * 2 - Monday - * 3 - Tuesday - * 4 - Wednesday - * 5 - Thursday - * 6 - Friday - * 7 - Saturday - */ - weekday?: number - hour?: number - minute?: number - second?: number - } - + year?: number + month?: number + day?: number + /** + * 1 - Sunday + * 2 - Monday + * 3 - Tuesday + * 4 - Wednesday + * 5 - Thursday + * 6 - Friday + * 7 - Saturday + */ + weekday?: number + hour?: number + minute?: number + second?: number +} + enum ScheduleEvery { Year = 'Year', Month = 'Month', @@ -190,7 +188,7 @@ class Schedule { } static at(date: Date, repeating = false) { - return new Schedule({ kind: 'At', data: { date, repeating }}) + return new Schedule({ kind: 'At', data: { date, repeating } }) } static interval(interval: ScheduleInterval) { @@ -198,7 +196,7 @@ class Schedule { } static every(kind: ScheduleEvery) { - return new Schedule({ kind: 'Every', data: { interval: kind }}) + return new Schedule({ kind: 'Every', data: { interval: kind } }) } }