Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(android): update notification instead of recreating #249

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/android/ForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class ForegroundService extends Service {

// Partial wake lock to prevent the app from going to sleep when locked
private PowerManager.WakeLock wakeLock;
private Notification.Builder notificationBuilder;

/**
* Allow clients to call on to the service.
Expand Down Expand Up @@ -153,6 +154,17 @@ private Notification makeNotification() {
* @param settings The config settings
*/
private Notification makeNotification(JSONObject settings) {
return makeNotification(settings, false);
}

/**
* Create a notification as the visible part to be able to put the service
* in a foreground state.
*
* @param settings The config settings
* @param isUpdate flag indication the settings should only update the notification
*/
private Notification makeNotification(JSONObject settings, boolean isUpdate) {
String title = settings.optString("title", NOTIFICATION_TITLE);
String text = settings.optString("text", NOTIFICATION_TEXT);
boolean bigText = settings.optBoolean("bigText", false);
Expand All @@ -162,33 +174,36 @@ private Notification makeNotification(JSONObject settings) {
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(pkgName);

Notification.Builder notification = new Notification.Builder(context)
.setContentTitle(title)
if(!isUpdate || notificationBuilder == null){
notificationBuilder = new Notification.Builder(context)
.setOngoing(true);
}

notificationBuilder.setContentTitle(title)
.setContentText(text)
.setOngoing(true)
.setSmallIcon(getIconResId(settings));

if (settings.optBoolean("hidden", true)) {
notification.setPriority(Notification.PRIORITY_MIN);
notificationBuilder.setPriority(Notification.PRIORITY_MIN);
}

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

setColor(notification, settings);
setColor(notificationBuilder, settings);

if (intent != null && settings.optBoolean("resume")) {
PendingIntent contentIntent = PendingIntent.getActivity(
context, NOTIFICATION_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);


notification.setContentIntent(contentIntent);
notificationBuilder.setContentIntent(contentIntent);
}

return notification.build();
return notificationBuilder.build();
}

/**
Expand All @@ -204,7 +219,7 @@ protected void updateNotification (JSONObject settings) {
return;
}

Notification notification = makeNotification(settings);
Notification notification = makeNotification(settings, true);
getNotificationManager().notify(NOTIFICATION_ID, notification);
}

Expand Down