Skip to content

Commit b2854d4

Browse files
committed
Update sendMessage in ServiceWorker
1 parent b1c83d2 commit b2854d4

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

PushMessageFromServer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ webPush.setVapidDetails(
1919

2020
// This is an sample subscription object that we get after subscript on browser
2121
const pushSubscription =
22+
2223
{
23-
"endpoint": "https://fcm.googleapis.com/fcm/send/eh4su7PQuF4:APA91bGJ2lTdArT_n0zLEkth_p7NTqyQ41KtXO5p0SsbcQ2iGgLHz1Y8FyDFS-MnWZjD5e6fair2TozX1bvccy_pHo4k-B3Ov_SLI-7lRvTE1_dppXQvP6IawxnSTBQk6AK5o4ks_mHv",
24+
"endpoint": "https://fcm.googleapis.com/fcm/send/fRXzVzVNHiY:APA91bH54ZXsVo1qq0FVb_OoPZ_CPRB2U-CGX6x8seB90AjpZQyu_AIS7BZXQ6XgV9BNBYBCC8_fL3dXxEaAgzNngOcU1ebuHlzfeXb_mAx5KpXlE1on_HEwZi8WPjdyNS9dONGmDaBT",
2425
"expirationTime": null,
2526
"keys": {
26-
"p256dh": "BEIlI4DO6N7gKHmT_FMHVHt8D2X4FWJp3vx9L8MOONfOsYNuYuxWAUWsscEWH4q5c-t6l5s769gGJr74n40H74w",
27-
"auth": "HSyGvAqddC7T0crQ3B6bHw"
27+
"p256dh": "BPR7g7k8pWxYFGoF6JRFcXKj6iKjg1bYZQQqB0Ele-LAeY7IhrapymrWMikxnRw82_euZLAFfnD8FT-TZfSsfJA",
28+
"auth": "P7ldHqkHFLl93VEkYFi20w"
2829
}
2930
}
3031

public/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<!-- For iOS web push -->
1111
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
1212
<meta name="apple-mobile-web-app-capable" content="yes">
13+
1314
</head>
1415

1516
<body>
@@ -29,9 +30,9 @@
2930
async function copyContent(message) {
3031
try {
3132
await navigator.clipboard.writeText(message);
32-
alert("copy");
33+
alert("Copied");
3334
/* Resolved - text copied to clipboard successfully */
34-
window.close();
35+
//window.close();
3536
} catch (err) {
3637
console.error('Failed to copy: ', err);
3738
/* Rejected - text failed to copy to the clipboard */

public/service-worker.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,36 @@ async function sendMessage(event, content) {
8484
type: 'window',
8585
});
8686

87-
let chatClient;
87+
let homeWindow;
8888

89-
// Let's see if we already have a chat window open:
89+
// Let's see if we already on root URL:
9090
for (const client of allClients) {
9191
const url = new URL(client.url);
9292

9393
// https://web-push-book.gauntface.com/common-notification-patterns/
9494
if (url.pathname === '/') {
95-
// Excellent, let's use it!
95+
96+
console.log('found homeUrlClient');
9697
await client.focus();
97-
chatClient = client;
98+
homeWindow = client;
9899
break;
99100
}
100101
}
101102

102-
// If we didn't find an existing chat window,
103+
// If we didn't find an existing window,
103104
// open a new one:
104-
if (!chatClient) {
105-
chatClient = await clients.openWindow('/');
106-
//await chatClient.focus();
105+
if (!homeWindow) {
106+
homeWindow = await clients.openWindow('/');
107+
// We can't access UI API from service worker, therefore we cannot focus a new created window.
108+
// await homeWindow.focus();
109+
console.log('new window opened');
107110
}
108111

109112
// Send a message to the client.
110-
chatClient.postMessage({
113+
homeWindow.postMessage({
111114
type: 'clipboard',
112115
content: content,
113116
});
114-
115117
}
116118

117119
async function openLink(data) {
@@ -124,4 +126,3 @@ async function openLink(data) {
124126
async function showNotificationAsync(payload, options) {
125127
await self.registration.showNotification(payload.title, options);
126128
}
127-

0 commit comments

Comments
 (0)