Search in sources :

Example 21 with AnalyticsEvent

use of com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent in project aws-sdk-android by aws-amplify.

the class NotificationClientBase method handleNotificationReceived.

/**
 * Handles Pinpoint push messages by posting a local notification when
 * the app is in the background, or sending a local broadcast if the app is
 * in the foreground. Also on Api level 19 devices and above, if local
 * notifications have been disabled and the app is in the background, a
 * local broadcast is sent.
 *
 * @param notificationDetails the notification message received by the device's messaging service
 * @return {@link NotificationClient.PushResult}.
 */
public NotificationClient.PushResult handleNotificationReceived(NotificationDetails notificationDetails) {
    final EventSourceType eventSourceType = EventSourceType.getEventSourceType(notificationDetails.getBundle());
    if (eventSourceType.isUnkown()) {
        return NotificationClient.PushResult.NOT_HANDLED;
    }
    final Bundle bundle = notificationDetails.getBundle();
    Map<String, String> eventSourceAttributes = eventSourceType.getAttributeParser().parseAttributes(bundle);
    if (eventSourceAttributes.isEmpty()) {
        return NotificationClient.PushResult.NOT_HANDLED;
    }
    final boolean isAppInForeground = appUtil.isAppInForeground();
    final String from = notificationDetails.getFrom();
    final Class<?> intentReceiver = notificationDetails.getTargetClass();
    final String imageUrl = bundle.getString(PINPOINT_IMAGE_PUSH_KEY);
    final String imageIconUrl = bundle.getString(PINPOINT_IMAGE_ICON_PUSH_KEY);
    final String imageSmallIconUrl = bundle.getString(PINPOINT_IMAGE_SMALL_ICON_PUSH_KEY);
    String intentAction = notificationDetails.getIntentAction();
    notificationChannelId = notificationDetails.getNotificationChannelId();
    log.info("Event source Attributes are:" + eventSourceAttributes);
    // User clicked on the notification to open the app
    if (eventSourceType.getEventTypeOpenend().equals(from)) {
        return this.handleNotificationOpen(eventSourceAttributes, bundle);
    }
    final AnalyticsEvent pushEvent;
    this.pinpointContext.getAnalyticsClient().updateEventSourceGlobally(eventSourceAttributes);
    if (isAppInForeground) {
        pushEvent = this.pinpointContext.getAnalyticsClient().createEvent(eventSourceType.getEventTypeReceivedForeground());
    } else {
        pushEvent = this.pinpointContext.getAnalyticsClient().createEvent(eventSourceType.getEventTypeReceivedBackground());
    }
    pushEvent.addAttribute("isAppInForeground", Boolean.toString(isAppInForeground));
    try {
        // notifications in the foreground.
        if (!pinpointContext.getPinpointConfiguration().getShouldPostNotificationsInForeground() && isAppInForeground) {
            // Notify the caller that the app was in the foreground.
            return NotificationClient.PushResult.APP_IN_FOREGROUND;
        } else {
            // image, and default sound.
            if ("1".equalsIgnoreCase(bundle.getString(NOTIFICATION_SILENT_PUSH_KEY))) {
                return NotificationClient.PushResult.SILENT;
            }
            // notification in the notification center.,
            if (!areAppNotificationsEnabled() || !displayNotification(bundle, intentReceiver, imageUrl, imageIconUrl, imageSmallIconUrl, eventSourceAttributes, intentAction, // campaign_id, journey_id, others
            eventSourceType.getEventSourceIdAttributeKey(), eventSourceType.getEventSourceActivityIdAttributeKey())) {
                // campaign_activity_id, journey_id, others
                // Local app notifications have been disabled by the
                // user from Settings -> App Info
                // or we couldn't display the notification for some
                // reason.
                pushEvent.addAttribute("isOptedOut", "true");
                // passed in handler.
                return NotificationClient.PushResult.OPTED_OUT;
            }
        }
    } finally {
        this.pinpointContext.getAnalyticsClient().recordEvent(pushEvent);
        this.pinpointContext.getAnalyticsClient().submitEvents();
    }
    return NotificationClient.PushResult.POSTED_NOTIFICATION;
}
Also used : AnalyticsEvent(com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent) Bundle(android.os.Bundle)

Example 22 with AnalyticsEvent

use of com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent in project aws-sdk-android by aws-amplify.

the class PinpointDBUtilTest method testInsertSingleEvent.

@Test
public void testInsertSingleEvent() throws Exception {
    AnalyticsEvent analyticsEvent = AnalyticsEvent.newInstance(mockContext, SESSION_ID, SESSION_START, SESSION_END, SESSION_DURATION, TIME_STAMP, EVENT_NAME);
    Uri uri = dbUtil.saveEvent(analyticsEvent);
    int idInserted = Integer.parseInt(uri.getLastPathSegment());
    assertNotEquals(idInserted, 0);
}
Also used : AnalyticsEvent(com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent) Uri(android.net.Uri) Test(org.junit.Test)

Example 23 with AnalyticsEvent

use of com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent in project aws-sdk-android by aws-amplify.

the class PinpointDBUtilTest method testQueryById.

@Test
public void testQueryById() {
    AnalyticsEvent analyticsEvent = AnalyticsEvent.newInstance(mockContext, SESSION_ID, SESSION_START, SESSION_END, SESSION_DURATION, TIME_STAMP, EVENT_NAME);
    Uri uri = dbUtil.saveEvent(analyticsEvent);
    int idInserted = Integer.parseInt(uri.getLastPathSegment());
    assertNotEquals(idInserted, 0);
    Cursor c = dbUtil.queryEventById(idInserted);
    assertNotNull(c);
    assertEquals(c.getCount(), 1);
    c.close();
}
Also used : AnalyticsEvent(com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent) Cursor(android.database.Cursor) Uri(android.net.Uri) Test(org.junit.Test)

Example 24 with AnalyticsEvent

use of com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent in project aws-sdk-android by aws-amplify.

the class ADMNotificationClientTest method testHandleADMPinpointMessageOpened.

@Test
public void testHandleADMPinpointMessageOpened() throws JSONException {
    NotificationClient.CampaignPushResult result = target.handleCampaignPush(buildNotificationDetails("_campaign.opened_notification"));
    assertEquals(NotificationClient.CampaignPushResult.NOTIFICATION_OPENED, result);
    ArgumentCaptor<AnalyticsEvent> eventCaptor = ArgumentCaptor.forClass(AnalyticsEvent.class);
    verify(mockEventRecorder, times(1)).recordEvent(eventCaptor.capture());
    final AnalyticsEvent receivedEvent = eventCaptor.getAllValues().get(0);
    assertThat(receivedEvent.getEventType(), is("_campaign.opened_notification"));
    assertTrue(receivedEvent.getEventTimestamp() > 0);
    assertThat(receivedEvent.getAllAttributes().size(), is(3));
    assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule"));
    assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn"));
    assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please"));
    assertThat(receivedEvent.getAllMetrics().size(), is(0));
}
Also used : AnalyticsEvent(com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent) Test(org.junit.Test)

Example 25 with AnalyticsEvent

use of com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent in project aws-sdk-android by aws-amplify.

the class ADMNotificationClientTest method testADMMessageReceivedDefaultDoNotPostNotificationInForeground.

@Test
public void testADMMessageReceivedDefaultDoNotPostNotificationInForeground() throws JSONException {
    // Force the app to be in the background.
    final AppUtil appUtil = Mockito.mock(AppUtil.class);
    Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil);
    Mockito.when(appUtil.isAppInForeground()).thenReturn(false);
    NotificationClient.CampaignPushResult pushResult = target.handleCampaignPush(buildNotificationDetails());
    ArgumentCaptor<AnalyticsEvent> eventCaptor = ArgumentCaptor.forClass(AnalyticsEvent.class);
    verify(mockEventRecorder, times(1)).recordEvent(eventCaptor.capture());
    final AnalyticsEvent receivedEvent = eventCaptor.getAllValues().get(0);
    assertThat(receivedEvent.getEventType(), is("_campaign.received_background"));
    assertTrue(receivedEvent.getEventTimestamp() > 0);
    for (Map.Entry<String, String> entry : receivedEvent.getAllAttributes().entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }
    assertThat(receivedEvent.getAllAttributes().size(), is(5));
    assertThat(receivedEvent.getAttribute("isOptedOut"), is("true"));
    assertThat(receivedEvent.getAttribute("isAppInForeground"), is("false"));
    assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule"));
    assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please"));
    assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn"));
    // optOut is true because this test can't get the app icon resource id.
    assertThat(receivedEvent.getAllMetrics().size(), is(0));
    assertTrue(pushResult.equals(NotificationClient.CampaignPushResult.OPTED_OUT));
}
Also used : AnalyticsEvent(com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent) Map(java.util.Map) Test(org.junit.Test)

Aggregations

AnalyticsEvent (com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsEvent)41 Test (org.junit.Test)27 Map (java.util.Map)16 AnalyticsClient (com.amazonaws.mobileconnectors.pinpoint.analytics.AnalyticsClient)7 HashMap (java.util.HashMap)6 AnalyticsContextBuilder (com.amazonaws.mobileconnectors.pinpoint.analytics.utils.AnalyticsContextBuilder)5 PinpointContext (com.amazonaws.mobileconnectors.pinpoint.internal.core.PinpointContext)5 Before (org.junit.Before)5 Uri (android.net.Uri)4 Cursor (android.database.Cursor)3 Event (com.amazonaws.services.pinpoint.model.Event)2 JSONObject (org.json.JSONObject)2 Service (android.app.Service)1 Bundle (android.os.Bundle)1 MockAppDetails (com.amazonaws.mobileconnectors.pinpoint.internal.core.system.MockAppDetails)1 EventsBatch (com.amazonaws.services.pinpoint.model.EventsBatch)1 PublicEndpoint (com.amazonaws.services.pinpoint.model.PublicEndpoint)1 PutEventsRequest (com.amazonaws.services.pinpoint.model.PutEventsRequest)1 Locale (java.util.Locale)1 JSONException (org.json.JSONException)1