Callback
The main callback interface for handling results of asynchronous operations in Pushwoosh SDK.
All asynchronous SDK operations (registration, tags management, user operations) use this callback interface to return results. The callback is always invoked on the main (UI) thread, making it safe to update UI elements directly without additional thread synchronization.
When to use callbacks:
- When you need to know if the operation succeeded or failed
- When you need to handle operation results (e.g., push token, tags data)
- When you want to show user feedback or update UI based on results
- When implementing retry logic for failed operations
Most SDK methods offer both callback and fire-and-forget versions. Use the callback version when you need to handle results; use the version without callback for fire-and-forget operations. Basic usage example:
// Register for push notifications with callback
Pushwoosh.getInstance().registerForPushNotifications((result) -> {
if (result.isSuccess()) {
// Operation succeeded
String token = result.getData().getToken();
Log.d("App", "Push token: " + token);
} else {
// Operation failed
Exception error = result.getException();
Log.e("App", "Registration failed: " + error.getMessage());
}
});
Content copied to clipboard
// Set tags with error handling
TagsBundle tags = new TagsBundle.Builder()
.putString("Name", "John Doe")
.putInt("Age", 25)
.build();
Pushwoosh.getInstance().setTags(tags, (result) -> {
if (result.isSuccess()) {
Log.d("App", "Tags updated successfully");
Toast.makeText(this, "Profile updated", Toast.LENGTH_SHORT).show();
} else {
Log.e("App", "Failed to update tags", result.getException());
Toast.makeText(this, "Update failed. Please try again", Toast.LENGTH_SHORT).show();
}
});
Content copied to clipboard
// Set user ID with callback
Pushwoosh.getInstance().setUserId("user_12345", (result) -> {
if (result.isSuccess()) {
Log.d("App", "User ID set successfully");
// Proceed with app initialization
initializeUserSession();
} else {
Log.e("App", "Failed to set user ID", result.getException());
// Retry or show error dialog
showRetryDialog();
}
});
Content copied to clipboard
private void registerWithRetry(int attemptNumber) {
Pushwoosh.getInstance().registerForPushNotifications((result) -> {
if (result.isSuccess()) {
Log.d("App", "Registration successful");
} else {
if (attemptNumber < MAX_RETRIES) {
Log.w("App", "Registration failed, retrying... Attempt " + (attemptNumber + 1));
new Handler().postDelayed(() -> registerWithRetry(attemptNumber + 1), 5000);
} else {
Log.e("App", "Registration failed after " + MAX_RETRIES + " attempts");
showErrorDialog("Unable to register for notifications");
}
}
});
}
Content copied to clipboard
// Show loading indicator during operation
showLoadingIndicator();
Pushwoosh.getInstance().getTags((result) -> {
hideLoadingIndicator();
if (result.isSuccess()) {
TagsBundle tags = result.getData();
String userName = tags.getString("Name", "Unknown");
int userAge = tags.getInt("Age", 0);
updateUserProfile(userName, userAge);
} else {
Log.e("App", "Failed to load tags", result.getException());
showError("Unable to load user profile");
}
});
Content copied to clipboard
Parameters
<T>
the type of data returned on successful operation completion
<E>
the type of exception returned on operation failure, must extend PushwooshException