mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-14 19:44:53 +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.
|
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
|
#### 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
|
```javascript
|
||||||
cordova.plugins.backgroundMode.setDefaults({
|
cordova.plugins.backgroundMode.setDefaults({
|
||||||
title: String,
|
title: String,
|
||||||
ticker: String,
|
ticker: String,
|
||||||
text: String
|
text: String,
|
||||||
})
|
icon: "icon" // this will look for icon.png in platforms/android/res/drawable
|
||||||
```
|
resume: true / false,
|
||||||
|
color: "#123456",
|
||||||
By default the app will come to foreground when taping on the notification. That can be changed also.
|
isPublic: true / false,
|
||||||
|
|
||||||
```javascript
|
|
||||||
cordova.plugins.backgroundMode.setDefaults({
|
|
||||||
resume: false
|
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,11 +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.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;
|
||||||
@ -48,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();
|
||||||
|
|
||||||
@ -59,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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -134,17 +153,31 @@ 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", ""))
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setSmallIcon(getIconResId());
|
.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")) {
|
if (intent != null && settings.optBoolean("resume")) {
|
||||||
|
|
||||||
PendingIntent contentIntent = PendingIntent.getActivity(
|
PendingIntent contentIntent = PendingIntent.getActivity(
|
||||||
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
notification.setContentIntent(contentIntent);
|
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.
|
* Retrieves the resource ID of the app icon.
|
||||||
*
|
*
|
||||||
@ -166,12 +205,12 @@ public class ForegroundService extends Service {
|
|||||||
* The resource ID of the app icon
|
* The resource ID of the app icon
|
||||||
*/
|
*/
|
||||||
private int getIconResId() {
|
private int getIconResId() {
|
||||||
|
JSONObject settings = BackgroundMode.getSettings();
|
||||||
Context context = getApplicationContext();
|
Context context = getApplicationContext();
|
||||||
Resources res = context.getResources();
|
Resources res = context.getResources();
|
||||||
String pkgName = context.getPackageName();
|
String pkgName = context.getPackageName();
|
||||||
|
|
||||||
int resId;
|
int resId = res.getIdentifier(settings.optString("icon", "icon"), "drawable", pkgName);
|
||||||
resId = res.getIdentifier("icon", "drawable", pkgName);
|
|
||||||
|
|
||||||
return resId;
|
return resId;
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,10 @@ exports._defaults = {
|
|||||||
text: 'Doing heavy tasks.',
|
text: 'Doing heavy tasks.',
|
||||||
ticker: 'App is running in background',
|
ticker: 'App is running in background',
|
||||||
resume: true,
|
resume: true,
|
||||||
silent: false
|
silent: false,
|
||||||
|
isPublic: false,
|
||||||
|
color: "",
|
||||||
|
icon: "icon"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user