Skip to content

Commit cd60e8d

Browse files
tjleingThomas Leing
andauthored
Fix NPE if Pinpoint configured without event collection (#3117)
Co-authored-by: Thomas Leing <[email protected]>
1 parent 97ad78e commit cd60e8d

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

aws-android-sdk-pinpoint/src/main/java/com/amazonaws/mobileconnectors/pinpoint/targeting/notification/NotificationClientBase.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -908,14 +908,16 @@ public NotificationClient.PushResult handleNotificationReceived(NotificationDeta
908908
return this.handleNotificationOpen(eventSourceAttributes, bundle);
909909
}
910910

911-
final AnalyticsEvent pushEvent;
912-
this.pinpointContext.getAnalyticsClient().updateEventSourceGlobally(eventSourceAttributes);
913-
if (isAppInForeground) {
914-
pushEvent = this.pinpointContext.getAnalyticsClient().createEvent(eventSourceType.getEventTypeReceivedForeground());
915-
} else {
916-
pushEvent = this.pinpointContext.getAnalyticsClient().createEvent(eventSourceType.getEventTypeReceivedBackground());
911+
AnalyticsEvent pushEvent = null;
912+
if (this.pinpointContext.getAnalyticsClient() != null) {
913+
this.pinpointContext.getAnalyticsClient().updateEventSourceGlobally(eventSourceAttributes);
914+
if (isAppInForeground) {
915+
pushEvent = this.pinpointContext.getAnalyticsClient().createEvent(eventSourceType.getEventTypeReceivedForeground());
916+
} else {
917+
pushEvent = this.pinpointContext.getAnalyticsClient().createEvent(eventSourceType.getEventTypeReceivedBackground());
918+
}
919+
pushEvent.addAttribute("isAppInForeground", Boolean.toString(isAppInForeground));
917920
}
918-
pushEvent.addAttribute("isAppInForeground", Boolean.toString(isAppInForeground));
919921
try {
920922
// Ignore whether the app is in the foreground if the configuration indicates it should post
921923
// notifications in the foreground.
@@ -944,15 +946,19 @@ public NotificationClient.PushResult handleNotificationReceived(NotificationDeta
944946
// user from Settings -> App Info
945947
// or we couldn't display the notification for some
946948
// reason.
947-
pushEvent.addAttribute("isOptedOut", "true");
949+
if (pushEvent != null) {
950+
pushEvent.addAttribute("isOptedOut", "true");
951+
}
948952
// We can't post a notification, so delegate to the
949953
// passed in handler.
950954
return NotificationClient.PushResult.OPTED_OUT;
951955
}
952956
}
953957
} finally {
954-
this.pinpointContext.getAnalyticsClient().recordEvent(pushEvent);
955-
this.pinpointContext.getAnalyticsClient().submitEvents();
958+
if (pushEvent != null) {
959+
this.pinpointContext.getAnalyticsClient().recordEvent(pushEvent);
960+
this.pinpointContext.getAnalyticsClient().submitEvents();
961+
}
956962
}
957963

958964
return NotificationClient.PushResult.POSTED_NOTIFICATION;

0 commit comments

Comments
 (0)