{"id":189258,"date":"2026-03-18T15:07:27","date_gmt":"2026-03-18T13:07:27","guid":{"rendered":"https:\/\/m50.lv\/?page_id=189258"},"modified":"2026-03-18T18:25:23","modified_gmt":"2026-03-18T16:25:23","slug":"webpush","status":"publish","type":"page","link":"https:\/\/m50.lv\/lv\/webpush\/","title":{"rendered":"webpush"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-page\" data-elementor-id=\"189258\" class=\"elementor elementor-189258\" data-elementor-post-type=\"page\">\n\t\t\t\t<div class=\"elementor-element elementor-element-4154d62 e-flex e-con-boxed e-con e-parent\" data-id=\"4154d62\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-2537ffd elementor-widget elementor-widget-html\" data-id=\"2537ffd\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"html.default\">\n\t\t\t\t\t<script>\r\nasync function subscribeUser() {\r\n  \/\/ 1. Register service worker\r\n  const registration = await navigator.serviceWorker.register('\/sw.js');\r\n\r\n  \/\/ 2. Ask permission\r\n  const permission = await Notification.requestPermission();\r\n  if (permission !== 'granted') {\r\n    console.log('Permission denied');\r\n    return;\r\n  }\r\n\r\n  \/\/ 3. Convert VAPID key\r\n  function urlBase64ToUint8Array(base64String) {\r\n    const padding = '='.repeat((4 - base64String.length % 4) % 4);\r\n    const base64 = (base64String + padding)\r\n      .replace(\/-\/g, '+')\r\n      .replace(\/_\/g, '\/');\r\n\r\n    const rawData = atob(base64);\r\n    return Uint8Array.from([...rawData].map(char => char.charCodeAt(0)));\r\n  }\r\n\r\n  const convertedKey = urlBase64ToUint8Array('BGkevIBi7Zqp1L0R2Huh4cAnmigE0287w9Xsyt1uCS-WiS_NfcCF3vElL2WdpBboet2GFFkNBHwpsp7JpJCn8hI');\r\n\r\n  \/\/ 4. Subscribe\r\n  const subscription = await registration.pushManager.subscribe({\r\n    userVisibleOnly: true,\r\n    applicationServerKey: convertedKey\r\n  });\r\n\r\n  console.log('Subscription:', subscription);\r\n\r\n  \/\/ 5. Send to backend (VERY IMPORTANT)\r\n  await fetch('https:\/\/n8n.m50.lv:5678\/webhook\/push-subscribe', {\r\n    method: 'POST',\r\n    headers: { 'Content-Type': 'application\/json' },\r\n    body: JSON.stringify(subscription)\r\n  });\r\n}\r\n\r\n\/\/ Trigger (button or auto)\r\nsubscribeUser();\r\n<\/script>\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"elementor_canvas","meta":{"_jet_sm_ready_style":"","_jet_sm_style":"","_jet_sm_controls_values":"","_jet_sm_fonts_collection":"","_jet_sm_fonts_links":"","footnotes":""},"class_list":["post-189258","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/m50.lv\/lv\/wp-json\/wp\/v2\/pages\/189258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m50.lv\/lv\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/m50.lv\/lv\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/m50.lv\/lv\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m50.lv\/lv\/wp-json\/wp\/v2\/comments?post=189258"}],"version-history":[{"count":19,"href":"https:\/\/m50.lv\/lv\/wp-json\/wp\/v2\/pages\/189258\/revisions"}],"predecessor-version":[{"id":189282,"href":"https:\/\/m50.lv\/lv\/wp-json\/wp\/v2\/pages\/189258\/revisions\/189282"}],"wp:attachment":[{"href":"https:\/\/m50.lv\/lv\/wp-json\/wp\/v2\/media?parent=189258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}