Skip to content

Commit 5d1824d

Browse files
committedJun 19, 2018
Improved PushNotificationService to support changes on Android API 26+ that require the use of a NotificationChannel. This change is only activated by the build server when targeting sdk 26 or higher. In addition, this change only applies to devices that don't have Google Play. The rest of the fix for push notifications in API 26+ (e.g. for Google Play devices) is part of the build server, as it generates the PushReceiver class which is used by google play devices.
1 parent 57388e8 commit 5d1824d

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed
 

‎Ports/Android/src/com/codename1/impl/android/PushNotificationService.java

+53-1
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,67 @@ public void run() {
101101
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
102102
Intent newIntent = new Intent(this, getStubClass());
103103
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, newIntent, PendingIntent.FLAG_CANCEL_CURRENT);
104-
104+
105+
106+
105107
Notification.Builder builder = new Notification.Builder(this)
106108
.setContentIntent(contentIntent)
107109
.setSmallIcon(android.R.drawable.stat_notify_sync)
108110
.setTicker(value)
109111
.setAutoCancel(true)
110112
.setWhen(System.currentTimeMillis())
111113
.setContentTitle(value)
114+
112115
.setDefaults(Notification.DEFAULT_ALL);
116+
117+
118+
119+
120+
// The following section is commented out so that builds against SDKs below 26
121+
// won't fail.
122+
/*<SDK26>
123+
if(android.os.Build.VERSION.SDK_INT >= 21){
124+
builder.setCategory("Notification");
125+
}
126+
if (android.os.Build.VERSION.SDK_INT >= 26) {
127+
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
128+
129+
String id = Display.getInstance().getProperty("android.NotificationChannel.id", "cn1-channel");
130+
131+
CharSequence name = Display.getInstance().getProperty("android.NotificationChannel.name", "Notifications");
132+
133+
String description = Display.getInstance().getProperty("android.NotificationChannel.description", "Remote notifications");
134+
135+
int importance = Integer.parseInt(Display.getInstance().getProperty("android.NotificationChannel.importance", ""+NotificationManager.IMPORTANCE_LOW));
136+
137+
android.app.NotificationChannel mChannel = new android.app.NotificationChannel(id, name,importance);
138+
139+
mChannel.setDescription(description);
140+
141+
mChannel.enableLights(Boolean.parseBoolean(Display.getInstance().getProperty("android.NotificationChannel.enableLights", "true")));
142+
143+
mChannel.setLightColor(Integer.parseInt(Display.getInstance().getProperty("android.NotificationChannel.lightColor", ""+android.graphics.Color.RED)));
144+
145+
mChannel.enableVibration(Boolean.parseBoolean(Display.getInstance().getProperty("android.NotificationChannel.enableVibration", "false")));
146+
String vibrationPatternStr = Display.getInstance().getProperty("android.NotificationChannel.vibrationPattern", null);
147+
if (vibrationPatternStr != null) {
148+
String[] parts = vibrationPatternStr.split(",");
149+
int len = parts.length;
150+
long[] pattern = new long[len];
151+
for (int i=0; i<len; i++) {
152+
pattern[i] = Long.parseLong(parts[i].trim());
153+
}
154+
mChannel.setVibrationPattern(pattern);
155+
}
156+
157+
158+
159+
mNotificationManager.createNotificationChannel(mChannel);
160+
System.out.println("Setting push channel to "+id);
161+
builder.setChannelId(id);
162+
}
163+
</SDK26>*/
164+
113165
Notification notif = builder.build();
114166
nm.notify((int)System.currentTimeMillis(), notif);
115167
}

0 commit comments

Comments
 (0)