-
Notifications
You must be signed in to change notification settings - Fork 0
/
localytics-push-notification.js
135 lines (127 loc) · 4.72 KB
/
localytics-push-notification.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var request = require('request');
module.exports = function () {
this.id = "localytics-push-notification"; // An id given to uniquely identify the activity.
this.label = "Send Push Notification"; // Label means name given to the activity, will be used as title.
this.input = { // JSON schema . Structure can be checked on http://json-schema.org/
"title": "Send Push Notification", // String , mandatory
"type": "object", // Do not change
//Inputs which the activity will take along with their validation rules(if any)
"properties": {
"access_token": {
"type": "string",
"title": "Authorize Localytics",
"oauth": "localytics",
"minLength": 1
},
"key": {
"title": "Key",
"type": "string",
"description": "API Keys for Localytics Push Notification"
},
"secret": {
"title": "App Secret",
"type": "string",
"description": "App Secret for Localytics Push Notification"
},
"target_type": {
"title": "Target Type",
"type": "string",
"description": "Specifying the Target type "
},
"target": {
"title": "Target",
"type": "string",
"description": "Target Type for Push Notification"
},
"title": {
"title": "Title",
"type": "string",
"description": "Title of the Notification"
},
"body": {
"title": "Body",
"type": "string",
"description": "Text Body of the Push Notification"
}
}
};
// will contain the text for the tooltip shown besides the activity title
this.help = "API for Localytics Push Notification";
this.output = { // JSON schema . Structure can be checked on http://json-schema.org/
"title": "output",
"type": "object",
"properties": {
"message": {
"title": "message",
"type": "string",
"displayTitle": "Message"
},
"result": {
"title": "Result",
"type": "string",
"displayTitle": "Result"
}
},
"displayTitle": "Output"
};
this.execute = function (input, output) {
// the core logic for the activity. this would be executed inside the flow engine.
//user can write anything norestriction.
// inputs parameter will provide you with the inputs given by the user to the activity
// output is function used to indicate the engine that activity execution is completed.
// syntax output(error,data,[logs])
// if error then use (error,data,[logs])
// if no error then use (null,data,[logs])
var app_id = input.access_token;
var body = {
"target_type": input.target_type,
"messages": [{
"target": input.target,
"alert": {
"title": input.title,
"body": input.body
},
"android": {
"priority": "normal"
},
"ios": {
"sound": "default.wav",
"badge": 1
}
}]
};
request({
method: 'POST',
url: 'https://messaging.localytics.com/v2/push/' + app_id,
body: body,
headers: {
Authorization: "Basic " + new Buffer(input.key + ':' + input.secret, 'utf8').toString('base64')
},
json: true
}, function (error, response, body) {
if (error) {
return output(error)
}
if (typeof (body) === 'string') {
try {
body = JSON.parse(body);
} catch (e) {
return output(e)
}
}
body.result = "Notification Send Successfully";
if (response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
return output(null, body);
}
if (response.statusCode && response.statusCode === 400) {
return output(body.error);
}
if (response.statusCode && response.statusCode === 401) {
return output(body.error);
}
if (response.statusCode && response.statusCode === 403) {
return output(body.error);
}
})
};
}