webãã©ã¦ã¶ã«Pushéç¥éããµã¼ãã¨jsã®ãµã³ãã«
ãã®ããã¥ã¡ã³ãã¯
以ä¸ã®ï¼ã¤ã®ããã¥ã¡ã³ããããããªããã£ãã«ç解ããããã®DRYãªåå¿é²ã§ãã
- Adding Push Notifications to a Web App | Web | Google Developers
- The Web Push Protocol | Web | Google Developers
èæ¯
ãã¤ã¦ サーバからブラウザにプッシュ通知を送りたい(非WebSocket、非ロングポーリング) - DRYな備忘録ãããæ¸ãããã©ãæ¹ãã¦Progressive Web Appã®ããã¥ã¡ã³ãè¡ã£ããã¢ãã¿ããã«åããã¥ãããªã£ã¦ã¦ä¸æ¡çãæããã®ã§æ¸ãã¾ãã
ãµã¼ãããPushéç¥ããã©ã¦ã¶ã«å¯¾ãã¦éãã¦ããã©ã¦ã¶ã®jsãéãã¦ã¦ãServiceWorkerãçãã¦ãããNotificationãåºããã£ã¦ãããã¤ã§ããæè¿ã ã¨ã¦ã§ãã®Gmailã¨ãFacebook Messangerè¥å¹²ããããã¤ã¨ãã§ããè¦ãæ°ããã¾ãã
ãã®å ¬å¼ããã¥ã¡ã³ããããã¨ãµã³ãã«è½ã¨ãã¦ãã¦ãã¤åãã§ããï¼ã£ã¦ããã¿ã¤ããªã®ãæå°éã®è¦ç´ ãç解ããã¤ã¤åããã¿ã¤ããªã®ããå®å ¨ã«ã©ã£ã¡ä»ããã®ããã¥ã¡ã³ãã«ãªã£ã¦ãã¦ãè¥å¹²ãããã«ããã§ãã
ä»åã¯ãWebPushProtocolã«ãããæå·åãããbodyãéããã¨ããã¨ãããé¤ããã¯ã©ã¤ã¢ã³ãã®ã³ã¼ãã¨ãµã¼ãã®ã³ã¼ããæ¸ããã®ã§ããã®ãã°ãåå¿é²ã¨ãã¦æãã¾ãã
æ¦å¿µã®ç¢ºèª
ãµã¼ãã¹ã¨ãã¦ã¯ãï¼ã¤ã§ã
- ãã©ã¦ã¶
- ãã¤ãã£ãã¢ããªéçºã«ããããããã¤ã¹ãã ã¨ç解ãã¦åé¡ãªãã§ã
- ãã©ã¦ã¶ã§ã¯ãµãã¤ã®ãã¨ããã¾ã
- ServiceWorkerãç»é²ããç»é²ããServiceWorkerã«ããã·ã¥ãsubscribeãããPushSubscriptionã¢ãã«ãåå¾ãã¾ã
- ããããApplePushNotificationã«ããã
Device Token
çãªãã®ã§ã
- ããããApplePushNotificationã«ããã
- ServiceWorkerå
é¨ã§ã¯ãã¿ãã®JavaScriptãæ»ãã§ãbackgroundã§åãç¶ããã¤ãã³ããªã¹ãã¼ãç»é²ãã¾ã
push
ãæ¥ãã¨ããã¨ããã¤ãã³ããªã¹ãã¼ãããã§å®ç¾©ãã¾ã
- ServiceWorkerãç»é²ããç»é²ããServiceWorkerã«ããã·ã¥ãsubscribeãããPushSubscriptionã¢ãã«ãåå¾ãã¾ã
- èªåãµã¼ã
- ãã©ã¦ã¶ã§ç»é²ã»åå¾ãããPushSubscriptionã¢ãã«ã®ãã¼ã¿ããèªä½ã¢ããªã±ã¼ã·ã§ã³ã«ãããã¦ã¼ã¶ãªã©ã¨ç´ä»ãã¦ä¿åãã¦ããå¿ è¦ãããã¾ã
- ä½ããã®ããªã¬ã¼ã§ï¼ãã¨ãã°ã¦ã¼ã¶ã«ããä»ã¦ã¼ã¶ã¸ã®å¼ã³ãããªã©ï¼ä¸è¨ã®ãPushãµã¼ãã¹ãã«ããã®subscription endpointã¸ãã®å 容ã§ããã·ã¥éç¥éã£ã¦ã¡ããã¾ããã¨ãããªã¯ã¨ã¹ããããå½¹å²ãããã¾ã
- Pushãµã¼ãã¹
- åããã¤ã¹ã«å¯¾ãã¦Pushãå®éã«éããµã¼ãã¹ã§ã
- Web-Push-Protocolã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãæºãããµã¼ãã¹ã§ããå¿ è¦ãããã¾ã
- åãã¯èªåãµã¼ããããã®ã¨ã³ããã¤ã³ãå©ãã ããªã®ã§ãã®éããæèããå¿ è¦ã¯ç¡ã â ããã ããï¼
ããããåãã³ã¼ãè¦ãã
ã¦ãã¹
ãããªæãã«åãã¾ã
å®éã®å¦ç
- ãã©ã¦ã¶ã«ç»é²ãããServiceWorker
- ã½ã¼ã¹ã³ã¼ã: service-worker.js
- ãããã¨
- èªåèªèº«ã«ä½ãã¤ãã³ãããã£ããããããã³ããªã³ã°ãã
install
,activate
,push
- èªåèªèº«ã«ä½ãã¤ãã³ãããã£ããããããã³ããªã³ã°ãã
- å¿
è¦ãªãã®
- ç¡ããã¶ã£ã¡ããç»é²ãããServiceWorkerãããã¹ã責åã¯å°ããã
- ã¦ã§ããã¼ã¸ã®main
- ã½ã¼ã¹ã³ã¼ã: main.js
- ãããã¨
- ServiceWorkerããã©ã¦ã¶ã«registerãã
- ãããã
registration.pushManager.subscribe()
ã¨ããã®ãå¼ã¶ - ä¸è¨ã§å¾ããã
subscription
ã¢ãã«ãèªä½ãµã¼ãã¸éãã¤ãã- ãã®
subscription
ã¨ããã®ããPushãæä¾ããGCMãµã¼ãã®ã¨ã³ããã¤ã³ãã¨ããã®ããã¤ã¹ï¼ãã©ã¦ã¶ï¼ã®DeviceTokençãªãã®ãæã£ã¦ãã
- ãã®
- å¿
è¦ãªãã®
- ApplicationServerPublicKeyãå¿
è¦
- ãµã¼ãã®ã¿ãç¥ãããApplicationServerPrivateKeyã«å¯¾ããªããã®
- ApplicationServerPublicKeyãå¿
è¦
- Pushãããªã¬ã¼ããèªåãµã¼ã
- ã½ã¼ã¹ã³ã¼ã: server.jsï¼ãã¡ããjsã§æ¸ãå¿ è¦ã¯ç¡ãï¼
- ãããã¨
- ã¦ã§ããã¼ã¸ã®jsããéããã¦ãã
subscription
ã®ä¿å - ä»»æã®ããªã¬ã¼ã§ãã®
subscription
ã¸Pushãéã- å³å¯ã«ã¯Pushã"éã"ã®ã¯gcmãªãMozilaPushServiceãªã®ã§ããPushãµã¼ãã¹ãæä¾ããã¨ã³ããã¤ã³ããå©ããã®ã»ããæ£ç¢º
- ã¾ããPushãµã¼ãã¹ãå©ãã¨ãã®ãªã¯ã¨ã¹ãã¯Web-Push-Protocolã§å®ããããæ¹æ³ã§æå·åããã¦ããå¿ è¦ãããããä»åã¯ããã¯web-pushã©ã¤ãã©ãªã«ãã¹ã¦ä»»ãã¦ãã
- ã¦ã§ããã¼ã¸ã®jsããéããã¦ãã
- å¿
è¦ãªãã®
- ApplicationServerPrivateKeyãå¿ è¦
注æç¹ã»ããã£ãã¨ãã
ServiceWorkerã®æ´æ°ãã§ããªã
- åé¡: ãã¼ã¸ããªãã¼ããã¦ãç·¨éããServiceWorkerãé©ç¨ããã¦ããªã
- åå : ServiceWorkerã®ã©ã¤ããµã¤ã¯ã«ã¯ããã©ã¦ã¶èªä½ãã¨ãã¦ã§ããã¼ã¸ã®ãã¡ã¤ã³ãã«ãã£ã¦å®ããããã®ã§ãã£ã¦ããã¦ã§ããã¼ã¸ã®ã¿ãï¼ãããããã¤ãã®ãã©ã¦ã¶ã®jsã©ã³ã¿ã¤ã ï¼ãã§ã¯ãªãã®ã§ããã¼ã¸ããªãã¼ããã¦ãæ´æ°ãããªã
- 解決:
getRegistraion
ããã®unregister
ãå¼ã¶ããããã¯chrome://serviceworker-internals/
ã«è¡ã該å½ã¦ã§ããã¼ã¸ã«ç»é²ããã¦ããServiceWorkerã[Unregister]ãã¿ã³ã§åé¤ãããªãããå¿ è¦ããã
Subscriptionã®æ´æ°ãã§ããªã
InvalidStateError: Registration failed - A subscription with a different applicationServerKey (or gcm_sender_id) already exists; to change the applicationServerKey, unsubscribe then resubscribe.
- åé¡:
registration.pushManager.subscribe()
ãå¼ã¶ã¨ä¸è¨ã®ãããªã¨ã©ã¼ãåºããã¨ãããã - åå : ã¨ã©ã¼ã¡ãã»ã¼ã¸ã§ã¯ãå¥ã®applicationServerKeyã使ã£ãsubscriptionããã§ã«ããã®ã§ãapplicationServerKeyãå¤ãã¦unsubscribeãã¦ãããã£ããsubscribeãã¦ããã¨è¨ã£ã¦ãããåºæ¬çã«ã¯ApplicationServerPublicKey㨠ApplicationServerPrivateKeyã®çµã¿åããã¯ã1ã¤ã®ããã¸ã§ã¯ãã«ã¤ãã¦ãã 1ã¤ã ããªã®ã§ããã®ãããªã¨ã©ã¼ã¯èµ·ããªãã¨æãããéçºä¸ã¨ãã ã¨èµ·ããããããã¾ããããã¤ãä¸è¨ã®ãµã³ãã«ã§ã¯ãnpmã®post-installã§1度ã ãVAPIDãä½ãããã«ãã¦ã¾ã
- 解決: ã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ãã¨ãªããå¾ã
ApplicationServerKeyã¨ã¯
InvalidAccessError: Failed to execute 'subscribe' on 'PushManager': The provided applicationServerKey is not valid.
Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
- åé¡: subscribeããã¨ãã«ãä¸è¨ã®ãããªã¨ã©ã¼ãåºããã¨ããããã©ãããapplicationServerKeyããããªãããã ããããããApplicationServerKeyã£ã¦ä½ãªã®ãã
- åå : ä»åãã¡ã°ãããã£ãã¨ãããªãã§ãããApplicationServerKeyã£ã¦ããã®ã¯ãWebPushProtocolã®æèã§ã®ãã ã®è¨ãæ¹ã§ããã®ä»ãããããªæèã¨ã¯é¢ä¿ãªãããã ã®ãå®ããããæ¹æ³ã§çæãããPublicKeyã¨PrivateKeyã®ãã¢ã¨ããã ãã§ããããã«GCM_API_KEYã¨ãFCM_SERVER_KEYã¨ãçªã£è¾¼ãã§å°ããã¨ããã±ã¼ã¹ãå¤ãããã¾ãåããªãã§ããã©ãRSAã¨ããç¡é¢ä¿ãååä¼¼ã¦ããã©ã
- 解決: ã»ãã§ããã®ãKeyã®ãã¢ãã¯ãä»åã¯web-pushã©ã¤ãã©ãªã®generateVAPIDKeysã«å ¨é¨ã¾ããã¦ãã¾ããPushãµã¼ãã¨ãã¦GCMãMozilaPushServiceãå©ããã¨ããããã©ãä»åã®ãµã³ãã«ã¯GCM, FCM, MPSãé¢ç¥ããªããWeb-Push-Protocolãç¨ããã¢ããªã±ã¼ã·ã§ã³ã®æ§æã®ãµã³ãã«ã¨ããä½ç½®ã¥ãã§ãã
éæ
- é渡æã¨ã¯ããããã¥ã¡ã³ããããã¥ãããã§ããã1å¹´åã¯çµ¶è³ããããããããã ã£ãã®ã«…
- Pushã®ããªã¬ã¼ããèªåãµã¼ããä»ãã¨ãGoã§æ¸ãã¦ããã ãã©ã
- ãã GitHub - gauntface/web-push-go ãã
- ãã GitHub - SherClockHolmes/webpush-go: Web Push API Encryption with VAPID support. ã ããª
- ãã£ã¨æ¥æ¬èªã§ä¸å¯§ãªãã¤è¦ã¤ãã¾ãã
- çæ°´åç©ãã¤ã¨ããã¯ãããæã¯éæ¿ããã¦ä½èª¿å´©ãããã©ãã¡ãã£ã¨é£ã£ã¦ããã«ã¼ã«ã«ãããå®å®ãã¦ã¿ãã¿ãç©ãã¦ã¾ã
DRYãªåå¿é²ã¨ãã¦
Building Progressive Web Apps: Bringing the Power of Native to the Browser
- ä½è : Tal Ater
- åºç社/ã¡ã¼ã«ã¼: Oreilly & Associates Inc
- çºå£²æ¥: 2017/07/25
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- ãã®ååãå«ãããã°ãè¦ã
ã¯ããã¦ã®Node.js -ãµã¼ãã¼ãµã¤ãJavaScriptã§Webã¢ããªãéçºãã-
- ä½è : æ¾å³¶æµ©é
- åºç社/ã¡ã¼ã«ã¼: SBã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2013/03/13
- ã¡ãã£ã¢: åè¡æ¬
- ã¯ãªãã¯: 15å
- ãã®ååãå«ãããã° (5件) ãè¦ã