Skip to content

Commit 8f27154

Browse files
authored
Merge pull request #41 from mariusbrn/swRegistration
add service worker registration prop
2 parents 2ca7e31 + 07cff83 commit 8f27154

File tree

4 files changed

+73
-35
lines changed

4 files changed

+73
-35
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Notification.propTypes = {
3434
onError: func,
3535
timeout: number,
3636
title: string.isRequired,
37-
options: object
37+
options: object,
38+
swRegistration: object,
3839
};
3940

4041
```
@@ -66,6 +67,10 @@ Notification.propTypes = {
6667
* `options` : Notification options. set `body`, `tag`, `icon` here.
6768
See also (https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification)
6869

70+
* `swRegistration` : ServiceWorkerRegistration. Use this prop to delegate the notification creation to a service worker.
71+
See also (https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification)
72+
⚠️ `onShow`, `onClick`, `onClose` and `onError` handlers won't work when notification is created by the service worker.
73+
6974

7075
## Usage example
7176

lib/components/Notification.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,29 @@ var Notification = function (_React$Component) {
134134
}
135135

136136
if (!this.notifications[opt.tag]) {
137-
var n = new window.Notification(this.props.title, opt);
138-
n.onshow = function (e) {
139-
_this3.props.onShow(e, opt.tag);
140-
setTimeout(function () {
141-
_this3.close(n);
142-
}, _this3.props.timeout);
143-
};
144-
n.onclick = function (e) {
145-
_this3.props.onClick(e, opt.tag);
146-
};
147-
n.onclose = function (e) {
148-
_this3.props.onClose(e, opt.tag);
149-
};
150-
n.onerror = function (e) {
151-
_this3.props.onError(e, opt.tag);
152-
};
153-
154-
this.notifications[opt.tag] = n;
137+
if (this.props.swRegistration && this.props.swRegistration.showNotification) {
138+
this.props.swRegistration.showNotification(this.props.title, opt);
139+
this.notifications[opt.tag] = {};
140+
} else {
141+
var n = new window.Notification(this.props.title, opt);
142+
n.onshow = function (e) {
143+
_this3.props.onShow(e, opt.tag);
144+
setTimeout(function () {
145+
_this3.close(n);
146+
}, _this3.props.timeout);
147+
};
148+
n.onclick = function (e) {
149+
_this3.props.onClick(e, opt.tag);
150+
};
151+
n.onclose = function (e) {
152+
_this3.props.onClose(e, opt.tag);
153+
};
154+
n.onerror = function (e) {
155+
_this3.props.onError(e, opt.tag);
156+
};
157+
158+
this.notifications[opt.tag] = n;
159+
}
155160
}
156161
}
157162

@@ -192,7 +197,8 @@ Notification.propTypes = {
192197
onError: _propTypes.func,
193198
timeout: _propTypes.number,
194199
title: _propTypes.string.isRequired,
195-
options: _propTypes.object
200+
options: _propTypes.object,
201+
swRegistration: _propTypes.object
196202
};
197203

198204
Notification.defaultProps = {
@@ -207,7 +213,8 @@ Notification.defaultProps = {
207213
onClose: function onClose() {},
208214
onError: function onError() {},
209215
timeout: 5000,
210-
options: {}
216+
options: {},
217+
swRegistration: null
211218
};
212219

213220
exports.default = Notification;

src/components/Notification.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,23 @@ class Notification extends React.Component {
101101
}
102102

103103
if (!this.notifications[opt.tag]) {
104-
let n = new window.Notification(this.props.title, opt);
105-
n.onshow = (e) => {
106-
this.props.onShow(e, opt.tag);
107-
setTimeout(() => {
108-
this.close(n);
109-
}, this.props.timeout);
110-
};
111-
n.onclick = (e) => {this.props.onClick(e, opt.tag); };
112-
n.onclose = (e) => {this.props.onClose(e, opt.tag); };
113-
n.onerror = (e) => {this.props.onError(e, opt.tag); };
114-
115-
this.notifications[opt.tag] = n;
104+
if (this.props.swRegistration && this.props.swRegistration.showNotification) {
105+
this.props.swRegistration.showNotification(this.props.title, opt)
106+
this.notifications[opt.tag] = {};
107+
} else {
108+
const n = new window.Notification(this.props.title, opt);
109+
n.onshow = e => {
110+
this.props.onShow(e, opt.tag);
111+
setTimeout(() => {
112+
this.close(n);
113+
}, this.props.timeout);
114+
};
115+
n.onclick = e => { this.props.onClick(e, opt.tag); };
116+
n.onclose = e => { this.props.onClose(e, opt.tag); };
117+
n.onerror = e => { this.props.onError(e, opt.tag); };
118+
119+
this.notifications[opt.tag] = n;
120+
}
116121
}
117122
}
118123

@@ -148,7 +153,8 @@ Notification.propTypes = {
148153
onError: func,
149154
timeout: number,
150155
title: string.isRequired,
151-
options: object
156+
options: object,
157+
swRegistration: object,
152158
};
153159

154160
Notification.defaultProps = {
@@ -163,7 +169,8 @@ Notification.defaultProps = {
163169
onClose: () => {},
164170
onError: () => {},
165171
timeout: 5000,
166-
options: {}
172+
options: {},
173+
swRegistration: null,
167174
};
168175

169176
export default Notification;

test/components/Notification_spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,25 @@ describe('Test of Notification', () => {
288288
expect(args[1]).to.be.eql('mytag');
289289
});
290290
});
291+
292+
describe('when swRegistration prop is defined', () => {
293+
const swRegistrationMock = { showNotification: sinon.stub().resolves({ notification: ee }) }
294+
const MY_TITLE = 'mytitle';
295+
const MY_OPTIONS = {
296+
tag: 'mytag',
297+
body: 'mybody',
298+
icon: 'myicon',
299+
lang: 'en',
300+
dir: 'ltr',
301+
requireInteraction: true,
302+
};
303+
304+
it('does not trigger Notification but trigger swRegistration.showNotification', () => {
305+
component = ReactTestUtils.renderIntoDocument(<Notification title={MY_TITLE} options={MY_OPTIONS} swRegistration={swRegistrationMock} />);
306+
expect(stubConstructor.calledWithNew()).to.be.eql(false);
307+
expect(swRegistrationMock.showNotification.calledWith(MY_TITLE, MY_OPTIONS)).to.be.eql(true);
308+
});
309+
});
291310
});
292311
});
293312
});

0 commit comments

Comments
 (0)