Skip to content

Commit

Permalink
Add event for remote and local notifications (close #478)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBenny committed Oct 8, 2021
1 parent 4274ef4 commit fb032b5
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 1 deletion.
Expand Up @@ -299,7 +299,7 @@ private void trackEvents() {
return;
}
TrackerEvents.trackAll(tracker);
eventsCreated += 10;
eventsCreated += 11;
final String made = "Made: " + eventsCreated;
runOnUiThread(() -> _eventsCreated.setText(made));
}
Expand Down
Expand Up @@ -18,6 +18,8 @@
import com.snowplowanalytics.snowplow.controller.TrackerController;
import com.snowplowanalytics.snowplow.event.AbstractPrimitive;
import com.snowplowanalytics.snowplow.event.DeepLinkReceived;
import com.snowplowanalytics.snowplow.event.MessageNotification;
import com.snowplowanalytics.snowplow.event.MessageNotificationTrigger;
import com.snowplowanalytics.snowplow.event.SelfDescribing;
import com.snowplowanalytics.snowplow.event.ConsentDocument;
import com.snowplowanalytics.snowplow.event.ConsentGranted;
Expand All @@ -30,6 +32,7 @@
import com.snowplowanalytics.snowplow.event.Timing;
import com.snowplowanalytics.snowplow.payload.SelfDescribingJson;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -52,6 +55,7 @@ public static void trackAll(@NonNull TrackerController tracker) {
trackEcommerceEvent(tracker);
trackConsentGranted(tracker);
trackConsentWithdrawn(tracker);
trackMessageNotification(tracker);
}

private static void trackDeepLink(TrackerController tracker) {
Expand Down Expand Up @@ -140,4 +144,18 @@ private static void trackConsentWithdrawn(TrackerController tracker) {
.build();
tracker.track(event);
}

private static void trackMessageNotification(TrackerController tracker) {
MessageNotification event = new MessageNotification("title", "body", MessageNotificationTrigger.push)
.notificationTimestamp("2020-12-31T15:59:60-08:00")
.category("category")
.action("action")
.bodyLocKey("loc key")
.bodyLocArgs(Arrays.asList("loc arg1", "loc arg2"))
.sound("chime.mp3")
.notificationCount(9)
.category("category1");

tracker.track(event);
}
}
@@ -0,0 +1,55 @@
package com.snowplowanalytics.snowplow.event;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RunWith(AndroidJUnit4.class)
public class MessageNotificationTest {

@Test
public void testExpectedForm() {
MessageNotification event = new MessageNotification("title", "body", MessageNotificationTrigger.push)
.notificationTimestamp("2020-12-31T15:59:60-08:00")
.action("action")
.bodyLocKey("loc key")
.bodyLocArgs(Arrays.asList("loc arg1", "loc arg2"))
.sound("chime.mp3")
.notificationCount(9)
.category("category1")
.attachments(Arrays.asList(new MessageNotificationAttachment("id", "type", "url")));

Map<String, Object> payload = event.getDataPayload();
assertNotNull(payload);
assertEquals("title", payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_TITLE));
assertEquals("body", payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_BODY));
assertEquals("push", payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_TRIGGER));
assertEquals("2020-12-31T15:59:60-08:00", payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_NOTIFICATIONTIMESTAMP));
assertEquals("action", payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_ACTION));
assertEquals("loc key", payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_BODYLOCKEY));
List<String> locArgs = (List<String>)payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_BODYLOCARGS);
assertNotNull(locArgs);
assertEquals(2, locArgs.size());
assertEquals("loc arg1", locArgs.get(0));
assertEquals("loc arg2", locArgs.get(1));
assertEquals("chime.mp3", payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_SOUND));
assertEquals(9, payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_NOTIFICATIONCOUNT));
assertEquals("category1", payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_CATEGORY));
List<MessageNotificationAttachment> attachments = (List<MessageNotificationAttachment>)payload.get(MessageNotification.PARAM_MESSAGENOTIFICATION_MESSAGENOTIFICATIONATTACHMENTS);
assertNotNull(attachments);
assertEquals(1, attachments.size());
MessageNotificationAttachment attachment = attachments.get(0);
assertEquals("id", attachment.get(MessageNotificationAttachment.PARAM_MESSAGENOTIFICATIONATTACHMENT_IDENTIFIER));
assertEquals("type", attachment.get(MessageNotificationAttachment.PARAM_MESSAGENOTIFICATIONATTACHMENT_TYPE));
assertEquals("url", attachment.get(MessageNotificationAttachment.PARAM_MESSAGENOTIFICATIONATTACHMENT_URL));
}
}

0 comments on commit fb032b5

Please sign in to comment.