-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationsBadge.html
More file actions
250 lines (214 loc) · 7.63 KB
/
NotificationsBadge.html
File metadata and controls
250 lines (214 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Notifications and Badge Demo</title>
<style>
:root {
color-scheme: light dark;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body {
margin: 0;
min-height: 100vh;
display: grid;
place-items: center;
background:
radial-gradient(circle at top left, rgba(80, 160, 255, 0.35), transparent 34rem),
radial-gradient(circle at bottom right, rgba(180, 90, 255, 0.30), transparent 32rem),
Canvas;
color: CanvasText;
}
main {
width: min(720px, calc(100vw - 48px));
padding: 32px;
border: 1px solid color-mix(in srgb, CanvasText 15%, transparent);
border-radius: 28px;
background: color-mix(in srgb, Canvas 82%, transparent);
box-shadow: 0 24px 80px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(24px);
}
h1 {
margin: 0 0 8px;
font-size: 34px;
letter-spacing: -0.04em;
}
p {
margin: 0 0 24px;
line-height: 1.5;
color: color-mix(in srgb, CanvasText 72%, transparent);
}
.panel {
display: grid;
gap: 14px;
margin-top: 24px;
}
.row {
display: flex;
gap: 12px;
flex-wrap: wrap;
}
button {
appearance: none;
border: 0;
border-radius: 14px;
padding: 12px 16px;
font: inherit;
font-weight: 650;
cursor: pointer;
background: CanvasText;
color: Canvas;
}
button.secondary {
background: color-mix(in srgb, CanvasText 12%, transparent);
color: CanvasText;
}
input {
min-width: 120px;
border: 1px solid color-mix(in srgb, CanvasText 18%, transparent);
border-radius: 14px;
padding: 12px 14px;
font: inherit;
background: Canvas;
color: CanvasText;
}
.status {
margin-top: 20px;
padding: 14px 16px;
border-radius: 16px;
background: color-mix(in srgb, CanvasText 8%, transparent);
color: color-mix(in srgb, CanvasText 78%, transparent);
}
</style>
</head>
<body>
<main>
<h1>Notifications and Badge Demo</h1>
<p>
This local HTML file demonstrates native notification passthrough and Dock badge control in an exported HTML to App bundle. Dock badge buttons unlock after native notification permission is granted.
</p>
<section class="panel">
<div class="row">
<button id="requestPermission">Request notification permission</button>
<button id="notify" disabled>Show notification</button>
<button id="notifyCount" class="secondary" disabled>Notify with current badge</button>
</div>
<div class="row">
<input id="badgeValue" value="1" aria-label="Dock badge value">
<button id="setBadge" disabled>Set Dock badge</button>
<button id="incrementBadge" class="secondary" disabled>Increment</button>
<button id="clearBadge" class="secondary" disabled>Clear badge</button>
</div>
</section>
<div id="status" class="status">Ready.</div>
</main>
<script>
const statusElement = document.getElementById("status");
const badgeInput = document.getElementById("badgeValue");
const gatedButtonIds = ["notify", "notifyCount", "setBadge", "incrementBadge", "clearBadge"];
function setStatus(text) {
statusElement.textContent = text;
}
function updateGatedControls() {
const granted = "Notification" in window && Notification.permission === "granted";
gatedButtonIds.forEach((id) => {
document.getElementById(id).disabled = !granted;
});
const requestButton = document.getElementById("requestPermission");
requestButton.disabled = !("Notification" in window) || granted;
requestButton.textContent = granted ? "Notification permission granted" : "Request notification permission";
}
async function ensureNotificationPermission() {
if (!("Notification" in window)) {
setStatus("Notification API is not available.");
updateGatedControls();
return false;
}
if (Notification.permission === "granted") {
updateGatedControls();
return true;
}
const permission = await Notification.requestPermission();
updateGatedControls();
return permission === "granted";
}
async function showNotification(title, body) {
const allowed = await ensureNotificationPermission();
if (!allowed) {
setStatus("Notification permission was not granted.");
return;
}
new Notification(title, {
body,
tag: "html-to-app-demo"
});
setStatus("Notification sent.");
}
function setBadge(value) {
if (!("Notification" in window) || Notification.permission !== "granted") {
setStatus("Grant notification permission before using Dock badge controls.");
updateGatedControls();
return;
}
if (window.HTMLtoApp && typeof window.HTMLtoApp.setBadge === "function") {
window.HTMLtoApp.setBadge(value);
setStatus(`Dock badge set to "${value}".`);
return;
}
setStatus("HTMLtoApp badge bridge is not available.");
}
function clearBadge() {
if (!("Notification" in window) || Notification.permission !== "granted") {
setStatus("Grant notification permission before using Dock badge controls.");
updateGatedControls();
return;
}
if (window.HTMLtoApp && typeof window.HTMLtoApp.clearBadge === "function") {
window.HTMLtoApp.clearBadge();
setStatus("Dock badge cleared.");
return;
}
setStatus("HTMLtoApp badge bridge is not available.");
}
document.getElementById("requestPermission").addEventListener("click", async () => {
const allowed = await ensureNotificationPermission();
setStatus(allowed ? "Notification permission granted. Badge controls are unlocked." : "Notification permission was not granted.");
});
document.getElementById("notify").addEventListener("click", () => {
showNotification("HTML to App", "This notification was triggered by local HTML.");
});
document.getElementById("notifyCount").addEventListener("click", () => {
showNotification("Current badge", `The current badge value is ${badgeInput.value || "empty"}.`);
});
document.getElementById("setBadge").addEventListener("click", () => {
setBadge(badgeInput.value);
});
document.getElementById("incrementBadge").addEventListener("click", () => {
const nextValue = String((parseInt(badgeInput.value, 10) || 0) + 1);
badgeInput.value = nextValue;
setBadge(nextValue);
});
document.getElementById("clearBadge").addEventListener("click", () => {
badgeInput.value = "";
clearBadge();
});
updateGatedControls();
</script>
<script>
(function () {
function showHTMLtoAppBrowserNotice() {
if (window.HTMLtoApp || document.getElementById("htmltoapp-browser-notice")) return;
const notice = document.createElement("aside");
notice.id = "htmltoapp-browser-notice";
notice.setAttribute("role", "note");
notice.style.cssText = "position:fixed;left:16px;right:16px;bottom:16px;z-index:2147483647;padding:14px 16px;border-radius:10px;background:#111827;color:#fff;box-shadow:0 12px 32px rgba(0,0,0,.28);font:14px/1.45 -apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif";
notice.innerHTML = '<strong style="display:block;margin-bottom:2px;">HTML to App example</strong><span>These examples are made to work in HTML to App only. Visit <a href="https://htmltoapp.app" style="color:#93c5fd;font-weight:700;">https://htmltoapp.app</a> for more information.</span>';
document.body.appendChild(notice);
}
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", showHTMLtoAppBrowserNotice);
else showHTMLtoAppBrowserNotice();
}());
</script>
</body>
</html>