Skip to content

Commit

Permalink
Merge pull request #880 from OneSignal/outcome_fix_hanging_awaits
Browse files Browse the repository at this point in the history
Updated callback scenarios to return null within the success callback
  • Loading branch information
jkasten2 authored Oct 24, 2019
2 parents b2b2f2f + 92842b6 commit cd97e20
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
16 changes: 10 additions & 6 deletions OneSignalSDK/app/src/main/java/com/onesignal/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -330,17 +331,19 @@ public void onFailure(OneSignal.EmailUpdateError error) {
public void onSendOutcomeClicked(View view) {
OneSignal.sendOutcome(outcomeName.getText().toString(), new OneSignal.OutcomeCallback() {
@Override
public void onSuccess(OutcomeEvent outcomeEvent) {
updateTextView(outcomeEvent.toString());
public void onSuccess(@Nullable OutcomeEvent outcomeEvent) {
if (outcomeEvent != null)
updateTextView(outcomeEvent.toString());
}
});
}

public void onSendUniqueOutcomeClicked(View view) {
OneSignal.sendUniqueOutcome(outcomeUnique.getText().toString(), new OneSignal.OutcomeCallback() {
@Override
public void onSuccess(OutcomeEvent outcomeEvent) {
updateTextView(outcomeEvent.toString());
public void onSuccess(@Nullable OutcomeEvent outcomeEvent) {
if (outcomeEvent != null)
updateTextView(outcomeEvent.toString());
}
});
}
Expand All @@ -351,8 +354,9 @@ public void onSendOutcomeWithValueClicked(View view) {

OneSignal.sendOutcomeWithValue(outcomeValueName.getText().toString(), Float.parseFloat(outcomeValue.getText().toString()), new OneSignal.OutcomeCallback() {
@Override
public void onSuccess(OutcomeEvent outcomeEvent) {
updateTextView(outcomeEvent.toString());
public void onSuccess(@Nullable OutcomeEvent outcomeEvent) {
if (outcomeEvent != null)
updateTextView(outcomeEvent.toString());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3149,8 +3149,14 @@ private static boolean isValidOutcomeEntry(String name) {
return true;
}

/**
* OutcomeEvent will be null in cases where the request was not sent:
* 1. OutcomeEvent cached already for re-attempt in future
* 2. Unique OutcomeEvent already sent for ATTRIBUTED session and notification(s)
* 3. Unique OutcomeEvent already sent for UNATTRIBUTED session during session
*/
public interface OutcomeCallback {
void onSuccess(OutcomeEvent outcomeEvent);
void onSuccess(@Nullable OutcomeEvent outcomeEvent);
}
/*
* End OneSignalOutcome module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ void sendUniqueOutcomeEvent(@NonNull final String name, @Nullable OneSignal.Outc
"\nSession: " + osSessionManager.getSession().toString() +
"\nOutcome name: " + name +
"\nnotificationIds: " + notificationIds);

// Return null within the callback to determine not a failure, but not a success in terms of the request made
if (callback != null)
callback.onSuccess(null);

return;
}

Expand All @@ -134,6 +139,11 @@ void sendUniqueOutcomeEvent(@NonNull final String name, @Nullable OneSignal.Outc
"Measure endpoint will not send because unique outcome already sent for: " +
"\nSession: " + osSessionManager.getSession().toString() +
"\nOutcome name: " + name);

// Return null within the callback to determine not a failure, but not a success in terms of the request made
if (callback != null)
callback.onSuccess(null);

return;
}

Expand Down Expand Up @@ -167,6 +177,7 @@ void onSuccess(String response) {
else
saveUnattributedUniqueOutcomeEvents();

// The only case where an actual success has occurred and the OutcomeEvent should be sent back
if (callback != null)
callback.onSuccess(outcomeEvent);
}
Expand All @@ -186,6 +197,10 @@ public void run() {
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.WARN,
"Sending outcome with name: " + name + " failed with status code: " + statusCode + " and response: " + response +
"\nOutcome event was cached and will be reattempted on app cold start");

// Return null within the callback to determine not a failure, but not a success in terms of the request made
if (callback != null)
callback.onSuccess(null);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import android.support.annotation.Nullable;

import com.onesignal.OneSignal.NotificationOpenedHandler;
import com.onesignal.OneSignal.NotificationReceivedHandler;
import com.onesignal.OneSignal.GetTagsHandler;
Expand Down Expand Up @@ -264,6 +266,42 @@ public void pauseInAppMessages(boolean pause) {
OneSignal.pauseInAppMessages(pause);
}

public void sendOutcome(String name) {
OneSignal.sendOutcome(name, new OneSignal.OutcomeCallback() {
@Override
public void onSuccess(@Nullable OutcomeEvent outcomeEvent) {
if (outcomeEvent == null)
unitySafeInvoke("onSendOutcomeSuccess", "");
else
unitySafeInvoke("onSendOutcomeSuccess", outcomeEvent.toJSONObject().toString());
}
});
}

public void sendUniqueOutcome(String name) {
OneSignal.sendUniqueOutcome(name, new OneSignal.OutcomeCallback() {
@Override
public void onSuccess(@Nullable OutcomeEvent outcomeEvent) {
if (outcomeEvent == null)
unitySafeInvoke("onSendOutcomeSuccess", "");
else
unitySafeInvoke("onSendOutcomeSuccess", outcomeEvent.toJSONObject().toString());
}
});
}

public void sendOutcomeWithValue(String name, float value) {
OneSignal.sendOutcomeWithValue(name, value, new OneSignal.OutcomeCallback() {
@Override
public void onSuccess(@Nullable OutcomeEvent outcomeEvent) {
if (outcomeEvent == null)
unitySafeInvoke("onSendOutcomeSuccess", "");
else
unitySafeInvoke("onSendOutcomeSuccess", outcomeEvent.toJSONObject().toString());
}
});
}

@Override
public void onOSPermissionChanged(OSPermissionStateChanges stateChanges) {
unitySafeInvoke("onOSPermissionChanged", stateChanges.toJSONObject().toString());
Expand Down

0 comments on commit cd97e20

Please sign in to comment.