-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathPushNotificationService.java
More file actions
217 lines (176 loc) · 8.21 KB
/
PushNotificationService.java
File metadata and controls
217 lines (176 loc) · 8.21 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
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
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.impl.android;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import com.codename1.push.PushCallback;
import com.codename1.ui.Display;
/**
* This class implements a push notification fallback service for applications that require
* push notification support but don't have Android Market installed
*
* @author Shai Almog
*/
public abstract class PushNotificationService extends Service implements PushCallback {
private java.util.Map<String,String> properties = new java.util.HashMap<String,String>();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
}
private String getProperty(String propertyName, String defaultValue) {
String val = properties.get(propertyName);
if (val == null) {
return defaultValue;
}
return val;
}
public void setProperty(String propertyName, String val) {
properties.put(propertyName, val);
}
public static void startServiceIfRequired(Class service, Context ctx) {
if(!AndroidImplementation.hasAndroidMarket(ctx)) {
SharedPreferences sh = ctx.getSharedPreferences("C2DMNeeded", Context.MODE_PRIVATE);
if(sh.getBoolean("C2DMNeeded", false)) {
Intent i = new Intent();
i.setAction(service.getClass().getName());
ctx.startService(i);
}
}
}
public static void forceStartService(String service, Context ctx) {
if(!AndroidImplementation.hasAndroidMarket(ctx)) {
SharedPreferences sh = ctx.getSharedPreferences("C2DMNeeded", Context.MODE_PRIVATE);
Editor editor = sh.edit();
editor.putBoolean("C2DMNeeded", true);
editor.commit();
Intent i = new Intent();
i.setAction(service);
ctx.startService(i);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
AndroidImplementation.registerPolling();
return START_STICKY;
}
@Override
public void onDestroy() {
AndroidImplementation.stopPollingLoop();
}
public abstract PushCallback getPushCallbackInstance();
public abstract Class getStubClass();
@Override
public void push(final String value) {
final PushCallback callback = getPushCallbackInstance();
if(callback != null) {
final boolean delayPushCompletion = "true".equals(Display.getInstance().getProperty("delayPushCompletion", "false")) ||
"true".equals(Display.getInstance().getProperty("android.delayPushCompletion", "false"));
if (delayPushCompletion) {
AndroidImplementation.acquirePushWakeLock(30000);
}
Display.getInstance().callSerially(new Runnable() {
public void run() {
try {
callback.push(value);
} finally {
if (!delayPushCompletion) {
Display.getInstance().notifyPushCompletion();
}
}
}
});
} else {
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent newIntent = new Intent(this, getStubClass());
PendingIntent contentIntent = AndroidImplementation.createPendingIntent(this, 0, newIntent);
Notification.Builder builder = new Notification.Builder(this)
.setContentIntent(contentIntent)
.setSmallIcon(android.R.drawable.stat_notify_sync)
.setTicker(value)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setContentTitle(value)
.setDefaults(Notification.DEFAULT_ALL);
// The following section is commented out so that builds against SDKs below 26
// won't fail.
/*<SDK26>
if(android.os.Build.VERSION.SDK_INT >= 21){
builder.setCategory("Notification");
}
if (android.os.Build.VERSION.SDK_INT >= 26) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = getProperty("android.NotificationChannel.id", "cn1-channel");
CharSequence name = getProperty("android.NotificationChannel.name", "Notifications");
String description = getProperty("android.NotificationChannel.description", "Remote notifications");
int importance = Integer.parseInt(getProperty("android.NotificationChannel.importance", ""+NotificationManager.IMPORTANCE_HIGH));
android.app.NotificationChannel mChannel = new android.app.NotificationChannel(id, name,importance);
mChannel.setDescription(description);
mChannel.enableLights(Boolean.parseBoolean(getProperty("android.NotificationChannel.enableLights", "true")));
mChannel.setLightColor(Integer.parseInt(getProperty("android.NotificationChannel.lightColor", ""+android.graphics.Color.RED)));
mChannel.enableVibration(Boolean.parseBoolean(getProperty("android.NotificationChannel.enableVibration", "false")));
String vibrationPatternStr = getProperty("android.NotificationChannel.vibrationPattern", null);
if (vibrationPatternStr != null) {
String[] parts = vibrationPatternStr.split(",");
int len = parts.length;
long[] pattern = new long[len];
for (int i=0; i<len; i++) {
pattern[i] = Long.parseLong(parts[i].trim());
}
mChannel.setVibrationPattern(pattern);
}
mNotificationManager.createNotificationChannel(mChannel);
System.out.println("Setting push channel to "+id);
builder.setChannelId(id);
}
</SDK26>*/
Notification notif = builder.build();
int notifId = getNotifyId();//(int)System.currentTimeMillis();
//notif.extras.putInt("notificationId", notifId);
nm.notify(notifId, notif);
}
}
static int getNotifyId() {
return 1;
}
static void cancelNotification(Context context) {
NotificationManager nm = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
nm.cancel(getNotifyId());
}
@Override
public void registeredForPush(String deviceId) {
}
@Override
public void pushRegistrationError(String error, int errorCode) {
}
}