Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/
package org.eclipse.paho.android.service;

import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
Expand Down Expand Up @@ -103,6 +105,9 @@ public class MqttAndroidClient extends BroadcastReceiver implements IMqttAsyncCl
private boolean traceEnabled = false;
private volatile boolean receiverRegistered = false;
private volatile boolean bindedService = false;
// notification for Foreground Service
private int foregroundServiceNotificationId = 1;
private Notification foregroundServiceNotification;

/**
* Constructor - create an MqttAndroidClient that can be used to communicate with an MQTT server on android
Expand Down Expand Up @@ -321,7 +326,28 @@ public IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttA
if (mqttService == null) { // First time - must bind to the service
Intent serviceStartIntent = new Intent();
serviceStartIntent.setClassName(myContext, SERVICE_NAME);
Object service = myContext.startService(serviceStartIntent);

Object service = null;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& foregroundServiceNotification != null) {
serviceStartIntent.putExtra(
MqttService.PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION,
foregroundServiceNotification);
serviceStartIntent.putExtra(
MqttService.PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID,
foregroundServiceNotificationId);
service = myContext.startForegroundService(serviceStartIntent);
} else {
try {
service = myContext.startService(serviceStartIntent);
} catch(IllegalStateException ex) {
IMqttActionListener listener = token.getActionCallback();
if (listener != null) {
listener.onFailure(token, ex);
}
}
}

if (service == null) {
IMqttActionListener listener = token.getActionCallback();
if (listener != null) {
Expand Down Expand Up @@ -1421,6 +1447,30 @@ private synchronized IMqttToken getMqttToken(Bundle data) {
return tokenMap.get(Integer.parseInt(activityToken));
}

/**
* Sets foregroundServiceNotification object. If it is not null at the time of
* MqttService start then the service will run in foreground mode which is
* mandatory to keep MQTT service operation when app is
* in the background on Android version >=8.
*
* This method has no effect if Build.VERSION.SDK_INT < Build.VERSION_CODES.O
*
* @param notification notification to be used when MqttService runs in foreground mode
*/
public void setForegroundServiceNotification(Notification notification) {
foregroundServiceNotification = notification;
}

/**
* Sets ID of the foreground service notification.
* If this method is not used then the default ID 1 will be used.
*
* @param id The identifier for foreground service notification
*/
public void setForegroundServiceNotificationId(int id) {
foregroundServiceNotificationId = id;
}

/**
* Sets the DisconnectedBufferOptions for this client
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.paho.android.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
Expand Down Expand Up @@ -226,6 +227,9 @@ public class MqttService extends Service implements MqttTraceHandler {

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && intent != null) {
Notification foregroundServiceNotification
= (Notification) (intent.getParcelableExtra(PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION));
if (foregroundServiceNotification != null)
startForeground(
intent.getIntExtra(PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID, 1),
foregroundServiceNotification
);
}

return START_STICKY;
}

Expand Down