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

Added inbox notification to parser #150

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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,42 @@ 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? {
val messageType = notificationData["messageType"] ?: return null

return when (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 +142,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
Loading