Skip to content

Commit

Permalink
Add inbox notification to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopsaheysa committed Jun 14, 2024
1 parent bd19a1d commit 0fb5826
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
3 changes: 3 additions & 0 deletions docs/Using-Push-Service.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,40 @@ 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
*/
@JvmStatic
@JvmName("parseNotification")
fun parseNotification(notificationData: Map<String, String>): 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<String, String>): 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<String, String>): 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<String, String>): PushMessage? {
val inboxId = notificationData["inboxId"] ?: return null
return PushMessageInboxReceived(inboxId, notificationData)
}
}
}
Expand Down Expand Up @@ -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<String, String>
): PushMessage(originalData)
8 changes: 7 additions & 1 deletion library/src/test/java/PushTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>()
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)
}
Expand Down

0 comments on commit 0fb5826

Please sign in to comment.