HTML to APK Builder

ANDROIDAPI REFERENCE

AndroidAPI: notifications and scheduled reminders

Reference for showNotification, cancelNotification, scheduleNotification, repeating reminders, exact alarms and notification permission handling.

Updated:

These are local notifications created on the device by your app. They are not push notifications: there is no server and no way to send a notification to other users. Each notification shows your app’s icon and opens your app when tapped.

Requirements:

  • Declare POST_NOTIFICATIONS in the project. On Android 13 and later the user must also allow notifications. showNotification asks automatically the first time; you can ask earlier with requestNotificationPermission.
  • Scheduled reminders are Pro. For exact timing, also declare SCHEDULE_EXACT_ALARM.
Method Returns / event Pro
showNotification notificationResult no
cancelNotification, cancelAllNotifications notificationResult no
scheduleNotification scheduledNotificationResult yes
cancelScheduledNotification scheduledNotificationResult yes
canScheduleExactAlarms boolean no

showNotification

AndroidAPI.showNotification(title, message)
AndroidAPI.showNotification(title, message, bigText) // expandable

Shows a notification immediately. With bigText, the notification can be expanded to show the longer text. If permission is missing on Android 13+, the app shows the permission dialog first and posts the notification after approval.

Result event: notificationResult {action: "post", notificationId, success, error}. Keep notificationId (a number) if you want to remove the notification later. A denial reports error: "permission_denied".

window.addEventListener('notificationResult', e => {
  const r = e.detail;
  if (r.action === 'post' && r.success) lastNotificationId = r.notificationId;
  if (r.action === 'post' && !r.success) showMessage('Notification not shown: ' + r.error);
});
AndroidAPI.showNotification('Timer finished', 'Your tea is ready.');

Notifications are meant for when the user is not looking at your page. For in-app feedback, use your own UI or showToast.

cancelNotification and cancelAllNotifications

AndroidAPI.cancelNotification(notificationId) // number from notificationResult
AndroidAPI.cancelAllNotifications()

Remove a shown notification or all of your app’s notifications. Both report notificationResult with action: "cancel" or "cancelAll". They do not cancel scheduled reminders; use cancelScheduledNotification for those.

if (lastNotificationId !== null) AndroidAPI.cancelNotification(lastNotificationId);

scheduleNotification

AndroidAPI.scheduleNotification(id, title, message, delayMs)
AndroidAPI.scheduleNotification(id, title, message, delayMs, repeatIntervalMs)

Pro. Schedules a notification delayMs milliseconds from now. id is your own string identifier; scheduling again with the same id replaces the earlier reminder. With repeatIntervalMs greater than 0, the reminder repeats at that interval. The reminder fires even if your app is closed.

Result event: scheduledNotificationResult {id, success, exact, triggerAt, warning, error}.

  • triggerAt is the planned time as a Unix timestamp in milliseconds.
  • exact: false with warning containing inexact_alarm means Android may deliver it a few minutes late. See canScheduleExactAlarms below.
  • warning containing notification_permission_missing means the reminder is scheduled but will not be visible until notifications are allowed.
  • Errors: pro_required, alarm_failed.
window.addEventListener('scheduledNotificationResult', e => {
  const r = e.detail;
  if (!r.success) return showMessage('Reminder not set: ' + r.error);
  if (r.warning.includes('inexact_alarm')) showMessage('The reminder may arrive a few minutes late.');
  localStorage.setItem('reminder-' + r.id, String(r.triggerAt));
});

// Remind in 30 minutes
AndroidAPI.scheduleNotification('water', 'Drink water', 'Time for a glass of water.', 30 * 60 * 1000);

// Every day, starting tomorrow at 09:00
const next = new Date(); next.setDate(next.getDate() + 1); next.setHours(9, 0, 0, 0);
AndroidAPI.scheduleNotification('daily-plan', 'Good morning', 'Check today’s plan.',
  next - Date.now(), 24 * 60 * 60 * 1000);

delayMs is relative. To target a clock time, calculate the difference from Date.now() as shown. Save your reminder ids and times in your own storage so the page can show and cancel them later. Do not promise that reminders survive a device restart.

cancelScheduledNotification

AndroidAPI.cancelScheduledNotification(id)

Pro. Cancels a scheduled or repeating reminder by its string id. Reports scheduledNotificationResult.

AndroidAPI.cancelScheduledNotification('daily-plan');
localStorage.removeItem('reminder-daily-plan');

canScheduleExactAlarms

AndroidAPI.canScheduleExactAlarms() // → true / false

Returns whether Android allows your app to schedule exact alarms. On Android 12 and later the user controls this in Alarms & reminders, and it requires the SCHEDULE_EXACT_ALARM declaration. When it returns false, reminders still work but may be delayed. Offer openExactAlarmSettings() for alarm-style apps, then check again when the user returns.

if (!AndroidAPI.canScheduleExactAlarms()) {
  exactHint.hidden = false;
  exactButton.onclick = () => AndroidAPI.openExactAlarmSettings();
}

Web Notification API

Standard new Notification(title, {body}) and Notification.requestPermission() also work in the generated app. They use the same native notification path, so the same POST_NOTIFICATIONS requirement applies. Use AndroidAPI when you need scheduling or the result events.