Skip to content

Commit e662aa2

Browse files
authored
Merge pull request #1416 from OneSignal/fix/null-appContext-on-getDeviceType
Check `NullPointerException` when `getDeviceType()`
2 parents 67a7f20 + 0957263 commit e662aa2

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSReceiveReceiptController.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,27 @@ synchronized public static OSReceiveReceiptController getInstance() {
5454
void sendReceiveReceipt(final CallbackToFutureAdapter.Completer<ListenableWorker.Result> callbackCompleter, @NonNull final String notificationId) {
5555
final String appId = OneSignal.appId == null || OneSignal.appId.isEmpty() ? OneSignal.getSavedAppId() : OneSignal.appId;
5656
final String playerId = OneSignal.getUserId();
57-
final int deviceType = new OSUtils().getDeviceType();
57+
Integer deviceType = null;
5858

5959
if (!remoteParamController.isReceiveReceiptEnabled()) {
6060
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "sendReceiveReceipt disable");
6161
endCallbackCompleterWithSuccess(callbackCompleter);
6262
return;
6363
}
6464

65+
try {
66+
deviceType = new OSUtils().getDeviceType();
67+
} catch (NullPointerException e) {
68+
e.printStackTrace();
69+
}
70+
71+
final Integer finalDeviceType = deviceType;
72+
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "OSReceiveReceiptController: Device Type is: " + finalDeviceType);
73+
6574
Runnable receiveReceiptRunnable = new Runnable() {
6675
@Override
6776
public void run() {
68-
repository.sendReceiveReceipt(appId, playerId, deviceType, notificationId, new OneSignalRestClient.ResponseHandler() {
77+
repository.sendReceiveReceipt(appId, playerId, finalDeviceType, notificationId, new OneSignalRestClient.ResponseHandler() {
6978
@Override
7079
void onSuccess(String response) {
7180
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "Receive receipt sent for notificationID: " + notificationId);

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSReceiveReceiptRepository.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
package com.onesignal;
2929

3030
import androidx.annotation.NonNull;
31+
import androidx.annotation.Nullable;
3132

3233
import org.json.JSONException;
3334
import org.json.JSONObject;
@@ -38,12 +39,15 @@ class OSReceiveReceiptRepository {
3839
private static final String PLAYER_ID = "player_id";
3940
private static final String DEVICE_TYPE = "device_type";
4041

41-
void sendReceiveReceipt(@NonNull String appId, @NonNull String playerId, @NonNull int deviceType, @NonNull String notificationId, @NonNull OneSignalRestClient.ResponseHandler responseHandler) {
42+
void sendReceiveReceipt(@NonNull String appId, @NonNull String playerId, @Nullable Integer deviceType, @NonNull String notificationId, @NonNull OneSignalRestClient.ResponseHandler responseHandler) {
4243
try {
4344
JSONObject jsonBody = new JSONObject()
4445
.put(APP_ID, appId)
45-
.put(PLAYER_ID, playerId)
46-
.put(DEVICE_TYPE, deviceType);
46+
.put(PLAYER_ID, playerId);
47+
48+
if (deviceType != null) {
49+
jsonBody.put(DEVICE_TYPE, deviceType);
50+
}
4751

4852
OneSignalRestClient.put("notifications/" + notificationId + "/report_received", jsonBody, responseHandler);
4953
} catch (JSONException e) {

0 commit comments

Comments
 (0)