mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-12-23 13:57:35 +00:00
Merge pull request #94 from Menardi/Android-Improvements
Android improvements
This commit is contained in:
commit
578ba9f686
18
README.md
18
README.md
@ -204,21 +204,19 @@ document.addEventListener('deviceready', function () {
|
||||
To indicate that the app is executing tasks in background and being paused would disrupt the user, the plug-in has to create a notification while in background - like a download progress bar.
|
||||
|
||||
#### Override defaults
|
||||
The title, ticker and text for that notification can be customized as follows:
|
||||
The title, ticker, text and icon for that notification can be customized as below. Also, by default the app will come to foreground when tapping on the notification. That can be changed by setting resume to false. On Android 5.0+, the color option will set the background color of the notification circle. Also on Android 5.0+, setting isPublic to true will make the full notification show on a secure lockscreen.
|
||||
|
||||
All of these fields are optional - only override the things you need to.
|
||||
|
||||
```javascript
|
||||
cordova.plugins.backgroundMode.setDefaults({
|
||||
title: String,
|
||||
ticker: String,
|
||||
text: String
|
||||
})
|
||||
```
|
||||
|
||||
By default the app will come to foreground when taping on the notification. That can be changed also.
|
||||
|
||||
```javascript
|
||||
cordova.plugins.backgroundMode.setDefaults({
|
||||
resume: false
|
||||
text: String,
|
||||
icon: "icon" // this will look for icon.png in platforms/android/res/drawable
|
||||
resume: true / false,
|
||||
color: "#123456",
|
||||
isPublic: true / false,
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,11 +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;
|
||||
@ -48,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();
|
||||
|
||||
@ -59,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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,17 +153,31 @@ 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", ""))
|
||||
.setOngoing(true)
|
||||
.setSmallIcon(getIconResId());
|
||||
|
||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
if(settings.optBoolean("isPublic") == true) {
|
||||
notification.setVisibility(Notification.VISIBILITY_PUBLIC);
|
||||
}
|
||||
|
||||
if(!settings.optString("color").equals("")) {
|
||||
try {
|
||||
notification.setColor(Color.parseColor(settings.optString("color")));
|
||||
} catch (Exception e) {
|
||||
Log.e("BackgroundMode", settings.optString("color") + " is not a valid color");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (intent != null && settings.optBoolean("resume")) {
|
||||
|
||||
PendingIntent contentIntent = PendingIntent.getActivity(
|
||||
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
||||
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
notification.setContentIntent(contentIntent);
|
||||
}
|
||||
@ -159,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.
|
||||
*
|
||||
@ -166,12 +205,12 @@ public class ForegroundService extends Service {
|
||||
* The resource ID of the app icon
|
||||
*/
|
||||
private int getIconResId() {
|
||||
JSONObject settings = BackgroundMode.getSettings();
|
||||
Context context = getApplicationContext();
|
||||
Resources res = context.getResources();
|
||||
String pkgName = context.getPackageName();
|
||||
|
||||
int resId;
|
||||
resId = res.getIdentifier("icon", "drawable", pkgName);
|
||||
int resId = res.getIdentifier(settings.optString("icon", "icon"), "drawable", pkgName);
|
||||
|
||||
return resId;
|
||||
}
|
||||
|
@ -62,7 +62,10 @@ exports._defaults = {
|
||||
text: 'Doing heavy tasks.',
|
||||
ticker: 'App is running in background',
|
||||
resume: true,
|
||||
silent: false
|
||||
silent: false,
|
||||
isPublic: false,
|
||||
color: "",
|
||||
icon: "icon"
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user