Skip to content

Instantly share code, notes, and snippets.

@nxsyed
Created July 12, 2018 20:21
Show Gist options
  • Save nxsyed/a4a2b37bb6857db47ab2af71103d6e94 to your computer and use it in GitHub Desktop.
Save nxsyed/a4a2b37bb6857db47ab2af71103d6e94 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import PubNubReact from 'pubnub-react';
import { Button, Input, List } from 'antd';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.pubnub = new PubNubReact({
publishKey: 'Your Pub Key',
subscribeKey: 'Your Sub Key'
});
this.pubnub.init(this);
}
componentWillMount() {
this.pubnub.subscribe({
channels: ['Alexa']
});
this.pubnub.getMessage('notify', (msg) => {
console.log(msg);
this.notify(msg.message);
});
}
publish(userMessage) {
this.pubnub.publish(
{
message: {
message:userMessage
},
channel: 'Alexa',
storeInHistory: true
},
(status, response) => {
// handle status, response
}
);
}
notify(message) {
if (!("Notification" in window)) {
alert("This browser does not support system notifications");
}
else if (Notification.permission === "granted") {
if(typeof message === 'string' || message instanceof String){
var notification = new Notification(message);
}else{
var notification = new Notification("Hello World");
}
}
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if (permission === "granted") {
var notification = new Notification("Hello World");
}
});
}
}
render() {
const messages = [
'Ok I\'ll be there in 5 mins',
'Call my number 415-xxx-6631',
'Alright, the door is open'
];
return (
<div className="App">
<List
header={<div>Choose a preselected response</div>}
bordered
dataSource={messages}
renderItem={item => (<List.Item>
<Button onClick={this.publish.bind(this, item)} type="primary">{item}</Button>
</List.Item>)
}
/>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment