unschedule

open fun unschedule()

Unschedules the local notification, preventing it from being displayed.

This method only cancels the scheduled notification from Android's AlarmManager. Unlike cancel, it does not remove notifications that have already been displayed from the notification tray.

Use Cases:

  • You want to prevent a scheduled notification from appearing
  • You want to reschedule a notification with different content
  • You don't care about notifications that are already in the notification tray
Example:

  // Reschedule a notification with updated content
  public void updateReminder(int oldRequestId, String newMessage, int newDelay) {
      // Unschedule old notification
      LocalNotificationRequest oldRequest = new LocalNotificationRequest(oldRequestId);
      oldRequest.unschedule();

      // Schedule new notification with updated content
      LocalNotification newNotification = new LocalNotification.Builder()
          .setMessage(newMessage)
          .setDelay(newDelay)
          .build();

      LocalNotificationRequest newRequest = Pushwoosh.getInstance()
          .scheduleLocalNotification(newNotification);

      // Store new request ID
      prefs.edit().putInt("reminder_id", newRequest.getRequestId()).apply();
  }

  // Cancel scheduled notification but leave displayed ones
  public void stopFutureReminders() {
      int requestId = prefs.getInt("future_reminder_id", -1);
      if (requestId != -1) {
          new LocalNotificationRequest(requestId).unschedule();
          Log.d("App", "Future reminders stopped");
      }
  }

Comparison with cancel:

unschedule✓ Yes✗ No
cancel✓ Yes✓ Yes

See also