-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lucasnakano
committed
May 6, 2019
1 parent
c4c662f
commit 464a3cc
Showing
10 changed files
with
1,573 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"ONESIGNAL_URL": "https://onesignal.com/api/v1/notifications", | ||
"ONESIGNAL_KEY": "", | ||
"ONESIGNAL_TOKEN": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"ONESIGNAL_URL": "https://onesignal.com/api/v1/notifications", | ||
"ONESIGNAL_KEY": "", | ||
"ONESIGNAL_TOKEN": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports.messageToOneSignal = (message) => { | ||
let payload = { | ||
'include_player_ids': message.provider_id, | ||
'app_id': process.env.ONESIGNAL_KEY, | ||
'contents': { 'en': message.message }, | ||
'url': message.deep_link, | ||
'data': { 'uri': message.deep_link }, | ||
'headings': { 'en': message.title }, | ||
} | ||
|
||
if (Object.keys(message).includes('image_url')) { | ||
payload['ios_attachments'] = { 'thumb': message.image_url } | ||
} | ||
|
||
return payload | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const {OneSignal} = require('./pushService.js') | ||
const {messageToOneSignal} = require('./converter.js') | ||
const toParseBody = (record) => JSON.parse(record.body) | ||
|
||
module.exports.handler = async (event, context) => { | ||
|
||
console.log(`Received event ${JSON.stringify(event)}`) | ||
|
||
const token = process.env.ONESIGNAL_TOKEN | ||
const url = process.env.ONESIGNAL_URL | ||
|
||
const sender = new OneSignal(token, url) | ||
|
||
const push = (message) => sender.send(message) | ||
|
||
const requests = event | ||
.Records | ||
.map(toParseBody) | ||
.map(messageToOneSignal) | ||
.map(push) | ||
|
||
await Promise.all(requests) | ||
|
||
return {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const fetch = require('node-fetch') | ||
|
||
class OneSignalError extends Error {} | ||
|
||
class OneSignal { | ||
constructor(token, url) { | ||
this._token = token | ||
this._url = url | ||
} | ||
|
||
async send(payload) { | ||
const response = await this._sendOneSignal(payload) | ||
const body = await response.json() | ||
const isSuccessResponse = this._isSuccess(response, body) | ||
|
||
console.log(`Onesignal response with ${response.status} and with body ${JSON.stringify(body)}.`) | ||
|
||
if(!isSuccessResponse) { | ||
const textPayload = JSON.stringify(payload) | ||
const textResponseBody = JSON.stringify(body) | ||
|
||
console.log(`[SEND_PUSH][ONE_SIGNAL][FAIL] Cannot send this payload ${textPayload} to onesignal. Retrive ${response.status} as status code and ${textResponseBody} as body`) | ||
|
||
throw new OneSignalError(`[SEND_PUSH][ONE_SIGNAL][FAIL] Cannot send this payload ${textPayload} to onesignal. Retrive ${response.status} as status code and ${textResponseBody} as body`) | ||
} | ||
|
||
console.log('Success Finish') | ||
|
||
return response | ||
} | ||
|
||
_isSuccess(response, body) { | ||
if (response.ok) { | ||
const hasErrorsField = Object.keys(body).includes('errors') | ||
|
||
return !hasErrorsField | ||
} | ||
|
||
return false | ||
} | ||
|
||
_sendOneSignal(payload) { | ||
const request = { | ||
method: 'POST', | ||
headers: { | ||
"Content-type": "application/json; charset=UTF-8", | ||
"Authorization": `Basic ${this._token}` | ||
}, | ||
|
||
body: JSON.stringify(payload) | ||
} | ||
|
||
console.log(`Request onesignal in this url '${this._url}' with this configuration ${JSON.stringify(request)}`) | ||
|
||
return fetch(this._url, request) | ||
} | ||
} | ||
|
||
module.exports.OneSignal = OneSignal | ||
module.exports.OneSignalError = OneSignalError |
Oops, something went wrong.