Chapter 53: Web Notifications API & Native Push

Sound, Vibration & Silent Alerts

Harness multi-sensory feedback across platforms using silent alerts, custom haptic vibration arrays, and system Focus Assist / DND integration.

LEARNING OBJECTIVES
  • Silence native system audio chimes using the silent: true option.
  • Define custom haptic vibration patterns using the vibrate millisecond array ([vibrate, pause, vibrate, ...]).
  • Compare hardware vibration capabilities and limitations across Android, iOS, Windows, and macOS.
  • Understand how host operating system Focus Assist, Do Not Disturb (DND), and device mute switches override web notification sensory outputs.
🎬 INTERACTIVE VISUAL PIPELINE Core Architecture Simulation
🌐
1. Input
Directives & Tags
⚙️
2. Parse
Tokenizer & AST
🌳
3. Layout
Box Model & Flow
🎨
4. Render
GPU Paint & Composite
PHASE 1: INPUT & DIRECTIVES
Browser receives declarative markup stream, parsing tag tokens and initializing component state.

📖 The Mental Model & Story (Intuitive Foundation)

Imagine sitting in a quiet university library or a packed boardroom presentation. If your phone suddenly blasts a loud trumpet fanfare when a new marketing email arrives, you will be deeply embarrassed. However, if your phone gives you a subtle, rhythmic "double-tap" vibration in your pocket—or if your laptop silently displays a notification banner in the corner without making a peep—you stay informed without disturbing anyone.

+─────────────────────────────────────────────────────────────────────────────+
|                         SENSORY FEEDBACK SPECTRUM                           |
+─────────────────────────────────────────────────────────────────────────────+
|                                                                             |
|  [ 1. Silent Toast ]          [ 2. Tactile Haptic ]     [ 3. Full Alert ]   |
|   { silent: true }             { vibrate: [100, 50, 100] } { renotify: true}|
|         │                                │                    │             |
|         ▼                                ▼                    ▼             |
|   Visual Banner Only           Subtle Pocket Rhythm       Chime + Haptic    |
|   (Night mode, background sync) (Urgent chats, reminders) (Alarms, 2FA)     |
|                                                                             |
+─────────────────────────────────────────────────────────────────────────────+

The Web Notifications API provides two primary sensory levers:

  1. The silent toggle: Suppresses any audio chime or hardware buzz, presenting a purely visual notification.
  2. The vibrate pattern array: Encodes custom haptic rhythms (like Morse code) directly into the device's physical vibration motor on supported mobile devices.

Technical Deep Dive & Specifications

The silent Boolean Option

The silent property instructs the host operating system to suppress all default notification sounds and vibrations:

const silentNotification = new Notification('Sync Complete', {
  body: '142 files synchronized in the background.',
  icon: 'https://example.com/icon.png',
  silent: true // No OS chime, no vibration!
});

Precedence Rule: If you provide both silent: true and a vibrate pattern in the same options dictionary, the W3C specification dictates that silent: true takes absolute precedence. The notification will remain completely quiet and motionless.

The vibrate Pattern Specification

The vibrate property accepts a sequence of unsigned integers representing millisecond durations. The array alternates between active vibration time and silent pause time:

vibrate: [ Buzz1, Pause1, Buzz2, Pause2, Buzz3 ]
// Example: SOS Morse Code in Haptics (... --- ...)
// Short: 100ms, Long: 300ms, Inter-element pause: 50ms, Letter pause: 200ms
const sosPattern = [
  // S (...)
  100, 50, 100, 50, 100, 
  200, // Pause between S and O
  // O (---)
  300, 50, 300, 50, 300, 
  200, // Pause between O and S
  // S (...)
  100, 50, 100, 50, 100
];

const urgentAlert = new Notification('🚨 Server Outage Detected', {
  body: 'Production database latency exceeded 5000ms.',
  vibrate: sosPattern,
  icon: 'https://example.com/alert.png'
});

Popular Haptic Vibration Presets

Preset Name Vibration Array (ms) Tactical Feel / Sensation Best Use Case
Subtle Tap [50] Single crisp, light micro-haptic Routine informational updates
Double Pulse [100, 100, 100] Two quick distinct vibrations Incoming direct messages
Heartbeat [150, 150, 150, 600, 150, 150, 150] Thump-thump rhythm Urgent alarms, critical alerts
Incoming Call [500, 250, 500, 250, 500] Long ringing pulses VoIP incoming calls, video rings

Hardware & OS Compatibility Matrix

+─────────────────────────────────────────────────────────────────────────+
|                  SENSORY FEATURE SUPPORT BY PLATFORM                    |
+─────────────────────────────────────────────────────────────────────────+
|  Platform                  | `silent: true` | `vibrate` Array Pattern   |
|────────────────────────────┼────────────────┼───────────────────────────|
|  Android (Chrome / Edge)   |  ✅ Supported  |  ✅ Full Hardware Support |
|  Windows 10 / 11           |  ✅ Supported  |  ❌ Ignored (No Motor)    |
|  macOS (Chrome / Safari)   |  ✅ Supported  |  ❌ Ignored (No Web API)  |
|  iOS Safari (16.4+ PWA)    |  ⚠️ OS Governed|  ❌ Unsupported           |
|  Linux Desktop             |  ✅ Supported  |  ❌ Ignored               |
+─────────────────────────────────────────────────────────────────────────+

What Happened to the sound Property?

In the original 2012 drafts of the W3C Notifications API, there was a sound: 'alert.mp3' property. This property was deprecated and completely removed from all modern web browsers for the following reasons:

  1. Autoplay Abuse: Malicious websites abused custom sound URLs to blast loud audio advertisements.
  2. OS Sound Uniformity: Host operating systems (Windows Action Center, macOS, Android) enforce user-configured notification chimes so users can distinguish notification origins by their system themes.
  3. Alternative Solution: If your application is active in the foreground and requires a custom chime, use the Web Audio API or new Audio('chime.mp3').play() inside your active page, paired with silent: true on the notification.

💻 Interactive Code Playground

Starter Code

Save this file as index.html and open it on your computer or Android mobile device:

Line-by-Line Code Breakdown

  • Lines 131–136: Defines structured haptic presets with exact millisecond vibration/pause ratios.
  • Lines 147–156: Dynamically computes visual widths for each vibration burst and pause gap using CSS flex weighting.
  • Lines 176–183: Builds the NotificationOptions dictionary. When silent: true is chosen, the vibrate property is omitted, adhering to spec precedence rules.
  • Line 185: Dispatches the configured multi-sensory notification instance.

Expected Browser Render Output


SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
HTML STARSHIP CODE TERMINAL playground.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45°C
INSPECTING DOM: VALID
TAGS: SCANNING...
+────────────────────────────────────────────────────────────+
| 📳 Haptic & Sensory Notification Studio                   |
| Select a sensory profile below to test silent notifications|
|                                                            |
| [ 🔇 Silent Mode ]              [ ⚡ Subtle Tap ]          |
|  silent: true                    [60]                      |
|                                                            |
| [ 💬 Double Pulse ]             [ 🚨 Emergency SOS ]       |
|  [120, 80, 120]                  [100,50,100,50,100...]    |
|                                                            |
| +────────────────────────────────────────────────────────+ |
| | Selected Profile: Double Pulse ([120, 80, 120])        | |
| | [█████████░░░░░░█████████]                             | |
| +────────────────────────────────────────────────────────+ |
|                                                            |
| [ Dispatch Configured Notification (Blue CTA) ]            |
| Status: Ready. Click a profile and dispatch.               |
+────────────────────────────────────────────────────────────+

🏋️ Hands-On Exercise

🎯 The Challenge: Critical Server Alert Dispatcher

Instructions:

  1. Create a function dispatchServerMonitoringAlert(alertLevel, serverName, metricName, metricValue) where alertLevel can be 'info', 'warning', or 'critical'.
  2. Configure sensory options based on alertLevel:
    • 'info': Title "ℹ️ Server Info: " + serverName, silent: true, body: metricName + ": " + metricValue.
    • 'warning': Title "⚠️ Server Warning: " + serverName, silent: false, vibrate: [150, 100, 150].
    • 'critical': Title "🔥 CRITICAL ALERT: " + serverName, silent: false, vibrate: [300, 100, 300, 100, 500], requireInteraction: true.
  3. Dispatch the notification and return the instance.

🏁 Starter Code Sandbox

SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
STARTER CODE SANDBOX exercise.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45°C
INSPECTING DOM: VALID
TAGS: SCANNING...

⚠️ Common Pitfalls

  1. Passing Audio URLs to sound: The sound option is deprecated and non-functional in all modern browsers. Do not attempt sound: '/audio/alert.mp3'.
  2. Combining silent: true with vibrate: If both are set, silent: true overrides vibrate, muting the haptic motor entirely.
  3. Assuming Desktop Vibration: Laptops and desktop monitors do not have vibration motors. Vibration arrays only execute on mobile/tablet devices with haptic actuators.

💡 Pro Tips

  1. Foreground Custom Chimes via Web Audio API: When document.visibilityState === 'visible', dispatch a silent: true desktop notification and play a rich spatial audio chime inside the page using the Web Audio API (AudioContext).
  2. Respecting System DND & Focus Assist: Never attempt to "work around" operating system Focus Modes. If Windows Focus Assist or macOS Do Not Disturb is active, the OS intentionally suppresses alerts for user productivity.

📌 Key Takeaways

  • silent: true suppresses all audio chimes and vibrations for unobtrusive visual notifications.
  • vibrate accepts an array of alternating vibration and pause durations in milliseconds ([vibe, pause, vibe, ...]).
  • If both silent: true and vibrate are supplied, silent: true takes precedence.
  • Hardware vibration is supported on Android devices, while desktop platforms safely ignore the vibrate array.
  • The legacy sound property is deprecated and removed; custom foreground audio should be handled via the Web Audio API.
  • --
⭐ LEARN: HTML 🌟 ⚔️ QUIZ BATTLE ARENA // ACTIVE
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 1 / 3

What is the effect of setting vibrate: [200, 100, 400] on a mobile Android device?

Question 1 / 3 Topic: HTML Fundamentals
00:45 REMAINING
XP REWARD
+250 XP
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 2 / 3

What happens if a developer specifies both silent: true and vibrate: [500, 500, 500] in the notification options?

Question 2 / 3 Topic: HTML Fundamentals
00:45 REMAINING
XP REWARD
+250 XP
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 3 / 3

Why is the sound property (e.g. sound: 'bell.mp3') no longer used in modern Web Notifications?

Question 3 / 3 Topic: HTML Fundamentals
00:45 REMAINING
XP REWARD
+250 XP