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

Release 15.2.0 #428

Merged
merged 7 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 6 additions & 20 deletions cordova-airship/src/android/AirshipCordova.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AirshipCordova : CordovaPlugin() {
val callbackContext: CallbackContext
)

private var listeners: MutableMap<EventType, MutableList<Listener>> = mutableMapOf()
private var listeners: MutableMap<String, MutableList<Listener>> = mutableMapOf()

companion object {
private val EVENT_NAME_MAP = mapOf(
Expand All @@ -49,7 +49,7 @@ class AirshipCordova : CordovaPlugin() {
EventType.MESSAGE_CENTER_UPDATED to "airship.event.message_center_updated",
EventType.PUSH_TOKEN_RECEIVED to "airship.event.push_token_received",
EventType.FOREGROUND_PUSH_RECEIVED to "airship.event.push_received",
EventType.BACKGROUND_PUSH_RECEIVED to "airship.event.background_push_received",
EventType.BACKGROUND_PUSH_RECEIVED to "airship.event.push_received",
EventType.NOTIFICATION_STATUS_CHANGED to "airship.event.notification_status_changed"
)
}
Expand Down Expand Up @@ -103,44 +103,30 @@ class AirshipCordova : CordovaPlugin() {
val jsonArgs = JsonValue.wrap(args).requireList()

val eventName = jsonArgs.get(0).requireString()
val event: EventType = EVENT_NAME_MAP.firstNotNullOf {
if (it.value == eventName) {
it.key
} else {
null
}
}

val listener = Listener(
listenerId = jsonArgs.get(1).requireInt(),
callbackContext = callbackContext
)

this.listeners.getOrPut(event) { mutableListOf() }.add(listener)
this.listeners.getOrPut(eventName) { mutableListOf() }.add(listener)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to map to types, instead only use the map when finding the listener. This fixes background response and makes it so we can combine foreground/background push received

notifyPendingEvents()
}

private fun removeListener(args: JSONArray) {
val jsonArgs = JsonValue.wrap(args).requireList()

val eventName = jsonArgs.get(0).requireString()
val event: EventType = EVENT_NAME_MAP.firstNotNullOf {
if (it.value == eventName) {
it.key
} else {
null
}
}

val listenerId = jsonArgs.get(1).requireInt()
this.listeners[event]?.removeAll {
this.listeners[eventName]?.removeAll {
it.listenerId == listenerId
}
}

private fun notifyPendingEvents() {
EventType.values().forEach { eventType ->
val listeners = this.listeners[eventType]
val listeners = this.listeners[EVENT_NAME_MAP[eventType]]
if (listeners?.isNotEmpty() == true) {
EventEmitter.shared().processPending(listOf(eventType)) { event ->
listeners.forEach { listeners ->
Expand Down Expand Up @@ -396,7 +382,7 @@ internal fun JsonSerializable.pluginResult(): PluginResult {
val json = this.toJsonValue()

return when {
json.isNull -> PluginResult(PluginResult.Status.OK)
json.isNull -> PluginResult(PluginResult.Status.OK, null as String?)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do not set a value on Android cordova it will set the value based on the status message.

json.isString -> PluginResult(PluginResult.Status.OK, json.requireString())
json.isBoolean -> PluginResult(PluginResult.Status.OK, json.requireBoolean())
json.isInteger -> PluginResult(PluginResult.Status.OK, json.getInt(0))
Expand Down
5 changes: 5 additions & 0 deletions cordova-airship/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export interface PushTokenReceivedEvent {
*/
export interface PushReceivedEvent {
pushPayload: PushPayload;

/**
* Indicates whether the push was received when the application was in the background or foreground.
*/
isForeground: boolean;
rlepinski marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down