Skip to content

Commit 681f2f9

Browse files
committed
Support for multi line text [Fixes katzer#171]
1 parent 8e3a352 commit 681f2f9

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

src/android/ForegroundService.java

+30-32
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ private void keepAwake() {
119119
*/
120120
private void sleepWell() {
121121
stopForeground(true);
122+
getNotificationManager().cancel(NOTIFICATION_ID);
122123

123124
if (wakeLock != null) {
124125
wakeLock.release();
@@ -129,10 +130,6 @@ private void sleepWell() {
129130
/**
130131
* Create a notification as the visible part to be able to put the service
131132
* in a foreground state by using the default settings.
132-
*
133-
* @return
134-
* A local ongoing notification which pending intent is bound to the
135-
* main activity.
136133
*/
137134
private Notification makeNotification() {
138135
return makeNotification(BackgroundMode.getSettings());
@@ -142,26 +139,29 @@ private Notification makeNotification() {
142139
* Create a notification as the visible part to be able to put the service
143140
* in a foreground state.
144141
*
145-
* @param settings
146-
* The config settings
147-
*
148-
* @return
149-
* A local ongoing notification which pending intent is bound to the
150-
* main activity.
142+
* @param settings The config settings
151143
*/
152144
private Notification makeNotification(JSONObject settings) {
145+
String text = settings.optString("text", "");
146+
boolean bigText = settings.optBoolean("bigText", false);
147+
153148
Context context = getApplicationContext();
154149
String pkgName = context.getPackageName();
155150
Intent intent = context.getPackageManager()
156151
.getLaunchIntentForPackage(pkgName);
157152

158153
Notification.Builder notification = new Notification.Builder(context)
159154
.setContentTitle(settings.optString("title", ""))
160-
.setContentText(settings.optString("text", ""))
155+
.setContentText(text)
161156
.setTicker(settings.optString("ticker", ""))
162157
.setOngoing(true)
163158
.setSmallIcon(getIconResId());
164159

160+
if (bigText || text.contains("\n")) {
161+
notification.setStyle(
162+
new Notification.BigTextStyle().bigText(text));
163+
}
164+
165165
setColor(notification, settings);
166166

167167
if (intent != null && settings.optBoolean("resume")) {
@@ -178,8 +178,7 @@ private Notification makeNotification(JSONObject settings) {
178178
/**
179179
* Update the notification.
180180
*
181-
* @param settings
182-
* The config settings
181+
* @param settings The config settings
183182
*/
184183
protected void updateNotification (JSONObject settings) {
185184
boolean isSilent = settings.optBoolean("silent", false);
@@ -189,18 +188,14 @@ protected void updateNotification (JSONObject settings) {
189188
return;
190189
}
191190

192-
Notification notification = makeNotification(settings);
193-
NotificationManager service = (NotificationManager)
194-
getSystemService(Context.NOTIFICATION_SERVICE);
191+
Notification notification = makeNotification(settings);
195192

196-
service.notify(NOTIFICATION_ID, notification);
193+
getNotificationManager().notify(
194+
NOTIFICATION_ID, notification);
197195
}
198196

199197
/**
200198
* Retrieves the resource ID of the app icon.
201-
*
202-
* @return
203-
* The resource ID of the app icon
204199
*/
205200
private int getIconResId() {
206201
JSONObject settings = BackgroundMode.getSettings();
@@ -222,14 +217,10 @@ private int getIconResId() {
222217
/**
223218
* Retrieve resource id of the specified icon.
224219
*
225-
* @param res
226-
* The app resource bundle.
227-
* @param icon
228-
* The name of the icon.
229-
* @param type
230-
* The resource type where to look for.
231-
* @param pkgName
232-
* The name of the package.
220+
* @param res The app resource bundle.
221+
* @param icon The name of the icon.
222+
* @param type The resource type where to look for.
223+
* @param pkgName The name of the package.
233224
*
234225
* @return The resource id or 0 if not found.
235226
*/
@@ -248,10 +239,8 @@ private int getIconResId(Resources res, String icon,
248239
/**
249240
* Set notification color if its supported by the SDK.
250241
*
251-
* @param notification
252-
* A Notification.Builder instance
253-
* @param settings
254-
* A JSON dict containing the color definition (red: FF0000)
242+
* @param notification A Notification.Builder instance
243+
* @param settings A JSON dict containing the color definition (red: FF0000)
255244
*/
256245
private void setColor(Notification.Builder notification,
257246
JSONObject settings) {
@@ -276,4 +265,13 @@ private void setColor(Notification.Builder notification,
276265
e.printStackTrace();
277266
}
278267
}
268+
269+
/**
270+
* Shared manager for the notification service.
271+
*/
272+
private NotificationManager getNotificationManager() {
273+
return (NotificationManager) getSystemService(
274+
Context.NOTIFICATION_SERVICE);
275+
}
276+
279277
}

www/background-mode.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,9 @@ exports._isActive = false;
274274
*/
275275
exports._defaults = {
276276
title: 'App is running in background',
277-
text: 'Doing heavy tasks.',
277+
text: "Doing heavy tasks.",
278278
ticker: 'Running in background',
279+
bigText: true,
279280
resume: true,
280281
silent: false,
281282
color: undefined,

0 commit comments

Comments
 (0)