Skip to content

Commit

Permalink
Support for multi line text [Fixes katzer#171]
Browse files Browse the repository at this point in the history
  • Loading branch information
katzer committed Jan 1, 2017
1 parent 8e3a352 commit 681f2f9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
62 changes: 30 additions & 32 deletions src/android/ForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private void keepAwake() {
*/
private void sleepWell() {
stopForeground(true);
getNotificationManager().cancel(NOTIFICATION_ID);

if (wakeLock != null) {
wakeLock.release();
Expand All @@ -129,10 +130,6 @@ private void sleepWell() {
/**
* Create a notification as the visible part to be able to put the service
* in a foreground state by using the default settings.
*
* @return
* A local ongoing notification which pending intent is bound to the
* main activity.
*/
private Notification makeNotification() {
return makeNotification(BackgroundMode.getSettings());
Expand All @@ -142,26 +139,29 @@ private Notification makeNotification() {
* Create a notification as the visible part to be able to put the service
* in a foreground state.
*
* @param settings
* The config settings
*
* @return
* A local ongoing notification which pending intent is bound to the
* main activity.
* @param settings The config settings
*/
private Notification makeNotification(JSONObject settings) {
String text = settings.optString("text", "");
boolean bigText = settings.optBoolean("bigText", false);

Context context = getApplicationContext();
String pkgName = context.getPackageName();
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(pkgName);

Notification.Builder notification = new Notification.Builder(context)
.setContentTitle(settings.optString("title", ""))
.setContentText(settings.optString("text", ""))
.setContentText(text)
.setTicker(settings.optString("ticker", ""))
.setOngoing(true)
.setSmallIcon(getIconResId());

if (bigText || text.contains("\n")) {
notification.setStyle(
new Notification.BigTextStyle().bigText(text));
}

setColor(notification, settings);

if (intent != null && settings.optBoolean("resume")) {
Expand All @@ -178,8 +178,7 @@ private Notification makeNotification(JSONObject settings) {
/**
* Update the notification.
*
* @param settings
* The config settings
* @param settings The config settings
*/
protected void updateNotification (JSONObject settings) {
boolean isSilent = settings.optBoolean("silent", false);
Expand All @@ -189,18 +188,14 @@ protected void updateNotification (JSONObject settings) {
return;
}

Notification notification = makeNotification(settings);
NotificationManager service = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = makeNotification(settings);

service.notify(NOTIFICATION_ID, notification);
getNotificationManager().notify(
NOTIFICATION_ID, notification);
}

/**
* Retrieves the resource ID of the app icon.
*
* @return
* The resource ID of the app icon
*/
private int getIconResId() {
JSONObject settings = BackgroundMode.getSettings();
Expand All @@ -222,14 +217,10 @@ private int getIconResId() {
/**
* Retrieve resource id of the specified icon.
*
* @param res
* The app resource bundle.
* @param icon
* The name of the icon.
* @param type
* The resource type where to look for.
* @param pkgName
* The name of the package.
* @param res The app resource bundle.
* @param icon The name of the icon.
* @param type The resource type where to look for.
* @param pkgName The name of the package.
*
* @return The resource id or 0 if not found.
*/
Expand All @@ -248,10 +239,8 @@ private int getIconResId(Resources res, String icon,
/**
* Set notification color if its supported by the SDK.
*
* @param notification
* A Notification.Builder instance
* @param settings
* A JSON dict containing the color definition (red: FF0000)
* @param notification A Notification.Builder instance
* @param settings A JSON dict containing the color definition (red: FF0000)
*/
private void setColor(Notification.Builder notification,
JSONObject settings) {
Expand All @@ -276,4 +265,13 @@ private void setColor(Notification.Builder notification,
e.printStackTrace();
}
}

/**
* Shared manager for the notification service.
*/
private NotificationManager getNotificationManager() {
return (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
}

}
3 changes: 2 additions & 1 deletion www/background-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@ exports._isActive = false;
*/
exports._defaults = {
title: 'App is running in background',
text: 'Doing heavy tasks.',
text: "Doing heavy tasks.",
ticker: 'Running in background',
bigText: true,
resume: true,
silent: false,
color: undefined,
Expand Down

0 comments on commit 681f2f9

Please sign in to comment.