पाठ 23 / 29
Push API
Users को timely updates के साथ engage रखने के लिए server push messaging को integrate करें।
Push Message Delivery
Push API आपके server को push service के माध्यम से users को messages भेजने में सक्षम बनाता है। Messages तब भी arrive होते हैं जब app बंद हो, और service worker उन्हें handle करता है।
Push messages को subscribe करें
Push को subscribe करें और server को endpoint भेजें।
navigator.serviceWorker.ready.then(reg => {
reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(PUBLIC_KEY)
}).then(subscription => {
console.log('Subscribed:', subscription.endpoint);
// Send subscription to server
fetch('/subscribe', {
method: 'POST',
body: JSON.stringify(subscription)
});
});
});
Output:
App subscribed to push messages; server can now send messages to user.