[PWA] Add Push Notifications to a PWA with React in Chrome and on Android
On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'll start by asking the user for permission to send them push notifications, and then look at how to intercept the push
event in a service worker. We can test the push notifications directly in Chrome's devtools, and we will also make a button that can trigger a push notification directly from the PWA app code itself.
Install:
npm install web-push -g
Generate the key:
web-push generate-vapid-keys
In src/serviceworker.js
(Generated by create-react-app) we need to save access to the service worker registration here. Set global.registration = registration
.
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => { global.registration = registration ...
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}
Register function for push notification:
function subscribe () {
const key = 'xxx-xxx-xxx';
global.registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array(key)
}).then(sub => {
console.log("Subscribed!")
}).catch(err => {
console.log("Did not subscribe.")
})
} function urlB64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/'); const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
Now in src/sw.js: we can listen for a 'push', event
from the push server just like we did for fetch. Add a push eventListener
. We're going to tell the event
to waitUntil
we show a push notification. Access the server worker registration with self.registration
and call showNotification
. That takes the title as the first argument and a hash of options as the second. For now, we'll set the icon
to an icon that we already have in the public folder and the body
to whatever text comes through the push from the server.
self.addEventListener('push', event => {
event.waitUntil(self.registration.showNotification('Todo List', {
icon: '/icon-120.jpg',
body: event.data.text()
}))
})
Last, in App.js, we write code to send push message:
testPushMessage = () => {
global.registration.showNotification('Test Message', {
body: 'Success!'
})
}
[PWA] Add Push Notifications to a PWA with React in Chrome and on Android的更多相关文章
- [PWA] Enable Push Notification in your web app
1. Clone the project: git clone https://github.com/GoogleChrome/push-notifications.git 2. install th ...
- Send Push Notifications to iOS Devices using Xcode 8 and Swift 3, APNs Auth Key
Send Push Notifications to iOS Devices using Xcode 8 and Swift 3 OCT 6, 2016 Push notifications are ...
- [Erlang 0106] Erlang实现Apple Push Notifications消息推送
我们的IOS移动应用要实现消息推送,告诉用户有多少条消息未读,类似下图的效果(笑果),特把APNS和Erlang相关解决方案笔记于此备忘. 上面图片中是Apple Notif ...
- iOS 中的Push Notifications简单实现(APNS)
Android中的通知只有一种,就是Local Notifications,而iOS中除了Local Notifications外,还有一种Push Notifications.ios的这2种noti ...
- Apple Remote Push Notifications
1.帮助文档参考: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/Remote ...
- (转)pem, cer, p12 and the pains of iOS Push Notifications encryption
转自:http://cloudfields.net/blog/ios-push-notifications-encryption/ The serious pains of setting up a ...
- Xcode - Your development team, "", does not support the Push Notifications capability.
1.问题描述: 从git上checkout了别人的一个工程文件,选择team时,Xcode显示如下问题 Your development team, "xxx.xxx.xxx", ...
- iOS8 Push Notifications
本文转载至 http://blog.csdn.net/pjk1129/article/details/39551887 原贴地址:https://parse.com/tutorials/ios-p ...
- Your development team, "", does not support the Push Notifications capability.
问题: Your development team, "", does not support the Push Notifications capability. 解决方法: 1 ...
随机推荐
- Postfix+Sasl+Courier-authlib+Dovecot+MySQL+extmail 邮件系统部署
# yum remove postfix ##删除系统自带postfix# userdel postfix# groupdel postdrop# groupadd -g 2525 postfix# ...
- jquery 的ajax
一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...
- jquery判断ie与使用ie来判断ie,推荐ie样式块
用jquery来判断浏览器类型,如果只是仅仅为了判断浏览器的类型而使用该方法,那么不建议使用,只是在你已经使用了jquery才建议使用,因为没必要因为这么小的一个功能就加载那么大的类库吧 Jquery ...
- 学习webservice
客户端测试页: WebService代码: using System; using System.Collections.Generic; using System.Linq; using Syste ...
- Linux内核中断引入用户空间(异步通知机制)【转】
转自:http://blog.csdn.net/kingdragonfly120/article/details/10858647 版权声明:本文为博主原创文章,未经博主允许不得转载. 当Linux内 ...
- protobuf 中的嵌套消息的使用 主要对set_allocated_和mutable_的使用
protobuf的简单的使用,不过还留下了一个问题,那就是之前主要介绍的都是对简单数据的赋值,简单数据直接采用set_xx()即可,但是如果不是简单变量而是自定义的复合类型变量,就没有简单的set函数 ...
- git的使用03
之前我们写的都是将代码存在本地,我们还可以将代码github官网上,放在github的服务器上去托管
- ffmpeg muxer 参数简要说明
参数 值 说明 movflags MP4 Muxer 标记 rtphint 增加RTP的hint track empty_moov 初始化空的moov box frag_keyframe 在视频关键帧 ...
- flask框架基本使用(2)(响应与重定向)
#转载请留言联系 flask 框架基本使用(1):https://www.cnblogs.com/chichung/p/9756935.html 1. flask 自定义返回状态码与响应头 from ...
- Guava源码学习(四)新集合类型
基于版本:Guava 22.0 Wiki:New collection types 0. 简介 Guava提供了很多好用的集合工具,比如Multiset和BiMap,本文介绍了这些新集合类型的使用方式 ...