From 04d5dc581b2f0cb13dcfbdb2f4042b0611cdf5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gear=C3=B3id=20Moroney?= Date: Wed, 3 Jun 2015 19:45:26 +0100 Subject: [PATCH] Android notification update now updates inline --- src/android/BackgroundMode.java | 10 ++++++---- src/android/ForegroundService.java | 28 ++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index 8b9afe4..2213619 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -59,12 +59,15 @@ public class BackgroundMode extends CordovaPlugin { // Tmp config settings for the notification private static JSONObject updateSettings; + ForegroundService mService; + // Used to (un)bind the service to with the activity private final ServiceConnection connection = new ServiceConnection() { @Override - public void onServiceConnected(ComponentName name, IBinder binder) { - // Nothing to do here + public void onServiceConnected(ComponentName name, IBinder service) { + ForegroundService.ForegroundBinder binder = (ForegroundService.ForegroundBinder) service; + mService = binder.getService(); } @Override @@ -216,8 +219,7 @@ public class BackgroundMode extends CordovaPlugin { */ private void updateNotifcation() { if (isBind) { - stopService(); - startService(); + mService.updateNotification(); } } diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index 1490e4b..9a9b90a 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -28,12 +28,14 @@ import org.json.JSONObject; import android.annotation.SuppressLint; 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.res.Resources; import android.graphics.Color; +import android.os.Binder; import android.os.Build; import android.os.Handler; import android.os.IBinder; @@ -49,6 +51,11 @@ public class ForegroundService extends Service { // Fixed ID for the 'foreground' notification private static final int NOTIFICATION_ID = -574543954; + private Notification.Builder notification; + + // Binder given to clients + private final IBinder mBinder = new ForegroundBinder(); + // Scheduler to exec periodic tasks final Timer scheduler = new Timer(); @@ -60,7 +67,18 @@ public class ForegroundService extends Service { */ @Override public IBinder onBind (Intent intent) { - return null; + return mBinder; + } + + /** + * Class used for the client Binder. Because we know this service always + * runs in the same process as its clients, we don't need to deal with IPC. + */ + public class ForegroundBinder extends Binder { + ForegroundService getService() { + // Return this instance of ForegroundService so clients can call public methods + return ForegroundService.this; + } } /** @@ -135,7 +153,7 @@ public class ForegroundService extends Service { Intent intent = context.getPackageManager() .getLaunchIntentForPackage(pkgName); - Notification.Builder notification = new Notification.Builder(context) + notification = new Notification.Builder(context) .setContentTitle(settings.optString("title", "")) .setContentText(settings.optString("text", "")) .setTicker(settings.optString("ticker", "")) @@ -174,6 +192,12 @@ public class ForegroundService extends Service { } } + public void updateNotification() { + Notification n = makeNotification(); + NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + mNotificationManager.notify(NOTIFICATION_ID, n); + } + /** * Retrieves the resource ID of the app icon. *