Android notification update now updates inline

This commit is contained in:
Gearóid Moroney 2015-06-03 19:45:26 +01:00
parent cec56db33c
commit 04d5dc581b
2 changed files with 32 additions and 6 deletions

View File

@ -59,12 +59,15 @@ public class BackgroundMode extends CordovaPlugin {
// Tmp config settings for the notification // Tmp config settings for the notification
private static JSONObject updateSettings; private static JSONObject updateSettings;
ForegroundService mService;
// Used to (un)bind the service to with the activity // Used to (un)bind the service to with the activity
private final ServiceConnection connection = new ServiceConnection() { private final ServiceConnection connection = new ServiceConnection() {
@Override @Override
public void onServiceConnected(ComponentName name, IBinder binder) { public void onServiceConnected(ComponentName name, IBinder service) {
// Nothing to do here ForegroundService.ForegroundBinder binder = (ForegroundService.ForegroundBinder) service;
mService = binder.getService();
} }
@Override @Override
@ -216,8 +219,7 @@ public class BackgroundMode extends CordovaPlugin {
*/ */
private void updateNotifcation() { private void updateNotifcation() {
if (isBind) { if (isBind) {
stopService(); mService.updateNotification();
startService();
} }
} }

View File

@ -28,12 +28,14 @@ import org.json.JSONObject;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Color; import android.graphics.Color;
import android.os.Binder;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
@ -49,6 +51,11 @@ public class ForegroundService extends Service {
// Fixed ID for the 'foreground' notification // Fixed ID for the 'foreground' notification
private static final int NOTIFICATION_ID = -574543954; 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 // Scheduler to exec periodic tasks
final Timer scheduler = new Timer(); final Timer scheduler = new Timer();
@ -60,7 +67,18 @@ public class ForegroundService extends Service {
*/ */
@Override @Override
public IBinder onBind (Intent intent) { 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() Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(pkgName); .getLaunchIntentForPackage(pkgName);
Notification.Builder notification = new Notification.Builder(context) notification = new Notification.Builder(context)
.setContentTitle(settings.optString("title", "")) .setContentTitle(settings.optString("title", ""))
.setContentText(settings.optString("text", "")) .setContentText(settings.optString("text", ""))
.setTicker(settings.optString("ticker", "")) .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. * Retrieves the resource ID of the app icon.
* *