diff --git a/plugins/notification/android/src/main/java/ChannelManager.kt b/plugins/notification/android/src/main/java/ChannelManager.kt index df3a3f36..cf68e666 100644 --- a/plugins/notification/android/src/main/java/ChannelManager.kt +++ b/plugins/notification/android/src/main/java/ChannelManager.kt @@ -138,14 +138,6 @@ class ChannelManager(private var context: Context) { 0xFFFFFF and notificationChannel.lightColor ) ) - Logger.debug( - Logger.tags("NotificationChannel"), - "visibility " + notificationChannel.lockscreenVisibility - ) - Logger.debug( - Logger.tags("NotificationChannel"), - "importance " + notificationChannel.importance - ) channels.put(channel) } val result = JSObject() diff --git a/plugins/notification/android/src/main/java/Notification.kt b/plugins/notification/android/src/main/java/Notification.kt index bbc75f64..9492901b 100644 --- a/plugins/notification/android/src/main/java/Notification.kt +++ b/plugins/notification/android/src/main/java/Notification.kt @@ -4,13 +4,10 @@ import android.content.ContentResolver import android.content.Context import android.graphics.Bitmap import android.graphics.BitmapFactory -import app.tauri.Logger -import app.tauri.plugin.Invoke import app.tauri.plugin.JSArray import app.tauri.plugin.JSObject import org.json.JSONException import org.json.JSONObject -import java.text.ParseException class Notification { var title: String? = null @@ -170,15 +167,6 @@ class Notification { return result } - fun setExtraFromString(extraFromString: String) { - try { - val jsonObject = JSONObject(extraFromString) - extra = JSObject.fromJSONObject(jsonObject) - } catch (e: JSONException) { - Logger.error(Logger.tags("Notification"), "Cannot rebuild extra data", e) - } - } - companion object { fun fromJson(jsonNotification: JSONObject): Notification { val notification: JSObject = try { @@ -229,26 +217,6 @@ class Notification { return notification } - fun getNotificationPendingList(invoke: Invoke): List? { - var notifications: List? = null - try { - notifications = invoke.getArray("notifications", JSArray()).toList() - } catch (_: JSONException) { - } - if (notifications.isNullOrEmpty()) { - invoke.reject("Must provide notifications array as notifications option") - return null - } - val notificationsList: MutableList = ArrayList(notifications.size) - for (notificationToCancel in notifications) { - try { - notificationsList.add(notificationToCancel.getInt("id")) - } catch (_: JSONException) { - } - } - return notificationsList - } - fun buildNotificationPendingList(notifications: List): JSObject { val result = JSObject() val jsArray = JSArray() diff --git a/plugins/notification/android/src/main/java/NotificationPlugin.kt b/plugins/notification/android/src/main/java/NotificationPlugin.kt index e67cdefc..8beb183b 100644 --- a/plugins/notification/android/src/main/java/NotificationPlugin.kt +++ b/plugins/notification/android/src/main/java/NotificationPlugin.kt @@ -98,21 +98,20 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { notificationStorage.appendNotifications(notifications) val result = JSObject() - val jsArray = JSArray() - for (id in ids) { - try { - val notification = JSObject().put("id", id) - jsArray.put(notification) - } catch (_: Exception) { - } - } - result.put("notifications", jsArray) + result.put("notifications", ids) invoke.resolve(result) } @Command - void cancel(invoke: Invoke) { - manager.cancel(invoke) + fun cancel(invoke: Invoke) { + val notifications: List = invoke.getArray("notifications", JSArray()).toList() + if (notifications.isEmpty()) { + invoke.reject("Must provide notifications array as notifications option") + return + } + + manager.cancel(notifications) + invoke.resolve() } @Command diff --git a/plugins/notification/android/src/main/java/TauriNotificationManager.kt b/plugins/notification/android/src/main/java/TauriNotificationManager.kt index 8c445095..ab5dc3de 100644 --- a/plugins/notification/android/src/main/java/TauriNotificationManager.kt +++ b/plugins/notification/android/src/main/java/TauriNotificationManager.kt @@ -21,15 +21,12 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.app.RemoteInput import app.tauri.Logger -import app.tauri.plugin.Invoke import app.tauri.plugin.JSObject -import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import java.text.SimpleDateFormat import java.util.Date - // Action constants const val NOTIFICATION_INTENT_KEY = "NotificationId" const val NOTIFICATION_OBJ_INTENT_KEY = "LocalNotficationObject" @@ -246,7 +243,7 @@ class TauriNotificationManager( if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { flags = flags or PendingIntent.FLAG_MUTABLE } - val pendingIntent = PendingIntent.getActivity(context, notification.id ?: 0, intent, flags) + val pendingIntent = PendingIntent.getActivity(context, notification.id, intent, flags) mBuilder.setContentIntent(pendingIntent) // Build action types @@ -258,7 +255,7 @@ class TauriNotificationManager( val actionIntent = buildIntent(notification, notificationAction!!.id) val actionPendingIntent = PendingIntent.getActivity( context, - (notification.id ?: 0) + notificationAction.id.hashCode(), + (notification.id) + notificationAction.id.hashCode(), actionIntent, flags ) @@ -295,7 +292,7 @@ class TauriNotificationManager( flags = PendingIntent.FLAG_MUTABLE } val deleteIntent = - PendingIntent.getBroadcast(context, notification.id ?: 0, dissmissIntent, flags) + PendingIntent.getBroadcast(context, notification.id, dissmissIntent, flags) mBuilder.setDeleteIntent(deleteIntent) } @@ -317,6 +314,7 @@ class TauriNotificationManager( * on a certain date "shape" (such as every first of the month) */ // TODO support different AlarmManager.RTC modes depending on priority + @SuppressLint("SimpleDateFormat") private fun triggerScheduledNotification(notification: android.app.Notification, request: Notification) { val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager val schedule = request.schedule @@ -331,7 +329,7 @@ class TauriNotificationManager( flags = flags or PendingIntent.FLAG_MUTABLE } var pendingIntent = - PendingIntent.getBroadcast(context, request.id ?: 0, notificationIntent, flags) + PendingIntent.getBroadcast(context, request.id, notificationIntent, flags) when (val scheduleKind = schedule?.kind) { is ScheduleKind.At -> { @@ -351,7 +349,7 @@ class TauriNotificationManager( val trigger = scheduleKind.interval.nextTrigger(Date()) notificationIntent.putExtra(TimedNotificationPublisher.CRON_KEY, scheduleKind.interval.toMatchString()) pendingIntent = - PendingIntent.getBroadcast(context, request.id ?: 0, notificationIntent, flags) + PendingIntent.getBroadcast(context, request.id, notificationIntent, flags) setExactIfPossible(alarmManager, schedule, trigger, pendingIntent) val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss") Logger.debug( @@ -376,13 +374,13 @@ class TauriNotificationManager( pendingIntent: PendingIntent ) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !alarmManager.canScheduleExactAlarms()) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.whileIdle == true) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.whileIdle) { alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, trigger, pendingIntent) } else { alarmManager[AlarmManager.RTC, trigger] = pendingIntent } } else { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.whileIdle == true) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.whileIdle) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, trigger, pendingIntent) } else { alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent) @@ -390,16 +388,12 @@ class TauriNotificationManager( } } - fun cancel(invoke: Invoke) { - val notificationsToCancel = Notification.getNotificationPendingList(invoke) - if (notificationsToCancel != null) { - for (id in notificationsToCancel) { - dismissVisibleNotification(id) - cancelTimerForNotification(id) - storage.deleteNotification(id.toString()) - } + fun cancel(notifications: List) { + for (id in notifications) { + dismissVisibleNotification(id) + cancelTimerForNotification(id) + storage.deleteNotification(id.toString()) } - invoke.resolve() } private fun cancelTimerForNotification(notificationId: Int) { @@ -511,7 +505,7 @@ class TimedNotificationPublisher : BroadcastReceiver() { return intent.getParcelableExtra(string) } - @SuppressLint("MissingPermission") + @SuppressLint("MissingPermission", "SimpleDateFormat") private fun rescheduleNotificationIfNeeded(context: Context, intent: Intent, id: Int): Boolean { val dateString = intent.getStringExtra(CRON_KEY) if (dateString != null) { diff --git a/plugins/notification/ios/Sources/NotificationPlugin.swift b/plugins/notification/ios/Sources/NotificationPlugin.swift index f08ffdc3..c352e1b0 100644 --- a/plugins/notification/ios/Sources/NotificationPlugin.swift +++ b/plugins/notification/ios/Sources/NotificationPlugin.swift @@ -83,21 +83,16 @@ class NotificationPlugin: Plugin { invoke.reject("`notifications` array is required") return } - var ids = [String]() + var ids = [Int]() for notification in notifications { let request = try showNotification(invoke: invoke, notification: notification) // TODO self.notificationHandler.notificationsMap[request.identifier] = notification - ids.append(request.identifier) + ids.append(Int(request.identifier) ?? -1) } - let ret = ids.map({ (id) -> JSObject in - return [ - "id": Int(id) ?? -1 - ] - }) invoke.resolve([ - "notifications": ret + "notifications": ids ]) } @@ -131,23 +126,18 @@ class NotificationPlugin: Plugin { } @objc func cancel(_ invoke: Invoke) { - guard let notifications = invoke.getArray("notifications", JSObject.self), + guard let notifications = invoke.getArray("notifications", NSNumber.self), notifications.count > 0 else { invoke.reject("`notifications` input is required") return } - let ids = notifications.map({ (value: JSObject) -> String in - if let idString = value["id"] as? String { - return idString - } else if let idNum = value["id"] as? NSNumber { - return idNum.stringValue - } - return "" - }) - - UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ids) + UNUserNotificationCenter.current().removePendingNotificationRequests( + withIdentifiers: notifications.map({ (id) -> String in + return id.stringValue + }) + ) invoke.resolve() } diff --git a/plugins/notification/src/mobile.rs b/plugins/notification/src/mobile.rs index b6f82650..83513eef 100644 --- a/plugins/notification/src/mobile.rs +++ b/plugins/notification/src/mobile.rs @@ -110,17 +110,7 @@ impl Notification { /// Cancel pending notifications. pub fn cancel(&self, notifications: Vec) -> crate::Result<()> { let mut args = HashMap::new(); - args.insert( - "notifications", - notifications - .into_iter() - .map(|id| { - let mut notification = HashMap::new(); - notification.insert("id", id); - notification - }) - .collect::>>(), - ); + args.insert("notifications", notifications); self.0.run_mobile_plugin("cancel", args).map_err(Into::into) }