From d3e491689dfd06374a5ba10d4162a316356a1304 Mon Sep 17 00:00:00 2001 From: Maxim Belov Date: Fri, 19 Apr 2024 06:22:35 +0300 Subject: [PATCH 1/3] handle notifications from a cold start --- cordova-airship/src/android/AirshipCordova.kt | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/cordova-airship/src/android/AirshipCordova.kt b/cordova-airship/src/android/AirshipCordova.kt index b2e92ff5..7d0fc032 100644 --- a/cordova-airship/src/android/AirshipCordova.kt +++ b/cordova-airship/src/android/AirshipCordova.kt @@ -2,13 +2,16 @@ package com.urbanairship.cordova +import android.annotation.SuppressLint import android.content.Context +import android.content.Intent import android.os.Build import com.urbanairship.Autopilot import com.urbanairship.PendingResult import com.urbanairship.UALog import com.urbanairship.actions.ActionResult import com.urbanairship.android.framework.proxy.EventType +import com.urbanairship.android.framework.proxy.Utils import com.urbanairship.android.framework.proxy.events.EventEmitter import com.urbanairship.android.framework.proxy.proxies.AirshipProxy import com.urbanairship.android.framework.proxy.proxies.FeatureFlagProxy @@ -16,6 +19,10 @@ import com.urbanairship.json.JsonList import com.urbanairship.json.JsonMap import com.urbanairship.json.JsonSerializable import com.urbanairship.json.JsonValue +import com.urbanairship.json.jsonMapOf +import com.urbanairship.push.NotificationInfo +import com.urbanairship.push.PushManager +import com.urbanairship.push.PushMessage import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob @@ -37,6 +44,7 @@ class AirshipCordova : CordovaPlugin() { ) private var listeners: MutableMap> = mutableMapOf() + private val notificationStack: MutableMap> = mutableMapOf() companion object { private val EVENT_NAME_MAP = mapOf( @@ -87,6 +95,33 @@ class AirshipCordova : CordovaPlugin() { notifyPendingEvents() } } + + cordova.getThreadPool().execute { + try { + notifyListeners( + EventType.BACKGROUND_NOTIFICATION_RESPONSE_RECEIVED, + cordova.getActivity().intent + ) + } catch (e: java.lang.Exception) { + val msg = e.toString() + UALog.e { msg } + } + } + + } + + override fun onNewIntent(intent: Intent?) { + try { + super.onNewIntent(intent) + notifyListeners( + EventType.BACKGROUND_NOTIFICATION_RESPONSE_RECEIVED, + intent!! + ) + } catch (e: java.lang.Exception) { + val msg = e.toString() + UALog.e { msg } + + } } override fun onReset() { @@ -97,6 +132,34 @@ class AirshipCordova : CordovaPlugin() { override fun onDestroy() { super.onDestroy() this.listeners.clear() + this.notificationStack.clear() + } + + @SuppressLint("RestrictedApi") + private fun notifyListeners(eventType: EventType, intent: Intent) { + val data = intent.extras + if (data != null && data.containsKey(PushManager.EXTRA_PUSH_MESSAGE_BUNDLE)) { + val dataString = data.toString(); + UALog.i { "Notification message on new intent: $dataString" } + val message = PushMessage.fromIntent(intent) ?: return + val id: Int = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, -1) + val tag: String? = intent.getStringExtra(PushManager.EXTRA_NOTIFICATION_TAG) + val notificationInfo = NotificationInfo(message, id, tag) + val listeners = this.listeners[eventType]; + val pluginResult = jsonMapOf( + "pushPayload" to Utils.notificationMap( + notificationInfo.message, + ), + ).pluginResult() + if (listeners?.isNotEmpty() == true) { + listeners.forEach { listener -> + pluginResult.keepCallback = true + listener.callbackContext.sendPluginResult(pluginResult) + } + } else { + this.notificationStack.getOrPut(eventType) { mutableListOf() }.add(notificationInfo) + } + } } private fun addListener(args: JSONArray, callbackContext: CallbackContext) { @@ -117,6 +180,18 @@ class AirshipCordova : CordovaPlugin() { ) this.listeners.getOrPut(event) { mutableListOf() }.add(listener) + + this.notificationStack[event]?.forEach { notificationInfo -> + val pluginResult = jsonMapOf( + "pushPayload" to Utils.notificationMap( + notificationInfo.message, + ), + ).pluginResult() + pluginResult.keepCallback = true + listener.callbackContext.sendPluginResult(pluginResult) + this.notificationStack[event]?.remove(notificationInfo) + } + notifyPendingEvents() } From 5446425546a0da0645aab7ac86612d3aecdf693f Mon Sep 17 00:00:00 2001 From: Maxim Belov Date: Fri, 19 Apr 2024 13:49:26 +0300 Subject: [PATCH 2/3] Add onBackgroundPushReceived method --- Example/index.html | 4 ++++ cordova-airship/types/index.d.ts | 10 ++++++++++ cordova-airship/www/Airship.js | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/Example/index.html b/Example/index.html index a65001c5..e8b7ca9e 100644 --- a/Example/index.html +++ b/Example/index.html @@ -34,6 +34,10 @@ console.log("onPushReceived: " + JSON.stringify(event)) }) + Airship.push.onBackgroundPushReceived(function(event) { + console.log("onBackgroundPushReceived: " + JSON.stringify(event)) + }) + Airship.push.onNotificationResponse(function(event) { console.log("onNotificationResponse: " + JSON.stringify(event)) }) diff --git a/cordova-airship/types/index.d.ts b/cordova-airship/types/index.d.ts index 74a39ae1..be31b5e8 100644 --- a/cordova-airship/types/index.d.ts +++ b/cordova-airship/types/index.d.ts @@ -1082,6 +1082,16 @@ export interface AirshipPush { callback: (event: PushReceivedEvent) => void ): Cancellable + /** + * Background push received listener. + * + * @param callback The callback. + * @return A cancellable that can be used to cancel the listener. + */ + onBackgroundPushReceived( + callback: (event: PushReceivedEvent) => void + ): Cancellable + /** * Notification response listener. * diff --git a/cordova-airship/www/Airship.js b/cordova-airship/www/Airship.js index 7ff42640..76895a82 100644 --- a/cordova-airship/www/Airship.js +++ b/cordova-airship/www/Airship.js @@ -377,6 +377,11 @@ airship.push.onPushReceived = function (callback) { return registerListener("airship.event.push_received", callback) } +airship.push.onBackgroundPushReceived = function (callback) { + argscheck.checkArgs('F', 'Airship.push.onBackgroundPushReceived', arguments) + return registerListener("airship.event.background_push_received", callback) +} + airship.push.onNotificationResponse = function (callback) { argscheck.checkArgs('F', 'Airship.push.onNotificationResponse', arguments) return registerListener("airship.event.notification_response", callback) From 93ebe8590a9bc4b1b6310186df083d7c89228129 Mon Sep 17 00:00:00 2001 From: Maxim Belov Date: Fri, 19 Apr 2024 13:53:39 +0300 Subject: [PATCH 3/3] Revert "handle notifications from a cold start" This reverts commit d3e491689dfd06374a5ba10d4162a316356a1304. --- cordova-airship/src/android/AirshipCordova.kt | 75 ------------------- 1 file changed, 75 deletions(-) diff --git a/cordova-airship/src/android/AirshipCordova.kt b/cordova-airship/src/android/AirshipCordova.kt index 7d0fc032..b2e92ff5 100644 --- a/cordova-airship/src/android/AirshipCordova.kt +++ b/cordova-airship/src/android/AirshipCordova.kt @@ -2,16 +2,13 @@ package com.urbanairship.cordova -import android.annotation.SuppressLint import android.content.Context -import android.content.Intent import android.os.Build import com.urbanairship.Autopilot import com.urbanairship.PendingResult import com.urbanairship.UALog import com.urbanairship.actions.ActionResult import com.urbanairship.android.framework.proxy.EventType -import com.urbanairship.android.framework.proxy.Utils import com.urbanairship.android.framework.proxy.events.EventEmitter import com.urbanairship.android.framework.proxy.proxies.AirshipProxy import com.urbanairship.android.framework.proxy.proxies.FeatureFlagProxy @@ -19,10 +16,6 @@ import com.urbanairship.json.JsonList import com.urbanairship.json.JsonMap import com.urbanairship.json.JsonSerializable import com.urbanairship.json.JsonValue -import com.urbanairship.json.jsonMapOf -import com.urbanairship.push.NotificationInfo -import com.urbanairship.push.PushManager -import com.urbanairship.push.PushMessage import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob @@ -44,7 +37,6 @@ class AirshipCordova : CordovaPlugin() { ) private var listeners: MutableMap> = mutableMapOf() - private val notificationStack: MutableMap> = mutableMapOf() companion object { private val EVENT_NAME_MAP = mapOf( @@ -95,33 +87,6 @@ class AirshipCordova : CordovaPlugin() { notifyPendingEvents() } } - - cordova.getThreadPool().execute { - try { - notifyListeners( - EventType.BACKGROUND_NOTIFICATION_RESPONSE_RECEIVED, - cordova.getActivity().intent - ) - } catch (e: java.lang.Exception) { - val msg = e.toString() - UALog.e { msg } - } - } - - } - - override fun onNewIntent(intent: Intent?) { - try { - super.onNewIntent(intent) - notifyListeners( - EventType.BACKGROUND_NOTIFICATION_RESPONSE_RECEIVED, - intent!! - ) - } catch (e: java.lang.Exception) { - val msg = e.toString() - UALog.e { msg } - - } } override fun onReset() { @@ -132,34 +97,6 @@ class AirshipCordova : CordovaPlugin() { override fun onDestroy() { super.onDestroy() this.listeners.clear() - this.notificationStack.clear() - } - - @SuppressLint("RestrictedApi") - private fun notifyListeners(eventType: EventType, intent: Intent) { - val data = intent.extras - if (data != null && data.containsKey(PushManager.EXTRA_PUSH_MESSAGE_BUNDLE)) { - val dataString = data.toString(); - UALog.i { "Notification message on new intent: $dataString" } - val message = PushMessage.fromIntent(intent) ?: return - val id: Int = intent.getIntExtra(PushManager.EXTRA_NOTIFICATION_ID, -1) - val tag: String? = intent.getStringExtra(PushManager.EXTRA_NOTIFICATION_TAG) - val notificationInfo = NotificationInfo(message, id, tag) - val listeners = this.listeners[eventType]; - val pluginResult = jsonMapOf( - "pushPayload" to Utils.notificationMap( - notificationInfo.message, - ), - ).pluginResult() - if (listeners?.isNotEmpty() == true) { - listeners.forEach { listener -> - pluginResult.keepCallback = true - listener.callbackContext.sendPluginResult(pluginResult) - } - } else { - this.notificationStack.getOrPut(eventType) { mutableListOf() }.add(notificationInfo) - } - } } private fun addListener(args: JSONArray, callbackContext: CallbackContext) { @@ -180,18 +117,6 @@ class AirshipCordova : CordovaPlugin() { ) this.listeners.getOrPut(event) { mutableListOf() }.add(listener) - - this.notificationStack[event]?.forEach { notificationInfo -> - val pluginResult = jsonMapOf( - "pushPayload" to Utils.notificationMap( - notificationInfo.message, - ), - ).pluginResult() - pluginResult.keepCallback = true - listener.callbackContext.sendPluginResult(pluginResult) - this.notificationStack[event]?.remove(notificationInfo) - } - notifyPendingEvents() }