Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Web Push Example with Non-Interactive Subscription & Auto-Activation in Service Worker #1410

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

GAURAVCHAWLA1808
Copy link

This PR fixes Issue #1408 by automating the push subscription process in the cookbook.push extension. Previously, users had to manually subscribe via the Service Worker DevTools. This update ensures the subscription is created automatically when the Service Worker activates.

Changes Implemented
background.js
Added event.waitUntil(subscribeUserVisibleOnlyFalse()); in the activate event to auto-subscribe users.
Improved logging for debugging.
Ensured userVisibleOnly = true for push notifications.
manifest.json
Added "gcm_sender_id": "103953800507" for better push compatibility.
popup.js
Improved error handling for subscription failures.
Added clear logs when subscribing/unsubscribing.
README.md
Updated instructions to include automatic subscription.
Added a template for send-push.js to help test push messages.
This update ensures a fully functional, real-world web push example without requiring manual DevTools interaction.

Fixes #1408

Copy link

google-cla bot commented Feb 11, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@GAURAVCHAWLA1808 GAURAVCHAWLA1808 changed the title Complete Web Push Example with Non-Interactive Subscription Enhance Web Push Example with Non-Interactive Subscription & Auto-Activation in Service Worker Feb 11, 2025
@oliverdunk
Copy link
Member

@patrickkettner, is this one you would like to look at? If not, feel free to assign it to me.

@GAURAVCHAWLA1808
Copy link
Author

Hi @oliverdunk , I would appreciate your guidance on this PR and any improvements I can make. Your insights would be valuable in ensuring everything aligns well with the project.

Additionally, I’m eager to contribute more—could you suggest other issues or areas where I can help? Looking forward to your guidance.
Thanks!

Copy link
Member

@oliverdunk oliverdunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this, and sorry for the delay leaving a review. Generally we want PRs to be well scoped and solve a single problem - in this case it's unclear what this PR's main goal is, as it does a lot of things:

  • Changes logging
  • Introduces a popup
  • Automatically subscribes on service worker startup
  • Significantly changes the README
  • Changes how the API key is provided

It would be great if you could choose one of those things and make the PR focused on that. We can then definitely review work on the others in additional PRs :)

@@ -1,21 +1,137 @@
# Service worker with push notification
# Service Worker with Push Notification
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking another look at the README. There are certainly some readability improvements, but the changes also take us further away from the template that we try to use for all samples: https://github.com/GoogleChrome/chrome-extensions-samples/blob/main/README-template.md

A few examples:

  • "This sample demonstrates" is a common phrase we use at the start of every sample.
  • We don't usually have a changelog in the README.

Could you take a look and try to make this closer to the template and avoid any unnecessary changes that don't significantly improve readability or add information that a new developer opening this sample would need?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the in convenience , I'll align Readme according to template .

git clone https://github.com/GoogleChrome/chrome-extensions-samples.git
cd chrome-extensions-samples/functional-samples/cookbook.push
```
2. Open `background.js` and **replace** `APPLICATION_SERVER_PUBLIC_KEY` with your public key from the web push test server.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPLICATION_SERVER_PUBLIC_KEY no longer exists in this version. I think it should do, though - can we revert that change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! That was removed unintentionally. I'll revert that change and ensure the instructions remain clear for developers.

console.log("✅ Background service worker is running!");

// Install event
self.addEventListener("install", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this? Otherwise, I would prefer to remove it as we generally don't want to make developers think they need to listen to service worker lifecycle events.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The install event was added for debugging purposes, but I agree that it might be misleading. I'll remove it.

// Activate event
self.addEventListener("activate", (event) => {
console.log("✅ Service Worker Activated.");
event.waitUntil(subscribeUserVisibleOnlyFalse());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work in a chrome.runtime.onInstalled listener instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll move this logic to a chrome.runtime.onInstalled listener and test if it works as expected.

},
"permissions": ["notifications"],
"host_permissions": ["https://*/*"],
"gcm_sender_id": "103953800507"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this , this was added as I was testing FCM ( Firebase Cloud Messaging ) for sending push notifications which needs "gcm_sender_id" .

}
},
"permissions": ["notifications"],
"host_permissions": ["https://*/*"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need host permissions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to allow the extension to interact with specific web pages , If it’s not essential, I can remove or restrict it to specific domains.

@GAURAVCHAWLA1808
Copy link
Author

Thanks for opening this, and sorry for the delay leaving a review. Generally we want PRs to be well scoped and solve a single problem - in this case it's unclear what this PR's main goal is, as it does a lot of things:

  • Changes logging
  • Introduces a popup
  • Automatically subscribes on service worker startup
  • Significantly changes the README
  • Changes how the API key is provided

It would be great if you could choose one of those things and make the PR focused on that. We can then definitely review work on the others in additional PRs :)

Thanks for your feedback, Oliver! I understand the concern regarding the PR scope. I'll separate the changes into multiple PRs, each focusing on a single issue .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Complete web push example with non-interactive subscription
2 participants