diff --git a/docs/Using-Push-Service.md b/docs/Using-Push-Service.md index 8d56060..a1f3960 100644 --- a/docs/Using-Push-Service.md +++ b/docs/Using-Push-Service.md @@ -129,6 +129,9 @@ The `PushMessage` is an abstract class that is implemented by following classes - `name` of the operation - `result` of the operation (for example that the operation was canceled by the user). - `originalData` - data on which was the push message constructed +- `PushMessageInboxReceived` - a new inbox message was triggered with the id + - `id` of the message + - `originalData` - data on which was the push message constructed Example push notification processing: diff --git a/library/src/main/java/com/wultra/android/mtokensdk/push/PushParser.kt b/library/src/main/java/com/wultra/android/mtokensdk/push/PushParser.kt index c0c5576..5e6e409 100644 --- a/library/src/main/java/com/wultra/android/mtokensdk/push/PushParser.kt +++ b/library/src/main/java/com/wultra/android/mtokensdk/push/PushParser.kt @@ -26,7 +26,7 @@ class PushParser { /** * When you receive a push notification, you can test it here if it's a "WMT" notification. * - * Return type can be [PushMessageOperationCreated] or [PushMessageOperationFinished] + * Return type can be [PushMessageOperationCreated], [PushMessageOperationFinished] or [PushMessageInboxReceived] * * @param notificationData: data of received notification. * @return parsed known [PushMessage] or null @@ -34,21 +34,32 @@ class PushParser { @JvmStatic @JvmName("parseNotification") fun parseNotification(notificationData: Map): PushMessage? { + return when (notificationData["messageType"]) { + "mtoken.operationInit" -> parseOperationCreated(notificationData) + "mtoken.operationFinished" -> parseOperationFinished(notificationData) + "mtoken.inboxMessage.new" -> parseInboxMessageReceived(notificationData) + else -> null + } + } + // Helper methods + private fun parseOperationCreated(notificationData: Map): PushMessage? { val id = notificationData["operationId"] ?: return null val name = notificationData["operationName"] ?: return null + return PushMessageOperationCreated(id, name, notificationData) + } - return when (notificationData["messageType"]) { - "mtoken.operationInit" -> { - PushMessageOperationCreated(id, name, notificationData) - } - "mtoken.operationFinished" -> { - notificationData["mtokenOperationResult"]?.let { return PushMessageOperationFinished(id, name, PushMessageOperationFinished.parseResult(it), notificationData) } - } - else -> { - null - } - } + private fun parseOperationFinished(notificationData: Map): PushMessage? { + val id = notificationData["operationId"] ?: return null + val name = notificationData["operationName"] ?: return null + val result = notificationData["mtokenOperationResult"] ?: return null + val operationResult = PushMessageOperationFinished.parseResult(result) + return PushMessageOperationFinished(id, name, operationResult, notificationData) + } + + private fun parseInboxMessageReceived(notificationData: Map): PushMessage? { + val inboxId = notificationData["inboxId"] ?: return null + return PushMessageInboxReceived(inboxId, notificationData) } } } @@ -129,3 +140,14 @@ class PushMessageOperationFinished( } } } + +/** + * Created when a new inbox message was triggered. + */ +class PushMessageInboxReceived( + /** Id of the inbox message. */ + val id: String, + + /** Original data on which was the push message constructed. */ + originalData: Map +): PushMessage(originalData) diff --git a/library/src/test/java/PushTests.kt b/library/src/test/java/PushTests.kt index ac612ef..7504d95 100644 --- a/library/src/test/java/PushTests.kt +++ b/library/src/test/java/PushTests.kt @@ -102,11 +102,17 @@ class PushTests { Assert.assertNull(makePush("mtoken.operationFinished", "1", null, "authentication.success")) } - private fun makePush(type: String?, id: String?, name: String?, opResult: String?): PushMessage? { + @Test + fun `test inbox new message`() { + Assert.assertNotNull(makePush("mtoken.inboxMessage.new", null, null, null, "666")) + } + + private fun makePush(type: String?, id: String?, name: String?, opResult: String?, inboxId: String? = null): PushMessage? { val map = mutableMapOf() type?.let { map["messageType"] = it } id?.let { map["operationId"] = it } name?.let { map["operationName"] = it } + inboxId?.let { map["inboxId"] = it } opResult?.let { map["mtokenOperationResult"] = it } return PushParser.parseNotification(map) }