update android impl

pull/340/head
Lucas Nogueira 2 years ago
parent eff7041d62
commit f776c742b4
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

1
Cargo.lock generated

@ -4927,6 +4927,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"log", "log",
"notify-rust", "notify-rust",
"rand 0.8.5",
"serde", "serde",
"serde_json", "serde_json",
"tauri", "tauri",

@ -16,6 +16,7 @@ serde_json.workspace = true
tauri.workspace = true tauri.workspace = true
log.workspace = true log.workspace = true
thiserror.workspace = true thiserror.workspace = true
rand = "0.8"
[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies] [target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
notify-rust = "4.5" notify-rust = "4.5"

@ -17,7 +17,7 @@ class Notification {
var body: String? = null var body: String? = null
var largeBody: String? = null var largeBody: String? = null
var summary: String? = null var summary: String? = null
var id: Int? = null var id: Int = 0
private var sound: String? = null private var sound: String? = null
private var smallIcon: String? = null private var smallIcon: String? = null
private var largeIcon: String? = null private var largeIcon: String? = null
@ -136,7 +136,7 @@ class Notification {
if (if (title != null) title != that.title else that.title != null) return false if (if (title != null) title != that.title else that.title != null) return false
if (if (body != null) body != that.body else that.body != null) return false if (if (body != null) body != that.body else that.body != null) return false
if (if (largeBody != null) largeBody != that.largeBody else that.largeBody != null) return false if (if (largeBody != null) largeBody != that.largeBody else that.largeBody != null) return false
if (if (id != null) id != that.id else that.id != null) return false if (id != that.id) return false
if (if (sound != null) sound != that.sound else that.sound != null) return false if (if (sound != null) sound != that.sound else that.sound != null) return false
if (if (smallIcon != null) smallIcon != that.smallIcon else that.smallIcon != null) return false if (if (smallIcon != null) smallIcon != that.smallIcon else that.smallIcon != null) return false
if (if (largeIcon != null) largeIcon != that.largeIcon else that.largeIcon != null) return false if (if (largeIcon != null) largeIcon != that.largeIcon else that.largeIcon != null) return false
@ -155,7 +155,7 @@ class Notification {
override fun hashCode(): Int { override fun hashCode(): Int {
var result = if (title != null) title.hashCode() else 0 var result = if (title != null) title.hashCode() else 0
result = 31 * result + if (body != null) body.hashCode() else 0 result = 31 * result + if (body != null) body.hashCode() else 0
result = 31 * result + if (id != null) id.hashCode() else 0 result = 31 * result + id.hashCode()
result = 31 * result + if (sound != null) sound.hashCode() else 0 result = 31 * result + if (sound != null) sound.hashCode() else 0
result = 31 * result + if (smallIcon != null) smallIcon.hashCode() else 0 result = 31 * result + if (smallIcon != null) smallIcon.hashCode() else 0
result = 31 * result + if (iconColor != null) iconColor.hashCode() else 0 result = 31 * result + if (iconColor != null) iconColor.hashCode() else 0
@ -180,50 +180,23 @@ class Notification {
} }
companion object { companion object {
/** fun fromJson(jsonNotification: JSONObject): Notification {
* Build list of the notifications from invoke payload val notification: JSObject = try {
*/ val identifier = jsonNotification.getLong("id")
fun buildNotificationList(invoke: Invoke): List<Notification>? { if (identifier > Int.MAX_VALUE || identifier < Int.MIN_VALUE) {
val notificationArray = invoke.getArray("notifications") throw Exception("The notification identifier should be a 32-bit integer")
if (notificationArray == null) {
invoke.reject("Must provide notifications array as notifications option")
return null
}
val resultNotifications: MutableList<Notification> =
ArrayList(notificationArray.length())
val notificationsJson: List<JSONObject> = try {
notificationArray.toList()
} catch (e: JSONException) {
invoke.reject("Provided notification format is invalid")
return null
}
for (jsonNotification in notificationsJson) {
val notification: JSObject = try {
val identifier = jsonNotification.getLong("id")
if (identifier > Int.MAX_VALUE || identifier < Int.MIN_VALUE) {
invoke.reject("The identifier should be a Java int")
return null
}
JSObject.fromJSONObject(jsonNotification)
} catch (e: JSONException) {
invoke.reject("Invalid JSON object sent to Notification plugin", e)
return null
}
try {
val activeNotification = buildNotificationFromJSObject(notification)
resultNotifications.add(activeNotification)
} catch (e: ParseException) {
invoke.reject("Invalid date format sent to Notification plugin", e)
return null
} }
JSObject.fromJSONObject(jsonNotification)
} catch (e: JSONException) {
throw Exception("Invalid notification JSON object", e)
} }
return resultNotifications return fromJSObject(notification)
} }
fun buildNotificationFromJSObject(jsonObject: JSObject): Notification { fun fromJSObject(jsonObject: JSObject): Notification {
val notification = Notification() val notification = Notification()
notification.source = jsonObject.toString() notification.source = jsonObject.toString()
notification.id = jsonObject.getInteger("id") notification.id = jsonObject.getInteger("id") ?: throw Exception("Missing notification identifier")
notification.body = jsonObject.getString("body") notification.body = jsonObject.getString("body")
notification.largeBody = jsonObject.getString("largeBody") notification.largeBody = jsonObject.getString("largeBody")
notification.summary = jsonObject.getString("summary") notification.summary = jsonObject.getString("summary")

@ -62,25 +62,51 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
} }
}*/ }*/
@Command
fun show(invoke: Invoke) {
val notification = Notification.fromJSObject(invoke.data)
val id = manager!!.schedule(notification)
val returnVal = JSObject().put("id", id)
invoke.resolve(returnVal)
}
@Command @Command
fun batch(invoke: Invoke) { fun batch(invoke: Invoke) {
val localNotifications = Notification.buildNotificationList(invoke) val notificationArray = invoke.getArray("notifications")
?: return if (notificationArray == null) {
val ids = manager!!.schedule(invoke, localNotifications) invoke.reject("Missing `notifications` argument")
if (ids != null) { return
notificationStorage?.appendNotifications(localNotifications) }
val result = JSObject()
val jsArray = JSArray() val notifications: MutableList<Notification> =
for (i in 0 until ids.length()) { ArrayList(notificationArray.length())
try { val notificationsInput: List<JSONObject> = try {
val notification = JSObject().put("id", ids.getInt(i)) notificationArray.toList()
jsArray.put(notification) } catch (e: JSONException) {
} catch (_: Exception) { invoke.reject("Provided notification format is invalid")
} return
}
for (jsonNotification in notificationsInput) {
val notification = Notification.fromJson(jsonNotification)
notifications.add(notification)
}
val ids = manager!!.schedule(notifications)
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)
invoke.resolve(result)
} }
result.put("notifications", jsArray)
invoke.resolve(result)
} }
@Command @Command

@ -47,7 +47,7 @@ class NotificationStorage(private val context: Context) {
if (jsNotification != null) { if (jsNotification != null) {
try { try {
val notification = val notification =
Notification.buildNotificationFromJSObject(jsNotification) Notification.fromJSObject(jsNotification)
notifications.add(notification) notifications.add(notification)
} catch (_: ParseException) { } catch (_: ParseException) {
} }
@ -89,7 +89,7 @@ class NotificationStorage(private val context: Context) {
fun getSavedNotification(key: String?): Notification? { fun getSavedNotification(key: String?): Notification? {
val jsNotification = getSavedNotificationAsJSObject(key) ?: return null val jsNotification = getSavedNotificationAsJSObject(key) ?: return null
val notification = try { val notification = try {
Notification.buildNotificationFromJSObject(jsNotification) Notification.fromJSObject(jsNotification)
} catch (ex: ParseException) { } catch (ex: ParseException) {
return null return null
} }

@ -116,27 +116,28 @@ class TauriNotificationManager(
} }
} }
fun schedule(invoke: Invoke?, notifications: List<Notification>): JSONArray? { private fun trigger(notificationManager: NotificationManagerCompat, notification: Notification): Int {
val ids = JSONArray() dismissVisibleNotification(notification.id)
val notificationManager = NotificationManagerCompat.from( cancelTimerForNotification(notification.id)
context buildNotification(notificationManager, notification)
)
val notificationsEnabled = notificationManager.areNotificationsEnabled() return notification.id
if (!notificationsEnabled) { }
invoke?.reject("Notifications not enabled on this device")
return null fun schedule(notification: Notification): Int {
} val notificationManager = NotificationManagerCompat.from(context)
return trigger(notificationManager, notification)
}
fun schedule(notifications: List<Notification>): List<Int> {
val ids = mutableListOf<Int>()
val notificationManager = NotificationManagerCompat.from(context)
for (notification in notifications) { for (notification in notifications) {
val id = notification.id val id = trigger(notificationManager, notification)
if (id == null) { ids.add(id)
invoke?.reject("Notification missing identifier")
return null
}
dismissVisibleNotification(id)
cancelTimerForNotification(id)
buildNotification(notificationManager, notification, invoke)
ids.put(id)
} }
return ids return ids
} }
@ -152,12 +153,8 @@ class TauriNotificationManager(
private fun buildNotification( private fun buildNotification(
notificationManager: NotificationManagerCompat, notificationManager: NotificationManagerCompat,
notification: Notification, notification: Notification,
invoke: Invoke?
) { ) {
var channelId = DEFAULT_NOTIFICATION_CHANNEL_ID val channelId = notification.channelId ?: DEFAULT_NOTIFICATION_CHANNEL_ID
if (notification.channelId != null) {
channelId = notification.channelId!!
}
val mBuilder = NotificationCompat.Builder( val mBuilder = NotificationCompat.Builder(
context, channelId context, channelId
) )
@ -213,8 +210,7 @@ class TauriNotificationManager(
try { try {
mBuilder.color = Color.parseColor(iconColor) mBuilder.color = Color.parseColor(iconColor)
} catch (ex: IllegalArgumentException) { } catch (ex: IllegalArgumentException) {
invoke?.reject("Invalid color provided. Must be a hex string (ex: #ff0000") throw Exception("Invalid color provided. Must be a hex string (ex: #ff0000")
return
} }
} }
createActionIntents(notification, mBuilder) createActionIntents(notification, mBuilder)
@ -427,9 +423,7 @@ class TauriNotificationManager(
} }
fun areNotificationsEnabled(): Boolean { fun areNotificationsEnabled(): Boolean {
val notificationManager = NotificationManagerCompat.from( val notificationManager = NotificationManagerCompat.from(context)
context
)
return notificationManager.areNotificationsEnabled() return notificationManager.areNotificationsEnabled()
} }

@ -33,8 +33,8 @@ use mobile::Notification;
#[derive(Debug, Default, Serialize, Deserialize)] #[derive(Debug, Default, Serialize, Deserialize)]
struct NotificationData { struct NotificationData {
/// Notification id. /// Notification id.
#[serde(default)] #[serde(default = "default_id")]
id: usize, id: i32,
/// The notification title. /// The notification title.
title: Option<String>, title: Option<String>,
/// The notification body. /// The notification body.
@ -43,6 +43,10 @@ struct NotificationData {
icon: Option<String>, icon: Option<String>,
} }
fn default_id() -> i32 {
rand::random()
}
/// The notification builder. /// The notification builder.
#[derive(Debug)] #[derive(Debug)]
pub struct NotificationBuilder<R: Runtime> { pub struct NotificationBuilder<R: Runtime> {

@ -31,7 +31,8 @@ pub fn init<R: Runtime, C: DeserializeOwned>(
impl<R: Runtime> crate::NotificationBuilder<R> { impl<R: Runtime> crate::NotificationBuilder<R> {
pub fn show(self) -> crate::Result<()> { pub fn show(self) -> crate::Result<()> {
self.handle self.handle
.run_mobile_plugin("show", self.data) .run_mobile_plugin::<ShowResponse>("show", self.data)
.map(|_| ())
.map_err(Into::into) .map_err(Into::into)
} }
} }
@ -59,6 +60,12 @@ impl<R: Runtime> Notification<R> {
} }
} }
#[derive(Deserialize)]
struct ShowResponse {
#[allow(dead_code)]
id: i32,
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct PermissionResponse { struct PermissionResponse {

Loading…
Cancel
Save