Skip to content
Open
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
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
classpath 'com.android.tools.build:gradle:7.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand All @@ -16,7 +16,7 @@ apply plugin: 'java-library'

ext{

compileSdkVersion = 24
compileSdkVersion = 31
buildToolsVersion = '23.0.3'


Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Dec 07 18:39:23 IST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
2 changes: 1 addition & 1 deletion org.eclipse.paho.android.service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

defaultConfig {
minSdkVersion 9
targetSdkVersion 24
targetSdkVersion 31

testApplicationId "org.eclipse.paho.android.service.test"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
import org.eclipse.paho.client.mqttv3.MqttToken;
import org.eclipse.paho.client.mqttv3.internal.wire.MqttWireMessage;

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 android.support.v4.content.LocalBroadcastManager;
Expand Down Expand Up @@ -157,6 +159,10 @@ public void onServiceDisconnected(ComponentName name) {
private volatile boolean receiverRegistered = false;
private volatile boolean bindedService = false;

// Foreground notification
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 @@ -412,7 +418,25 @@ public IMqttToken connect(MqttConnectOptions options, Object userContext,
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 >= 26 && 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 (Exception e) {
IMqttActionListener listener = token.getActionCallback();
if (listener != null) {
listener.onFailure(token, e);
}
}
}
if (service == null) {
IMqttActionListener listener = token.getActionCallback();
if (listener != null) {
Expand Down Expand Up @@ -1782,4 +1806,15 @@ public void registerResources(Context context){
public void sendNoWait(MqttWireMessage message, MqttToken token) throws MqttException {
mqttService.sendNoWait(clientHandle, message, token);
}

/**
*
* @param notification - ForegroundServiceNotification
* @param id - ID of the foreground service notification.
*
*/
public void setForegroundService(Notification notification, int id) {
this.foregroundServiceNotification = notification;
this.foregroundServiceNotificationId = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.paho.client.mqttv3.internal.wire.MqttWireMessage;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
Expand Down Expand Up @@ -230,6 +231,9 @@ public class MqttService extends Service implements MqttTraceHandler {
// Identifier for Intents, log messages, etc..
static final String TAG = "MqttService";

static final String PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION = MqttService.class.getSimpleName() + ".FOREGROUND_SERVICE_NOTIFICATION_ID";
static final String PAHO_MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID = MqttService.class.getSimpleName() + ".FOREGROUND_SERVICE_NOTIFICATION";

// callback id for making trace callbacks to the Activity
// needs to be set by the activity as appropriate
private String traceCallbackId;
Expand Down Expand Up @@ -620,6 +624,15 @@ public int onStartCommand(final Intent intent, int flags, final int startId) {
// run till explicitly stopped, restart when
// process restarted
registerBroadcastReceivers();
if (Build.VERSION.SDK_INT >= 26 && 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