-
Notifications
You must be signed in to change notification settings - Fork 34
/
IntentService.java
executable file
·375 lines (308 loc) · 10.4 KB
/
IntentService.java
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package ti.goosh;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.util.concurrent.atomic.AtomicInteger;
import java.lang.reflect.Type;
import java.lang.Math;
import java.util.Random;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.util.TiRHelper;
import com.google.android.gms.gcm.GcmListenerService;
public class IntentService extends GcmListenerService {
private static final String LCAT = "ti.goosh.IntentService";
private static final AtomicInteger atomic = new AtomicInteger(0);
@Override
public void onMessageReceived(String from, Bundle bundle) {
Log.d(LCAT, "Push notification received from: " + from);
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d(LCAT, String.format("Notification key : %s => %s (%s)", key, value.toString(), value.getClass().getName()));
}
parseNotification(bundle);
}
private int getResource(String type, String name) {
int icon = 0;
if (name != null) {
int index = name.lastIndexOf(".");
if (index > 0) name = name.substring(0, index);
try {
icon = TiRHelper.getApplicationResource(type + "." + name);
} catch (TiRHelper.ResourceNotFoundException ex) {
Log.e(LCAT, type + "." + name + " not found; make sure it's in platform/android/res/" + type);
}
}
return icon;
}
private Bitmap getBitmapFromURL(String src) throws Exception {
HttpURLConnection connection = (HttpURLConnection)(new URL(src)).openConnection();
connection.setDoInput(true);
connection.setUseCaches(false); // Android BUG
connection.connect();
return BitmapFactory.decodeStream( new BufferedInputStream( connection.getInputStream() ) );
}
private void parseNotification(Bundle bundle) {
Context context = getApplicationContext();
Boolean appInBackground = !TiApplication.isCurrentActivityInForeground();
Boolean showNotification = true;
String jsonData = bundle.getString("data");
JsonObject data = null;
try {
data = (JsonObject) new Gson().fromJson(jsonData, JsonObject.class);
} catch (Exception ex) {
Log.e(LCAT, "Error parsing data JSON: " + ex.getMessage());
return;
}
if (data != null && data.has("alert") == true) {
if (appInBackground) {
showNotification = true;
} else {
if (data.has("force_show_in_foreground")) {
JsonPrimitive showInFore = data.getAsJsonPrimitive("force_show_in_foreground");
showNotification = ((showInFore.isBoolean() && showInFore.getAsBoolean() == true));
} else {
showNotification = false;
}
}
} else {
Log.i(LCAT, "Not showing notification cause missing data.alert");
showNotification = false;
}
if (showNotification) {
Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra(TiGooshModule.INTENT_EXTRA, jsonData);
PendingIntent contentIntent = PendingIntent.getActivity(this, new Random().nextInt(), notificationIntent, PendingIntent.FLAG_ONE_SHOT);
// Start building notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
int builder_defaults = 0;
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
builder.setPriority(2);
// Body
String alert = null;
if (data.has("alert")) {
alert = data.getAsJsonPrimitive("alert").getAsString();
builder.setContentText(alert);
builder.setTicker(alert);
}
// BigText
String big_text = null;
if (data.has("big_text")) {
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.bigText( data.getAsJsonPrimitive("big_text").getAsString() );
if (data.has("big_text_summary")) {
bigTextStyle.setSummaryText( data.getAsJsonPrimitive("big_text_summary").getAsString() );
}
builder.setStyle(bigTextStyle);
}
// Icons
try {
int smallIcon = this.getResource("drawable", "notificationicon");
if (smallIcon > 0) {
builder.setSmallIcon(smallIcon);
}
} catch (Exception ex) {
Log.e(LCAT, "Smallicon exception: " + ex.getMessage());
}
// Large icon
if (data.has("icon")) {
try {
Bitmap icon = this.getBitmapFromURL( data.getAsJsonPrimitive("icon").getAsString() );
builder.setLargeIcon(icon);
} catch (Exception ex) {
Log.e(LCAT, "Icon exception: " + ex.getMessage());
}
}
// Color
if (data.has("color")) {
try {
int color = Color.parseColor( data.getAsJsonPrimitive("color").getAsString() );
builder.setColor( color );
} catch (Exception ex) {
Log.e(LCAT, "Color exception: " + ex.getMessage());
}
}
// Title
if (data.has("title")) {
builder.setContentTitle( data.getAsJsonPrimitive("title").getAsString() );
} else {
builder.setContentTitle( TiApplication.getInstance().getAppInfo().getName() );
}
// Badge
if (data.has("badge")) {
int badge = data.getAsJsonPrimitive("badge").getAsInt();
BadgeUtils.setBadge(context, badge);
builder.setNumber(badge);
}
// Sound
if (data.has("sound")) {
JsonPrimitive sound = data.getAsJsonPrimitive("sound");
if ( ("default".equals(sound.getAsString())) || (sound.isBoolean() && sound.getAsBoolean() == true) ) {
builder_defaults |= Notification.DEFAULT_SOUND;
} else {
int resource = getResource("raw", data.getAsJsonPrimitive("sound").getAsString());
builder.setSound( Uri.parse("android.resource://" + context.getPackageName() + "/" + resource) );
}
}
// Vibration
if (data.has("vibrate")) {
try {
JsonElement vibrateJson = data.get("vibrate");
if (vibrateJson.isJsonPrimitive()) {
JsonPrimitive vibrate = vibrateJson.getAsJsonPrimitive();
if (vibrate.isBoolean() && vibrate.getAsBoolean() == true) {
builder_defaults |= Notification.DEFAULT_VIBRATE;
}
} else if (vibrateJson.isJsonArray()) {
JsonArray vibrate = vibrateJson.getAsJsonArray();
if (vibrate.size() > 0) {
long[] pattern = new long[vibrate.size()];
int i = 0;
for(i = 0; i < vibrate.size(); i++) {
pattern[i] = vibrate.get(i).getAsLong();
}
builder.setVibrate(pattern);
}
}
} catch(Exception ex) {
Log.e(LCAT, "Vibrate exception: " + ex.getMessage());
}
}
// Lights
if (data.has("lights")) {
try {
JsonElement lightsJson = data.get("lights");
if (lightsJson.isJsonObject()) {
JsonObject lights = lightsJson.getAsJsonObject();
int argb = Color.parseColor(lights.get("argb").getAsString());
int onMs = lights.get("onMs").getAsInt();
int offMs = lights.get("offMs").getAsInt();
if (-1 != argb && -1 != onMs && -1 != offMs) {
builder.setLights(argb, onMs, offMs);
}
}
} catch(Exception ex) {
Log.e(LCAT, "Lights exception: " + ex.getMessage());
}
} else {
builder_defaults |= Notification.DEFAULT_LIGHTS;
}
// Ongoing
if (data.has("ongoing")) {
try {
JsonElement ongoingJson = data.get("ongoing");
if (ongoingJson.isJsonPrimitive()) {
Boolean ongoing = ongoingJson.getAsBoolean();
builder.setOngoing(ongoing);
}
} catch(Exception ex) {
Log.e(LCAT, "Ongoing exception: " + ex.getMessage());
}
} else {
builder_defaults |= Notification.DEFAULT_LIGHTS;
}
// Group
if (data.has("group")) {
try {
JsonElement groupJson = data.get("group");
if (groupJson.isJsonPrimitive()) {
String group = groupJson.getAsString();
builder.setGroup(group);
}
} catch(Exception ex) {
Log.e(LCAT, "Group exception: " + ex.getMessage());
}
} else {
builder_defaults |= Notification.DEFAULT_LIGHTS;
}
// GroupSummary
if (data.has("group_summary")) {
try {
JsonElement groupsumJson = data.get("group_summary");
if (groupsumJson.isJsonPrimitive()) {
Boolean groupsum = groupsumJson.getAsBoolean();
builder.setGroupSummary(groupsum);
}
} catch(Exception ex) {
Log.e(LCAT, "Group summary exception: " + ex.getMessage());
}
} else {
builder_defaults |= Notification.DEFAULT_LIGHTS;
}
// When
if (data.has("when")) {
try {
JsonElement whenJson = data.get("when");
if (whenJson.isJsonPrimitive()) {
int when = whenJson.getAsInt();
builder.setWhen(when);
}
} catch(Exception ex) {
Log.e(LCAT, "When exception: " + ex.getMessage());
}
} else {
builder_defaults |= Notification.DEFAULT_LIGHTS;
}
// Only alert once
if (data.has("only_alert_once")) {
try {
JsonElement oaoJson = data.get("only_alert_once");
if (oaoJson.isJsonPrimitive()) {
Boolean oao = oaoJson.getAsBoolean();
builder.setOnlyAlertOnce(oao);
}
} catch(Exception ex) {
Log.e(LCAT, "Only alert once exception: " + ex.getMessage());
}
} else {
builder_defaults |= Notification.DEFAULT_LIGHTS;
}
// Builder defaults OR
builder.setDefaults(builder_defaults);
// Tag
String tag = null;
if (data.has("tag")) {
tag = data.getAsJsonPrimitive("tag").getAsString();
}
// Nid
int id = 0;
if (data.has("id")) {
// ensure that the id sent from the server is negative to prevent
// collision with the atomic integer
id = -1 * Math.abs(data.getAsJsonPrimitive("id").getAsInt());
} else {
id = atomic.getAndIncrement();
}
// Send
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(tag, id, builder.build());
}
if (!appInBackground && TiGooshModule.getInstance() != null) {
TiGooshModule.getInstance().sendMessage(jsonData, appInBackground);
}
}
}