-
Notifications
You must be signed in to change notification settings - Fork 1
/
GCMIntentService.java
281 lines (214 loc) · 11.2 KB
/
GCMIntentService.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
package com.malcom.library.android.module.notifications.gcm;
import static com.malcom.library.android.module.notifications.MCMNotificationModule.applicationCode;
import static com.malcom.library.android.module.notifications.MCMNotificationModule.applicationSecretkey;
import static com.malcom.library.android.module.notifications.MCMNotificationModule.environmentType;
import static com.malcom.library.android.module.notifications.MCMNotificationModule.senderId;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Random;
import java.util.Set;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;
import com.malcom.library.android.MCMDefines;
import com.malcom.library.android.exceptions.ApplicationPackageNotFoundException;
import com.malcom.library.android.module.notifications.MCMNotificationModule;
/**
* Service responsible for handling GCM messages.
*
* @author Malcom Ventures, S.L.
* @since 2012
*/
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "Malcom GCMIntentService";
private static final Random rnd = new Random();
private static PowerManager.WakeLock wakeLock;
private static int idNotification;
public GCMIntentService() {
super(senderId);
idNotification = 0;
}
protected String getSenderId(Context context) {
final SharedPreferences prefs = context.getSharedPreferences( "GCM_SETTINGS", 0);
final String id = prefs.getString("GCM_SENDER_ID", null );
return prefs.getString(id, null);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(MCMDefines.LOG_TAG, "Device registered: regId = " + registrationId);
// Get the stored registrationId and if it's not equal, update it at server
String regId = GCMRegistrar.getRegistrationId(context);
if (regId != null) {
if(!MalcomServerUtilities.register(context, registrationId, environmentType, applicationCode, applicationSecretkey)){
GCMRegistrar.unregister(context);
}
}
}
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(MCMDefines.LOG_TAG, "Device unregistered");
if (GCMRegistrar.isRegisteredOnServer(context)) {
MalcomServerUtilities.unregister(context, registrationId, applicationCode, applicationSecretkey);
} else {
// This callback results from the call to unregister made on
// MalcomServerUtilities when the registration to the server failed.
Log.i(MCMDefines.LOG_TAG, "Ignoring unregister callback");
}
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(MCMDefines.LOG_TAG, "Received a new notification message");
generateNotification(context.getApplicationContext(), intent);
}
@Override
public void onError(Context context, String errorId) {
Log.e(MCMDefines.LOG_TAG, "Received error: " + errorId);
}
/*
* Issues a notification to inform the user about the received notification.
*/
private static void generateNotification(Context context, Intent i) {
try {
int iconResId = getApplicationIcon(context);
long when = System.currentTimeMillis();
SharedPreferences prefs = context.getSharedPreferences( "GCM_SETTINGS", 0);
String title = prefs.getString(MCMNotificationModule.GCM_TITLE_NOTIFICATION, "");
Bundle extras = i.getExtras();
String message = (String)i.getExtras().get(MCMNotificationModule.ANDROID_MESSAGE_KEY);
try {
message = URLDecoder.decode(message, "UTF8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NullPointerException npe) {
npe.printStackTrace();
}
String webUrl = (String)i.getExtras().get(MCMNotificationModule.ANDROID_MESSAGE_KEY + "."
+ MCMNotificationModule.ANDROID_MESSAGE_RICHMEDIA_KEY);
String efficacyKey = (String)i.getExtras().get(MCMNotificationModule.ANDROID_MESSAGE_KEY + "."
+ MCMNotificationModule.ANDROID_MESSAGE_EFFICACY_KEY);
String segmentId = null;
if(i.getExtras().get(MCMNotificationModule.ANDROID_MESSAGE_KEY + "."
+ MCMNotificationModule.ANDROID_MESSAGE_SEGMENT_KEY)!=null){
segmentId = (String)i.getExtras().get(MCMNotificationModule.ANDROID_MESSAGE_KEY + "."
+ MCMNotificationModule.ANDROID_MESSAGE_SEGMENT_KEY);
}
Notification notification = new Notification(iconResId, message, when);
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Vibration
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
// Sound
String soundName = (String)i.getExtras().get(MCMNotificationModule.ANDROID_MESSAGE_KEY + "."
+ MCMNotificationModule.ANDROID_NOTIFICATION_SOUND_KEY);
int notificationSoundId = 0;
if (soundName != null) {
notificationSoundId = context.getResources().getIdentifier(soundName , "raw", context.getPackageName());
}
if (notificationSoundId != 0) {
notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/"+notificationSoundId);
} else {
notification.defaults |= Notification.DEFAULT_SOUND;
}
String className = prefs.getString("GCM_CLASS", null );
Class classNotification = null;
try {
classNotification = Class.forName(className);
} catch (ClassNotFoundException e) {
Log.d(MCMDefines.LOG_TAG, "Class not found: "+className);
}
if (classNotification == null) {
classNotification = GCMIntentService.class;
}
Intent notificationIntent = new Intent(context, classNotification);
//Required to avoid getting the same intent in the activity each time. Each message is different so
//the intent should be different (the action) so extras can be different.
notificationIntent.setAction((String.valueOf(rnd.nextLong())+message));
notificationIntent.putExtra(MalcomNotificationReceiver.NOTIFICATION_SHOW_ALERT_KEY, new Boolean(true));
notificationIntent.putExtra(MCMNotificationModule.ANDROID_MESSAGE_EFFICACY_KEY,efficacyKey);
if(segmentId!=null)
notificationIntent.putExtra(MCMNotificationModule.ANDROID_MESSAGE_SEGMENT_KEY,segmentId);
notificationIntent.putExtra(MCMNotificationModule.ANDROID_MESSAGE_KEY, message);
notificationIntent.putExtra(MCMNotificationModule.ANDROID_MESSAGE_RICHMEDIA_KEY, webUrl);
// Add custom fields
Set<String> keys = i.getExtras().keySet();
for (String key:keys) {
Object o = i.getExtras().get(key);
notificationIntent.putExtra(key, (String)o);
}
//Set intent so it does not start a new activity
//
//Notes:
// - The flag FLAG_ACTIVITY_SINGLE_TOP makes that only one instance of the activity exists(each time the
// activity is summoned no onCreate() method is called instead, onNewIntent() is called.
// - If we use FLAG_ACTIVITY_CLEAR_TOP it will make that the last "snapshot"/TOP of the activity it will
// be this called this intent. We do not want this because the HOME button will call this "snapshot".
// To avoid this behaviour we use FLAG_ACTIVITY_BROUGHT_TO_FRONT that simply takes to foreground the
// activity.
//
//See http://developer.android.com/reference/android/content/Intent.html
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message, intent);
//This makes the device to wake-up is is idle with the screen off.
wakeUp(context);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Random randomGenerator = new Random();
idNotification = randomGenerator.nextInt(1000);
notificationManager.notify(idNotification, notification);
Log.d(MCMDefines.LOG_TAG, "Notification created for the recieve PUSH message.");
} catch (ApplicationPackageNotFoundException e) {
Log.e(MCMDefines.LOG_TAG, "The notification could not be created because it is not possible to locate the application icon.");
}
}
/*
* gets the application Icon.
*
* @param context
* @return
* @throws ApplicationPackageNotFoundException
*/
private static int getApplicationIcon(Context context) throws ApplicationPackageNotFoundException{
try {
//ApplicationInfo app = context.getPackageManager().getApplicationInfo(MCMCoreAdapter.applicationPackage, 0);
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
ApplicationInfo app = context.getPackageManager().getApplicationInfo(packageInfo.packageName, 0);
return app.icon;
} catch (NameNotFoundException e) {
Log.e(MCMDefines.LOG_TAG, "Application package not found!.");
throw new ApplicationPackageNotFoundException();
}
}
/*
* Makes the device to wake-up. yeah!
*
* @param ctx
*/
public static void wakeUp(Context ctx) {
// Log.d(MCMDefines.LOG_TAG, "Wake up!.");
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if(isScreenOn==false) {
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"malcom_library_wakeup");
wl.acquire(10000);
PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"malcom_library_cpuwakeup");
wl_cpu.acquire(10000);
}
}
}