Skip to content

Commit 6fede14

Browse files
authoredNov 14, 2020
Merge pull request #7 from hannesa2/feature/foregroundSservice
Feature/foreground service
2 parents 3d0034f + 902fb6d commit 6fede14

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed
 

‎org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttAndroidClient.java

+51-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
*/
1717
package org.eclipse.paho.android.service;
1818

19+
import android.app.Notification;
1920
import android.content.BroadcastReceiver;
2021
import android.content.ComponentName;
2122
import android.content.Context;
2223
import android.content.Intent;
2324
import android.content.IntentFilter;
2425
import android.content.ServiceConnection;
26+
import android.os.Build;
2527
import android.os.Bundle;
2628
import android.os.IBinder;
2729
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@@ -103,6 +105,9 @@ public class MqttAndroidClient extends BroadcastReceiver implements IMqttAsyncCl
103105
private boolean traceEnabled = false;
104106
private volatile boolean receiverRegistered = false;
105107
private volatile boolean bindedService = false;
108+
// notification for Foreground Service
109+
private int foregroundServiceNotificationId = 1;
110+
private Notification foregroundServiceNotification;
106111

107112
/**
108113
* Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
@@ -321,7 +326,28 @@ public IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttA
321326
if (mqttService == null) { // First time - must bind to the service
322327
Intent serviceStartIntent = new Intent();
323328
serviceStartIntent.setClassName(myContext, SERVICE_NAME);
324-
Object service = myContext.startService(serviceStartIntent);
329+
330+
Object service = null;
331+
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
332+
&& foregroundServiceNotification != null) {
333+
serviceStartIntent.putExtra(
334+
MqttService.PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION,
335+
foregroundServiceNotification);
336+
serviceStartIntent.putExtra(
337+
MqttService.PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID,
338+
foregroundServiceNotificationId);
339+
service = myContext.startForegroundService(serviceStartIntent);
340+
} else {
341+
try {
342+
service = myContext.startService(serviceStartIntent);
343+
} catch(IllegalStateException ex) {
344+
IMqttActionListener listener = token.getActionCallback();
345+
if (listener != null) {
346+
listener.onFailure(token, ex);
347+
}
348+
}
349+
}
350+
325351
if (service == null) {
326352
IMqttActionListener listener = token.getActionCallback();
327353
if (listener != null) {
@@ -1421,6 +1447,30 @@ private synchronized IMqttToken getMqttToken(Bundle data) {
14211447
return tokenMap.get(Integer.parseInt(activityToken));
14221448
}
14231449

1450+
/**
1451+
* Sets foregroundServiceNotification object. If it is not null at the time of
1452+
* MqttService start then the service will run in foreground mode which is
1453+
* mandatory to keep MQTT service operation when app is
1454+
* in the background on Android version >=8.
1455+
*
1456+
* This method has no effect if Build.VERSION.SDK_INT < Build.VERSION_CODES.O
1457+
*
1458+
* @param notification notification to be used when MqttService runs in foreground mode
1459+
*/
1460+
public void setForegroundServiceNotification(Notification notification) {
1461+
foregroundServiceNotification = notification;
1462+
}
1463+
1464+
/**
1465+
* Sets ID of the foreground service notification.
1466+
* If this method is not used then the default ID 1 will be used.
1467+
*
1468+
* @param id The identifier for foreground service notification
1469+
*/
1470+
public void setForegroundServiceNotificationId(int id) {
1471+
foregroundServiceNotificationId = id;
1472+
}
1473+
14241474
/**
14251475
* Sets the DisconnectedBufferOptions for this client
14261476
*

‎org.eclipse.paho.android.service/src/main/java/org/eclipse/paho/android/service/MqttService.java

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.eclipse.paho.android.service;
1717

1818
import android.annotation.SuppressLint;
19+
import android.app.Notification;
1920
import android.app.Service;
2021
import android.content.BroadcastReceiver;
2122
import android.content.Context;
@@ -226,6 +227,9 @@ public class MqttService extends Service implements MqttTraceHandler {
226227

227228
// Identifier for Intents, log messages, etc..
228229
static final String TAG = "MqttService";
230+
// names of the start service Intent extras for foreground service mode
231+
static final String PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID = "org.eclipse.paho.android.service.MqttService.FOREGROUND_SERVICE_NOTIFICATION_ID";
232+
static final String PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION = "org.eclipse.paho.android.service.MqttService.FOREGROUND_SERVICE_NOTIFICATION";
229233
// somewhere to persist received messages until we're sure
230234
// that they've reached the application
231235
MessageStore messageStore;
@@ -596,6 +600,16 @@ public int onStartCommand(final Intent intent, int flags, final int startId) {
596600
// process restarted
597601
registerBroadcastReceivers();
598602

603+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && intent != null) {
604+
Notification foregroundServiceNotification
605+
= (Notification) (intent.getParcelableExtra(PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION));
606+
if (foregroundServiceNotification != null)
607+
startForeground(
608+
intent.getIntExtra(PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID, 1),
609+
foregroundServiceNotification
610+
);
611+
}
612+
599613
return START_STICKY;
600614
}
601615

0 commit comments

Comments
 (0)
Please sign in to comment.