{"id":289369,"date":"2018-09-13T18:25:14","date_gmt":"2018-09-13T14:25:14","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=289369"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=289369","title":{"rendered":"\u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c Web Push Notifications \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c pywebpush \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<h3>\u0417\u0430\u0447\u0435\u043c \u0435\u0449\u0435 \u043e\u0434\u043d\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e?<\/h3>\n<p>  \u041a\u043e\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u043e \u043c\u043d\u043e\u0439 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0440\u043d\u043e\u0432\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 push notifications, \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435 push notifications. \u0412\u043e\u0442 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u0433\u043e\u0434\u043d\u044b\u0435:<br \/>  <a href=\"https:\/\/habr.com\/company\/ruvds\/blog\/350486\/\">\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 JS: \u0432\u0435\u0431 push-\u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f<\/a><br \/>  <a href=\"https:\/\/habr.com\/post\/321924\/\">Web PUSH Notifications \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e924\/<\/a><br \/>  <a href=\"https:\/\/habr.com\/company\/2gis\/blog\/350034\/\">Service Workers. Web Push \u0438 \u0433\u0434\u0435 \u043e\u043d\u0438 \u043e\u0431\u0438\u0442\u0430\u044e\u0442 <\/a><br \/>  \u042d\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e, \u043d\u043e \u043b\u0438\u0447\u043d\u043e \u043c\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u043b\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0433\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b\u043e \u0431\u044b \u0441\u0440\u0430\u0437\u0443, \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u043a\u043e\u043f\u0438\u043f\u0430\u0441\u0442\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e. \u041d\u0443 \u0438 \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e \u0441\u0440\u0435\u0434\u0438 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432 \u043d\u0435\u0442 \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434 \u0431\u0435\u043a \u043d\u0430 \u043f\u0438\u0442\u043e\u043d\u0435.<br \/>  \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0439 \u0432 \u0438\u0442\u043e\u0433\u0435 \u0437\u0430\u043d\u044f\u043b\u0430 \u0442\u0440\u0438 \u0434\u043d\u044f \u0438 \u043c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043d\u043e\u0433\u043e\u0432\u0430\u0442\u043e. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043c\u043e\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043a\u043e\u043c\u0443-\u0442\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c push notifications \u0437\u0430 \u0442\u0440\u0438 \u0447\u0430\u0441\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u0442\u0440\u0435\u0445 \u0434\u043d\u0435\u0439.<br \/>  \u041f\u0440\u043e\u0435\u043a\u0442, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f \u0440\u0430\u0431\u043e\u0442\u0430\u044e, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043d\u0430 Django \u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0445\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u044b \u044f \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a \u044d\u0442\u043e\u043c\u0443 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0443, \u043d\u043e \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0435 \u043b\u0435\u0433\u043a\u043e \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u0443\u044e\u0442 \u0435\u0433\u043e \u043a Flask \u0438\u043b\u0438 \u0447\u0435\u043c\u0443 \u043b\u0438\u0431\u043e \u0435\u0449\u0435.<br \/>  \u0418\u0442\u0430\u043a, \u043f\u043e\u0433\u043d\u0430\u043b\u0438.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043a\u043b\u044e\u0447\u0438<\/h3>\n<p>  \u0411\u0435\u0437 \u043a\u043b\u044e\u0447\u0435\u0439 \u043d\u0430\u0441, \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043d\u0438\u043a\u0443\u0434\u0430 \u043d\u0435 \u043f\u0443\u0441\u0442\u044f\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u043d\u0438\u0445. \u0414\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0435\u0439 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430 <a href=\"https:\/\/github.com\/web-push-libs\/vapid\/tree\/master\/python\">py_vapid<\/a>. \u041e\u043d \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 <a href=\"https:\/\/github.com\/web-push-libs\/pywebpush\">pywebpush<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043c \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043f\u043e\u0437\u0436\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b, \u0447\u0442\u043e\u0431\u044b \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u0430\u0442\u044c, \u043d\u0430\u0447\u043d\u0435\u043c \u0441 pywebpush:  <\/p>\n<pre><code class=\"bash\">&gt; bin\/pip install pywebpush &lt;\u0417\u0434\u0435\u0441\u044c \u0432\u044b\u0432\u043e\u0434 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438&gt; &gt; bin\/vapid --applicationServerKey No private_key.pem file found.         Do you want me to create one for you? (Y\/n)Y Generating private_key.pem Generating public_key.pem Application Server Key = &lt;\u041c\u043e\u0439 Server Key&gt; <\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Application Server Key \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0444\u0430\u0439\u043b settings.py.  <\/p>\n<pre><code class=\"python\">NOTIFICATION_KEY = &lt;\u041c\u043e\u0439 Server Key&gt; <\/code><\/pre>\n<p>  \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c NOTIFICATION_KEY \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u041f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c <a href=\"https:\/\/docs.djangoproject.com\/en\/2.1\/ref\/templates\/api\/#writing-your-own-context-processors\">\u0441\u0432\u043e\u0439 context_processor<\/a>.  <\/p>\n<h3>\u0414\u0435\u043b\u0430\u0435\u043c Service worker<\/h3>\n<p>  Service worker, \u043a\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u2014 \u044d\u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0439 \u0432 \u0444\u043e\u043d\u043e\u0432\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435. \u041d\u0430\u043c \u043e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u0448\u0438\u0445 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0439.<br \/>  \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043a\u043e\u0434 service worker \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"javascript\">self.addEventListener('push', function(event) {   var message = JSON.parse(event.data.text()); \/\/   event.waitUntil(     self.registration.showNotification(message.title, {       body: message.body,     })   ); });<\/code><\/pre>\n<p>  \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448 service worker. \u041f\u0440\u043e\u0446\u0435\u0441\u0441, \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043e\u043f\u0438\u0441\u0430\u043d <a href=\"https:\/\/developers.google.com\/web\/fundamentals\/push-notifications\/subscribing-a-user\">\u0437\u0434\u0435\u0441\u044c<\/a>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u0440\u0430\u0442\u043a\u043e:  <\/p>\n<pre><code class=\"javascript\"> function checkWorkerAndPushManager () {     if (!('serviceWorker' in navigator)) {         console.log('Workers are not supported.');         return;     }     if (!('PushManager' in window)) {         console.log('Push notifications are not supported.');         return;     } }  function registerWorker () { \twindow.addEventListener('load', function () {         navigator.serviceWorker.register('\/static\/js\/sw.js').then(function (registration) {             console.log('ServiceWorker registration successful');         }, function (err) {             console.log('ServiceWorker registration failed: ', err);             return;         });     }); \treturn true; }  var supported = checkWorkerAndPushManager();  if (supported){         var worker = registerWorker (); } <\/code><\/pre>\n<p>  \u041e\u0442\u043b\u0438\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u043d\u0430\u0448\u0435\u0433\u043e service worker. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u0439\u0434\u0438\u0442\u0435 \u0432 Developer Tools \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435, \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u043e\u0441\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0432\u043e\u043a\u0435\u0440\u0430 \u0438 \u043f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u043d\u0430 \u0432\u043a\u043b\u0430\u0434\u043a\u0443 Application \u0438 \u0441\u043b\u0435\u0432\u0430 \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 Service Worker.<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/qr\/vh\/s-\/qrvhs-jnlf-njoiviujvqufxszk.png\" alt=\"image\"><br \/>  \u0415\u0441\u043b\u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0435 \u043d\u0435 \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435, \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u044b \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f.<br \/>  \u041d\u0443 \u0432\u043e\u0442, \u043c\u044b \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u043c \u043e\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u0438 \u0441\u0435\u0431\u0435. \u041a\u0440\u0443\u0442\u043e, \u0435\u0434\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435.  <\/p>\n<h3>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043d\u0430 \u043f\u043e\u043a\u0430\u0437 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0439<\/h3>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432\u043e\u043a\u0435\u0440 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d, \u043f\u043e\u043f\u0440\u043e\u0441\u0438\u043c \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f.  <\/p>\n<pre><code class=\"javascript\"> function requestPermission() {   return new Promise(function(resolve, reject) {     const permissionResult = Notification.requestPermission(function(result) {       \/\/ \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430.       resolve(result);     });     if (permissionResult) {       permissionResult.then(resolve, reject);     }   })   .then(function(permissionResult) {     if (permissionResult !== 'granted') {       console.log(permissionResult);       throw new Error('Permission not granted.');     }   });   return true; } <\/code><\/pre>\n<p>  \u041a \u044d\u0442\u043e\u043c\u0443 \u043a\u043e\u0434\u0443 \u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u043e\u043d \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.  <\/p>\n<h3>\u041f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u0441\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0443<\/h3>\n<p>  \u041f\u043e\u0434\u043f\u0438\u0441\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u0436\u0435 \u043d\u0430 \u0444\u0440\u043e\u043d\u0442\u0435. <a href=\"https:\/\/developers.google.com\/web\/fundamentals\/push-notifications\/subscribing-a-user\">\u0417\u0434\u0435\u0441\u044c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043a\u043e\u0434 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438<\/a>, \u043d\u043e \u043d\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 urlBase64ToUint8Array, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043a\u043e\u0434 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043d\u0435\u0439.  <\/p>\n<pre><code class=\"javascript\"> NOTIFICATION_KEY = '{{ NOTIFICATION_KEY }};  function urlBase64ToUint8Array(base64String) {   const padding = '='.repeat((4 - base64String.length % 4) % 4);   const base64 = (base64String + padding)     .replace(\/\\-\/g, '+')     .replace(\/_\/g, '\/')   ;   const rawData = window.atob(base64);   return Uint8Array.from([...rawData].map((char) =&gt; char.charCodeAt(0))); }  function subscribeUserToPush(key) {    return navigator.serviceWorker.register('\/static\/coolwriter\/js\/sw.js')   .then(function(registration) {         var subscribeOptions = {             userVisibleOnly: true,             applicationServerKey: urlBase64ToUint8Array(key),         };    return registration.pushManager.subscribe(subscribeOptions)    })   .then(function(pushSubscription) {     sendSubscriptionToBackEnd(pushSubscription);   }); } <\/code><\/pre>\n<p>  (\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0430\u044f \u0437\u0434\u0435\u0441\u044c urlBase64ToUint8Array, \u0432\u0438\u0434\u0438\u043c\u043e, \u0438\u0437 \u0440\u0430\u0437\u0440\u044f\u0434\u0430 \u043a\u043e\u0441\u0442\u044b\u043b\u0435\u0439 \u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0432, \u043d\u043e \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043f\u0435\u0440\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e btoa \u043d\u0435 \u043f\u0440\u0438\u0432\u0435\u043b\u0430 \u043a \u0443\u0441\u043f\u0435\u0445\u0443, \u0443\u0436 \u043d\u0435 \u0437\u043d\u0430\u044e, \u043f\u043e\u0447\u0435\u043c\u0443. \u041c\u043e\u0436\u0435\u0442 \u043a\u0442\u043e \u043f\u043e\u0434\u0441\u043a\u0430\u0436\u0435\u0442).<br \/>  \u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440. \u0423 \u043c\u0435\u043d\u044f \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"javascript\"> function sendSubscriptionToBackEnd(subscription) {     $.post(         SAVE_REGISTRATION_URL,         {            'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val(),            \/\/\u0415\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432 \u0442\u0435\u043c\u043f\u043b\u0435\u0439\u0442\u0435 \u0435\u0441\u0442\u044c \u0442\u0435\u0433 {% csrf_token %}.             'registration_data': JSON.stringify(subscription)         }     ); } <\/code><\/pre>\n<p>  \u041d\u0443 \u0438 \u0434\u0430\u043b\u044c\u0448\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u044d\u0442\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u041c\u043e\u0436\u043d\u043e \u043f\u0440\u044f\u043c\u043e \u0441\u0442\u0440\u043e\u043a\u043e\u0439. \u0414\u0430, \u043d\u0435 \u0432\u0437\u0434\u0443\u043c\u0430\u0439\u0442\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u044c user-subscription \u0442\u0438\u043f\u0430 \u00ab\u043e\u0434\u0438\u043d \u043a \u043e\u0434\u043d\u043e\u043c\u0443\u00bb. \u0412\u0440\u043e\u0434\u0435 \u0431\u044b \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u043d\u043e \u0432\u0434\u0440\u0443\u0433 \u043a\u043e\u043c\u0443 \u0432\u0437\u0434\u0443\u043c\u0430\u0435\u0442\u0441\u044f.<br \/>  \u0423 \u043c\u0435\u043d\u044f \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u043e\u0442 \u0442\u0430\u043a\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u043e\u043d\u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0435\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0443:  <\/p>\n<pre><code class=\"python\">class UserSubscription(models.Model):     subscription = models.CharField(max_length=500)     user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='subscriptions') <\/code><\/pre>\n<p>  <\/p>\n<h3>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0448\u0430\u0433. \u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e pywebpush<\/h3>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0432\u0441\u0435 \u043f\u043e \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0443, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u044b\u0445 \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0435\u0439. \u041d\u0443, \u0440\u0430\u0437\u0432\u0435 \u0447\u0442\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448 service worker \u043c\u043e\u0433 \u0435\u0433\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c.  <\/p>\n<pre><code class=\"python\">import json from pywebpush import webpush, WebPushException from django.conf import settings  from .models import UserSubscription   def push_notification(user_id):     user_subscriptions = UserSubscription.objects.filter(user_id=notification.user_id)     for subscription in user_subscriptions:         data = json.dumps({         'title': 'Hello',         'body': 'there',     })         try:             webpush(                 subscription_info=json.loads(subscription.subscription),                 data=data,                 vapid_private_key='.\/private_key.pem',                 vapid_claims={                     'sub': 'mailto:{}'.format(settings.ADMIN_EMAIL),                 }             )             notification.sent = True             notification.save()         except WebPushException as ex:             print('I\\'m sorry, Dave, but I can\\'t do that: {}'.format(repr(ex)))             print(ex)             # Mozilla returns additional information in the body of the response.             if ex.response and ex.response.json():                 extra = ex.response.json()                 print('Remote service replied with a {}:{}, {}',                       extra.code,                       extra.errno,                       extra.message                       ) <\/code><\/pre>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0443\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c push_notification \u0438\u0437 django shell \u0438 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c.<br \/>  \u0412 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u0435\u0449\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u043e \u0441\u0442\u0430\u0442\u0443\u0441\u043e\u043c 410. \u0422\u0430\u043a\u043e\u0439 \u043e\u0442\u0432\u0435\u0442 \u0433\u043e\u0432\u043e\u0440\u0438\u0442, \u0447\u0442\u043e \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0430 \u0430\u043d\u043d\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0430, \u0438 \u0442\u0430\u043a\u0438\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u043a\u0438 \u0445\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0438\u0437 \u0411\u0414.   <\/p>\n<h3>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <a href=\"https:\/\/github.com\/safwanrahman\/django-webpush\">django-webpush<\/a>. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0442\u0435\u043c, \u043a\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 Django, \u0441\u0442\u043e\u0438\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0441 \u043d\u0435\u0435.<\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/post\/423193\/\"> https:\/\/habr.com\/post\/423193\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<h3>\u0417\u0430\u0447\u0435\u043c \u0435\u0449\u0435 \u043e\u0434\u043d\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e?<\/h3>\n<p>  \u041a\u043e\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u0434\u043e \u043c\u043d\u043e\u0439 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0440\u043d\u043e\u0432\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 push notifications, \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u043d\u0430 \u0445\u0430\u0431\u0440\u0435 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435 push notifications. \u0412\u043e\u0442 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u0433\u043e\u0434\u043d\u044b\u0435:<br \/>  <a href=\"https:\/\/habr.com\/company\/ruvds\/blog\/350486\/\">\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 JS: \u0432\u0435\u0431 push-\u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f<\/a><br \/>  <a href=\"https:\/\/habr.com\/post\/321924\/\">Web PUSH Notifications \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e924\/<\/a><br \/>  <a href=\"https:\/\/habr.com\/company\/2gis\/blog\/350034\/\">Service Workers. Web Push \u0438 \u0433\u0434\u0435 \u043e\u043d\u0438 \u043e\u0431\u0438\u0442\u0430\u044e\u0442 <\/a><br \/>  \u042d\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e, \u043d\u043e \u043b\u0438\u0447\u043d\u043e \u043c\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u043b\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0433\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b\u043e \u0431\u044b \u0441\u0440\u0430\u0437\u0443, \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u043a\u043e\u043f\u0438\u043f\u0430\u0441\u0442\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e. \u041d\u0443 \u0438 \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e \u0441\u0440\u0435\u0434\u0438 \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432 \u043d\u0435\u0442 \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434 \u0431\u0435\u043a \u043d\u0430 \u043f\u0438\u0442\u043e\u043d\u0435.<br \/>  \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0439 \u0432 \u0438\u0442\u043e\u0433\u0435 \u0437\u0430\u043d\u044f\u043b\u0430 \u0442\u0440\u0438 \u0434\u043d\u044f \u0438 \u043c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043d\u043e\u0433\u043e\u0432\u0430\u0442\u043e. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043c\u043e\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043a\u043e\u043c\u0443-\u0442\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c push notifications \u0437\u0430 \u0442\u0440\u0438 \u0447\u0430\u0441\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u0442\u0440\u0435\u0445 \u0434\u043d\u0435\u0439.<br \/>  \u041f\u0440\u043e\u0435\u043a\u0442, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f \u0440\u0430\u0431\u043e\u0442\u0430\u044e, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043d\u0430 Django \u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0445\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u044b \u044f \u0431\u0443\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a \u044d\u0442\u043e\u043c\u0443 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0443, \u043d\u043e \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0435 \u043b\u0435\u0433\u043a\u043e \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u0443\u044e\u0442 \u0435\u0433\u043e \u043a Flask \u0438\u043b\u0438 \u0447\u0435\u043c\u0443 \u043b\u0438\u0431\u043e \u0435\u0449\u0435.<br \/>  \u0418\u0442\u0430\u043a, \u043f\u043e\u0433\u043d\u0430\u043b\u0438.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-289369","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/289369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=289369"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/289369\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=289369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=289369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=289369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}