cancel
Cancels and removes the local notification associated with this request.
This method performs a complete cancellation:
- If the notification is scheduled but not yet displayed: unschedules it from AlarmManager
- If the notification is already displayed: removes it from the notification tray
- Removes the notification from Pushwoosh's internal database
// E-commerce: Cancel cart reminder when user completes purchase
public void onPurchaseComplete() {
SharedPreferences prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);
int cartReminderId = prefs.getInt("cart_reminder_id", -1);
if (cartReminderId != -1) {
LocalNotificationRequest request = new LocalNotificationRequest(cartReminderId);
request.cancel(); // Remove scheduled cart reminder
// Clean up stored ID
prefs.edit().remove("cart_reminder_id").apply();
Log.d("App", "Cart reminder cancelled after purchase");
}
}
// Fitness app: Cancel workout reminder if user completes workout early
public void onWorkoutComplete() {
if (scheduledWorkoutRequest != null) {
scheduledWorkoutRequest.cancel();
scheduledWorkoutRequest = null;
Toast.makeText(this, "Workout reminder cancelled", Toast.LENGTH_SHORT).show();
}
}
// News app: Cancel notification when user reads article
public void onArticleRead(int articleId) {
String requestKey = "article_" + articleId + "_notification";
int requestId = prefs.getInt(requestKey, -1);
if (requestId != -1) {
new LocalNotificationRequest(requestId).cancel();
prefs.edit().remove(requestKey).apply();
}
}
// Cancel all reminders when user logs out
public void onUserLogout() {
List<Integer> requestIds = database.getAllNotificationRequestIds();
for (int requestId : requestIds) {
new LocalNotificationRequest(requestId).cancel();
}
database.clearAllNotificationRequestIds();
}
Content copied to clipboard
Important Notes:
- This method is idempotent - safe to call multiple times for the same request
- If notification was already dismissed by user, this cleans up internal state
- If request ID is invalid or notification doesn't exist, method silently succeeds