#Mobile web push notifications for my website

6 messages · Page 1 of 1 (latest)

neon ginkgo
#

How do I impellent Mobile web push notifications for my website, if I open it on my Mobile browser?

I have made a web site in which there is an Alert button and whenever it dats updated by a counter it sends push notifications in my Desktop web browser, but the same is not happening if I open my website in Mobile web browser, someone help me in that , thank you

amber rivet
#

Which mobile browser? Web Push is not universally supported.

amber rivet
#

I have minimal experience. only got it working once. however, this worked in iOS for me:

  const handlePushEnable = async () => {
    // Request user permission for notifications
    const userChoice = await window.Notification.requestPermission();
    if (userChoice === 'granted') {
      // Subscribe to push manager
      const registration = await navigator.serviceWorker.getRegistration();
      const subscribed = await registration.pushManager.getSubscription();
      if (subscribed) {
        console.info('User is already subscribed to push notifications.');
        return;
      }
      const subscription = await registration.pushManager.subscribe({
        userVisibleOnly: true,
        applicationServerKey: urlB64ToUint8Array(process.env.REACT_APP_VAPID)
      });

      // Send subscription information to API server
      try {
        await pushRegistration(subscription);
      } catch (error) {
        console.error('Unable to add push subscription to API', error);
      }

      navigator.serviceWorker.controller.postMessage({
        type: 'ENABLE_PUSH',
      });
    }
    setPushEnabled(userChoice);
  };
#

oh! it only works when the site is added as a PWA (added to home screen). is that what you're missing?

#

AFAIK that same code should work on various Android browsers, but haven't tested.