Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b9fb36

Browse files
author
Charlie Weems
committed
Refactor to class methods, update readme
1 parent 6b973c0 commit 8b9fb36

File tree

4 files changed

+110
-83
lines changed

4 files changed

+110
-83
lines changed

‎README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Disclaimer: this plugin is an independent project that is not supported by Twili
44

55
The Push Notifications Flex Plugin allows you to create desktop notifications for agents using Twilio Flex. Notifications occur when a task is reserved for an agent.
66

7+
Works in all browsers that support the Push API: https://caniuse.com/#feat=push-api
78

89
## Setup
910

@@ -12,12 +13,38 @@ Make sure you have [Node.js](https://nodejs.org) as well as [`npm`](https://npmj
1213
Afterwards install the dependencies by running `npm install`:
1314

1415
```bash
16+
git clone https://github.com/cweems/plugin-push-notifications.git
1517
cd plugin-push-notifications
1618

1719
# If you use npm
1820
npm install
1921
```
2022

23+
## Push API Options
24+
Add these to the `options` object in `showLocalNotification`.
25+
26+
Visual Options
27+
"body": "<String>",
28+
"icon": "<URL String>",
29+
"image": "<URL String>",
30+
"badge": "<URL String>",
31+
"vibrate": "<Array of Integers>",
32+
"sound": "<URL String>",
33+
"dir": "<String of 'auto' | 'ltr' | 'rtl'>",
34+
35+
Behavioural Options
36+
"tag": "<String>",
37+
"data": "<Anything>",
38+
"requireInteraction": "<boolean>",
39+
"renotify": "<Boolean>",
40+
"silent": "<Boolean>",
41+
42+
Visual & Behavioural Options
43+
"actions": "<Array of Strings>",
44+
45+
Information Option. No visual affect.
46+
"timestamp": "<Long>"
47+
2148
## Development
2249

2350
In order to develop locally, you can use the Webpack Dev Server by running:

‎public/img/flex_icon.png‎

47.1 KB
Loading[フレーム]

‎src/CustomTaskListComponent.js‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎src/PushNotificationsPlugin.js‎

Lines changed: 83 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,120 @@
11
import { FlexPlugin } from 'flex-plugin';
22
import React from 'react';
3-
import CustomTaskListComponent from './CustomTaskListComponent';
43

54
const PLUGIN_NAME = 'PushNotificationsPlugin';
65

76
export default class PushNotificationsPlugin extends FlexPlugin {
87

9-
constructor() {
8+
constructor(flex,manager) {
109
super(PLUGIN_NAME);
10+
11+
this.currentTasks = [];
12+
this.manager = null;
13+
this.registration = null;
14+
this.permission = null;
15+
16+
this.taskListener = this.taskListener.bind(this);
17+
this.registerServiceWorker = this.registerServiceWorker.bind(this);
18+
this.registerSuccess = this.registerSuccess.bind(this);
19+
this.init = this.init.bind(this)
1120
}
1221

1322
/**
14-
* This code is run when your plugin is being started
15-
* Use this to modify any UI components or attach to the actions framework
23+
* Generates the message that will display in the push alert
1624
*
17-
* @param flex { typeof import('@twilio/flex-ui') }
18-
* @param manager { import('@twilio/flex-ui').Manager }
25+
* @param newTask { 'task triggering the push' }
26+
* @param allTasks { 'all task reservations for user' }
1927
*/
2028

29+
alertMessage(newTask, allTasks) {
2130

22-
init(flex, manager) {
23-
let permission;
24-
let registration;
25-
let currentTasks = [];
31+
let currentTask = allTasks.get(newTask);
32+
let name = currentTask._task.attributes.name;
33+
let channel = currentTask._task.attributes.channelType;
2634

27-
functionalertMessage(newTask,allTasks){
28-
console.log(newTask,allTasks);
35+
return`New ${channel} task from ${name}`;
36+
}
2937

30-
let currentTask = allTasks.get(newTask);
31-
let name = currentTask._task.attributes.name;
32-
let channel = currentTask._task.attributes.channelType;
38+
/**
39+
* Subscribes to the redux store.
40+
* Fires a notification when a new task SID is added.
41+
*/
3342

34-
return `New ${channel} task from ${name}`;
35-
}
43+
taskListener() {
44+
const nextState = this.manager.store.getState();
45+
const nextTasks = nextState.flex.worker.tasks;
46+
const nextTaskKeys = Array.from(nextTasks.keys());
3647

37-
function taskListener() {
38-
let nextState = manager.store.getState();
39-
let nextTasks = nextState.flex.worker.tasks;
40-
let nextTaskKeys = Array.from(nextTasks.keys());
48+
for(let i = 0; i < nextTaskKeys.length; i++) {
49+
if (!this.currentTasks.includes(nextTaskKeys[i])) {
50+
this.currentTasks = nextTaskKeys;
4151

42-
console.log(nextState.flex.worker.tasks);
52+
const newTask = nextTaskKeys[i];
53+
const message = this.alertMessage(newTask, nextTasks);
4354

44-
for(var i = 0; i < nextTaskKeys.length; i++) {
45-
if (!currentTasks.includes(nextTaskKeys[i])) {
46-
currentTasks = nextTaskKeys;
55+
if(this.registration) {
56+
this.showLocalNotification('Twilio Flex', message, this.registration);
57+
}
58+
console.log('New task')
59+
}
60+
}
61+
}
4762

48-
let newTask = nextTaskKeys[i];
49-
let message = alertMessage(newTask, nextTasks);
63+
showLocalNotification(title, body, swRegistration) {
64+
const options = {
65+
body,
66+
"icon": "/img/flex_icon.png",
67+
// here you can add more properties like icon, image, vibrate, etc.
68+
};
5069

51-
if(registration) {
52-
showLocalNotification('Twilio Flex', message, registration);
53-
}
54-
console.log('New task')
55-
}
56-
}
57-
}
70+
swRegistration.showNotification(title, options);
71+
}
5872

59-
const requestNotificationPermission = async () => {
60-
const permission = await window.Notification.requestPermission();
61-
// value of permission can be 'granted', 'default', 'denied'
62-
// granted: user has accepted the request
63-
// default: user has dismissed the notification permission popup by clicking on x
64-
// denied: user has denied the request.
65-
if(permission !== 'granted'){
66-
throw new Error('Permission not granted for Notification');
67-
}
73+
registerServiceWorker() {
74+
if (!('serviceWorker' in navigator)) {
75+
return;
6876
}
6977

70-
function showLocalNotification(title, body, swRegistration) {
71-
const options = {
72-
body,
73-
// here you can add more properties like icon, image, vibrate, etc.
74-
};
75-
76-
swRegistration.showNotification(title, options);
78+
if (!('PushManager' in window)) {
79+
return;
7780
}
7881

79-
function registerServiceWorker() {
80-
if (!('serviceWorker' in navigator)) {
81-
return;
82-
}
83-
84-
if (!('PushManager' in window)) {
85-
return;
86-
}
87-
88-
return navigator.serviceWorker.register('/agent-desktop/service-worker.js')
82+
return navigator.serviceWorker.register('/agent-desktop/service-worker.js')
8983
.then(function(reg) {
90-
console.log('Service worker successfully registered.');
91-
registration = reg;
92-
permission = requestNotificationPermission();
93-
return registration;
94-
})
84+
this.registerSuccess(reg);
85+
}.bind(this))
9586
.catch(function(err) {
9687
console.error('Unable to register service worker.', err);
9788
});
98-
}
89+
}
9990

100-
registerServiceWorker();
101-
const listen = manager.store.subscribe(taskListener);
91+
registerSuccess(reg) {
92+
this.registration = reg;
93+
this.permission = this.requestNotificationPermission();
94+
console.log('Service worker successfully registered.');
10295

103-
flex.AgentDesktopView.Panel1.Content.add(
104-
<CustomTaskListComponent key="demo-component" />,
105-
{
106-
sortOrder: -1,
96+
return this.registration;
97+
}
98+
99+
/**
100+
* value of permission can be 'granted', 'default', 'denied'
101+
*/
102+
103+
async requestNotificationPermission () {
104+
const permission = await window.Notification.requestPermission();
105+
// value of permission can be 'granted', 'default', 'denied'
106+
// granted: user has accepted the request
107+
// default: user has dismissed the notification permission popup by clicking on x
108+
// denied: user has denied the request.
109+
if(permission !== 'granted'){
110+
throw new Error('Permission not granted for Notification');
107111
}
108-
);
112+
}
113+
114+
init(flex, manager) {
115+
this.manager = manager;
116+
this.registerServiceWorker();
117+
118+
const listen = manager.store.subscribe(this.taskListener);
109119
}
110120
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /