Web push notification is clickable rich content messages that are sent to users who are subscribed to a specific website. Web push notifications can be delivered to mobile(s) or tablet(s) even if the browser is closed. So, the user doesn’t have to be on specific website or webpage to receive notifications.
Web push notifications are used to for delivering quality content to engaged users, allow us to effectively re-engage them with personalized, relevant content.
Web Push Notification uses two complementary APIs:
AMP web push is specially developed for AMP websites, where user can able to subscribe to specific from AMP version of site.
Add the following script in your <head>
section. This piece of code will load the amp-web-push library by amp project.
<script async custom-element=”amp-web-push” src=”https://cdn.ampproject.org/v0/amp-web-push-0.1.js”></script>
Web Push Notifications are based on service workers because they operate in the background. Means the code runs only if the user interacts with notification by clicking on it or closing it. OneSignal is one the service providers who support AMP.
Download the following files and add them to the root directory of your site:
Note: Right-click on above links and save the link as a local file
Add the following code in <body>
of AMP page
<amp-web-push helper-iframe-url="https://example.com/helper-iframe.html?appId=YOUR-APP-ID" permission-dialog-url="https://example.com/permission-dialog.html?appId=YOUR-APP-ID" service-worker-url="https://example.com/OneSignalSDKWorker.js?appId=YOUR-APP-ID" ></amp-web-push>
example.com
with your domain.YOUR-APP-ID
with your OneSignal app ID.You have to create two buttons: Subscribe and Unsubscribe. So, users will able to subscribe and unsubscribe from AMP pages.
<!– A subscription widget – subscribe button –> <amp-web-push-widgetvisibility=“unsubscribed“ layout=“fixed“ width=“250“height=“80“> <buttonon=“tap:amp-web-push.subscribe”>Subscribe to push Notifications</button> </amp-web-push-widget>
<!– An unsubscription widget – unsubscribe button –> <amp-web-push-widget visibility=“subscribed“ layout=“fixed“ width=“250“height=“80“> <button on=“tap:amp-web-push.unsubscribe“>Unsubscribe from push Notifications</button> </amp-web-push-widget>
Go to AMP options panel -> General settings -> Push Notifications -> Enable OneSignal Push Notifications.
Enter the APP ID. You can also find it in OneSignal’s Plugin settings:
AMP web push is only compatible with URLs that begin with https://.
OneSignal still supports AMP web push for HTTP sites that use a subdomain of OneSignal (e.g. subdomain.os.tc), but when viewing the HTTP site as an AMP page, the site URL must be HTTPS.
When users view an AMP page on an actual mobile device, they are often directed by Google to a URL like “https://www.google.com/amp/www.site.com”. This means that HTTP sites that don’t ordinarily support HTTPS will actually support HTTPS thanks to Google if the AMP page is cached by Google and navigated from Google’s search results.
If your site doesn’t have an HTTPS connection(SSL), then enable the HTTP Site option and enter the subdomain:
You can even find it in your OneSignal’s plugin Settings:
Good going! Now you should able send Web Push Notifications.
If you have any queries from this article or you are looking for any tutorial/guide on AMP, let us know in the comment section below.