From 0bb75c24f08680b6e301b50d06bd03cc056f9f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Kre=C5=A1i=C4=87?= Date: Wed, 7 Aug 2024 08:24:51 +0000 Subject: [PATCH] Pull request #76: Ikresic MM-6561 add custompayload to the action momessage Merge in MML/infobip-mobile-messaging-huawei from ikresic-MM-6561-add-custompayload-to-the-action-momessage to master Squashed commit of the following: commit 8ca7ca84b7a4aa9eb4349d64b6680c45e4a4d776 Author: ikresic Date: Fri Aug 2 16:36:00 2024 +0200 - provided customPayload to the MOmessage commit 0d2a4620977546b06de080d75a456a39656fa6f5 Author: ikresic Date: Fri Aug 2 15:33:42 2024 +0200 - provided customPayload to the MOmessage commit 23548d679128087175050486720c6998689f1cdb Author: ikresic Date: Fri Aug 2 14:38:16 2024 +0200 - provided customPayload to the MOmessage --- .../NotificationActionTapReceiverTest.java | 36 +++++++++++++++++++ .../messages/MoMessageSenderTest.java | 33 +++++++++++++++++ .../interactive/MobileInteractiveImpl.java | 1 + 3 files changed, 70 insertions(+) diff --git a/infobip-mobile-messaging-huawei-sdk/src/androidTest/java/org/infobip/mobile/messaging/interactive/notification/NotificationActionTapReceiverTest.java b/infobip-mobile-messaging-huawei-sdk/src/androidTest/java/org/infobip/mobile/messaging/interactive/notification/NotificationActionTapReceiverTest.java index c8f077e0..e0faa8c8 100644 --- a/infobip-mobile-messaging-huawei-sdk/src/androidTest/java/org/infobip/mobile/messaging/interactive/notification/NotificationActionTapReceiverTest.java +++ b/infobip-mobile-messaging-huawei-sdk/src/androidTest/java/org/infobip/mobile/messaging/interactive/notification/NotificationActionTapReceiverTest.java @@ -14,6 +14,8 @@ import org.infobip.mobile.messaging.interactive.platform.InteractiveBroadcaster; import org.infobip.mobile.messaging.interactive.platform.MockActivity; import org.infobip.mobile.messaging.interactive.tools.MobileMessagingTestCase; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; @@ -33,6 +35,7 @@ public class NotificationActionTapReceiverTest extends MobileMessagingTestCase { private ArgumentCaptor messageArgumentCaptor; private NotificationActionTapReceiver notificationActionTapReceiver; private ActivityStarterWrapper activityStarterWrapper; + private JSONObject userInfo; @Override public void setUp() throws Exception { @@ -56,6 +59,32 @@ public void setUp() throws Exception { //noinspection WrongConstant Mockito.when(contextMock.getSystemService(eq(Context.NOTIFICATION_SERVICE))).thenReturn(notificationManagerMock); + + try { + userInfo = new JSONObject(); + userInfo.put("messageId", "random-message-id-123456"); + + JSONObject aps = new JSONObject(); + JSONObject alert = new JSONObject(); + alert.put("body", "text"); + aps.put("alert", alert); + aps.put("category", "categoryId"); + userInfo.put("aps", aps); + + JSONObject customPayload = new JSONObject(); + customPayload.put("key", "value"); + + JSONObject nestedObject = new JSONObject(); + nestedObject.put("key", "value"); + customPayload.put("nestedObject", nestedObject); + + customPayload.put("nullAttribute", JSONObject.NULL); + customPayload.put("numberAttribute", 123); + + userInfo.put("customPayload", customPayload); + } catch (JSONException e) { + e.printStackTrace(); + } } @Test @@ -66,7 +95,9 @@ public void test_should_send_notification_action_clicked_event() throws Exceptio NotificationCategory givenNotificationCategory = givenNotificationCategory(givenTappedNotificationAction); Message givenMessage = createMessage(context, "SomeMessageId", givenNotificationCategory.getCategoryId(), false); int givenNotificationId = 1234; + givenMessage.setCustomPayload(userInfo); Intent givenIntent = givenIntent(givenMessage, givenNotificationCategory, givenTappedNotificationAction, givenNotificationId); + givenMessage.setCustomPayload(userInfo); // When notificationActionTapReceiver.onReceive(contextMock, givenIntent); @@ -92,8 +123,10 @@ public void test_should_send_action_clicked_event_and_open_activity_if_action_sh NotificationCategory givenNotificationCategory = givenNotificationCategory(givenTappedNotificationAction); Message givenMessage = createMessage(context, "SomeMessageId", givenNotificationCategory.getCategoryId(), false); int givenNotificationId = 1234; + givenMessage.setCustomPayload(userInfo); Intent givenIntent = givenIntent(givenMessage, givenNotificationCategory, givenTappedNotificationAction, givenNotificationId); Mockito.when(broadcastSender.notificationActionTapped(any(Message.class), any(NotificationCategory.class), any(NotificationAction.class))).thenReturn(givenIntent); + givenMessage.setCustomPayload(userInfo); // When notificationActionTapReceiver.onReceive(contextMock, givenIntent); @@ -119,8 +152,10 @@ public void test_should_send_action_clicked_event_and_not_open_activity_if_actio NotificationCategory givenNotificationCategory = givenNotificationCategory(givenTappedNotificationAction); Message givenMessage = createMessage(context, "SomeMessageId", givenNotificationCategory.getCategoryId(), false); int givenNotificationId = 1234; + givenMessage.setCustomPayload(userInfo); Intent givenIntent = givenIntent(givenMessage, givenNotificationCategory, givenTappedNotificationAction, givenNotificationId); Mockito.when(broadcastSender.notificationActionTapped(any(Message.class), any(NotificationCategory.class), any(NotificationAction.class))).thenReturn(givenIntent); + givenMessage.setCustomPayload(userInfo); // When notificationActionTapReceiver.onReceive(contextMock, givenIntent); @@ -167,6 +202,7 @@ private void verifyProperPayloadWasSentAndActionsTriggered(NotificationAction gi assertJEquals(givenTappedNotificationAction, actualAction); assertJEquals(givenNotificationCategory, actualCategory); assertJEquals(givenMessage, actualMessage); + assertJEquals(givenMessage.getCustomPayload(), actualMessage.getCustomPayload()); Mockito.verify(mobileInteractive, Mockito.times(1)).triggerSdkActionsFor(actualAction, actualMessage); } diff --git a/infobip-mobile-messaging-huawei-sdk/src/androidTest/java/org/infobip/mobile/messaging/mobileapi/messages/MoMessageSenderTest.java b/infobip-mobile-messaging-huawei-sdk/src/androidTest/java/org/infobip/mobile/messaging/mobileapi/messages/MoMessageSenderTest.java index 330730c8..7898b147 100644 --- a/infobip-mobile-messaging-huawei-sdk/src/androidTest/java/org/infobip/mobile/messaging/mobileapi/messages/MoMessageSenderTest.java +++ b/infobip-mobile-messaging-huawei-sdk/src/androidTest/java/org/infobip/mobile/messaging/mobileapi/messages/MoMessageSenderTest.java @@ -17,6 +17,8 @@ import org.infobip.mobile.messaging.storage.SQLiteMessageStore; import org.infobip.mobile.messaging.tools.MobileMessagingTestCase; import org.infobip.mobile.messaging.util.PreferenceHelper; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -130,6 +132,37 @@ public void shouldSendMultipleMessages() throws Exception { assertEquals(false, messages.get(1).getCustomPayload().opt("myBooleanKey")); } + @Test + public void testCustomPayloadNestedObjects() { + String jsonStr = "{\n" + + " \"messageId\": \"messageId\",\n" + + " \"aps\": {\n" + + " \"badge\": 6,\n" + + " \"sound\": \"default\",\n" + + " \"alert\": {\n" + + " \"bidy\":\"text\"\n" + + " }\n" + + " },\n" + + " \"customPayload\": {\n" + + " \"key\": \"value\",\n" + + " \"nestedObject\": {\n" + + " \"key\": \"value\"\n" + + " }\n" + + " }\n" + + "}"; + + JSONObject message = null; + JSONObject expectedCustomPayload = new JSONObject(); + try { + message = new JSONObject(jsonStr); + expectedCustomPayload.put("key", "value"); + expectedCustomPayload.put("nestedObject", new JSONObject().put("key", "value")); + assertEquals(expectedCustomPayload.toString(), message.getJSONObject("customPayload").toString()); + } catch (JSONException e) { + throw new RuntimeException(e); + } + } + @Test public void shouldOnlyKeepMessagesToBeRetried() { diff --git a/infobip-mobile-messaging-huawei-sdk/src/main/java/org/infobip/mobile/messaging/interactive/MobileInteractiveImpl.java b/infobip-mobile-messaging-huawei-sdk/src/main/java/org/infobip/mobile/messaging/interactive/MobileInteractiveImpl.java index 2a6f8d88..9ab199c6 100644 --- a/infobip-mobile-messaging-huawei-sdk/src/main/java/org/infobip/mobile/messaging/interactive/MobileInteractiveImpl.java +++ b/infobip-mobile-messaging-huawei-sdk/src/main/java/org/infobip/mobile/messaging/interactive/MobileInteractiveImpl.java @@ -137,6 +137,7 @@ private Message messageFor(final String categoryId, final NotificationAction act message.setBody(categoryId + " " + action.getId()); HashMap map = new HashMap<>(); map.put("initialMessageId", initialMessage.getMessageId()); + message.setCustomPayload(initialMessage.getCustomPayload()); message.setInternalData(InternalDataMapper.mergeExistingInternalDataWithAnythingToJson(initialMessage.getInternalData(), map)); return message; }