-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (81 loc) · 2.69 KB
/
index.js
File metadata and controls
95 lines (81 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*!
* afpushservice :: nodejs sdk
* Copyright (c) 2016 appfarms GmbH & Co. KG <info@appfarms.com>
* MIT Licensed
*/
"use strict"
var request = require('request');
const EventEmitter = require('events');
const util = require('util');
class AFPushService extends EventEmitter
{
constructor () {
super();
EventEmitter.call(this);
}
/**
* Method to send a push notification with given filter to appfarms push notification service
*
* @param apiKey - apiKey of the app you want to send the notification to
* @param title - message title (only Android and iOS Notification Center)
* @param message - notification message
* @param payload - custom payload
* @param filter - custom filter
*/
sendPushNotification (apiKey, title, message, payload, filter) {
// check if apiKey is string
if (typeof apiKey != "string" || apiKey.length <= 0)
{
this.emit("error", new Error("No valid API Key given :'"+ apiKey +"'"));
}
// check if title is string
if (title != null && (typeof title != "string" || title.length <= 0))
{
this.emit("error", new Error("No valid title given :'"+ title +"'"));
}
// check if message is string
if (typeof message != "string" || message.length <= 0)
{
this.emit("error", new Error("No valid message given :'"+ message +"'"));
}
// check if payload is object
if (payload != null && typeof payload != "object")
{
this.emit("error", new Error("No valid payload given :'"+ payload +"'"));
}
// check if filter is object
if (filter != null && typeof filter != "object" && typeof filter != "array")
{
this.emit("error", new Error("No valid filter given :'"+ filter +"'"));
}
var requestData = {
"restKey": apiKey,
"body": message
};
if (title != null)
{
requestData.title = title
}
if (payload != null)
{
requestData.payload = payload;
}
if (filter != null)
{
requestData.matchFilters = 1;
requestData.filters = (typeof filter != "object") ? [filter] : filter;
}
request({
method: "POST",
url : 'https://push.appfarms.com/api/notifications',
json : requestData
})
.on('response', (response) => {
this.emit('success', response);
})
.on('error', (err) => {
this.emit('error', err);
});
}
}
module.exports = AFPushService;