|
16 | 16 | */
|
17 | 17 | package org.eclipse.paho.android.service;
|
18 | 18 |
|
| 19 | +import android.app.Notification; |
19 | 20 | import android.content.BroadcastReceiver;
|
20 | 21 | import android.content.ComponentName;
|
21 | 22 | import android.content.Context;
|
22 | 23 | import android.content.Intent;
|
23 | 24 | import android.content.IntentFilter;
|
24 | 25 | import android.content.ServiceConnection;
|
| 26 | +import android.os.Build; |
25 | 27 | import android.os.Bundle;
|
26 | 28 | import android.os.IBinder;
|
27 | 29 | import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
@@ -103,6 +105,9 @@ public class MqttAndroidClient extends BroadcastReceiver implements IMqttAsyncCl
|
103 | 105 | private boolean traceEnabled = false;
|
104 | 106 | private volatile boolean receiverRegistered = false;
|
105 | 107 | private volatile boolean bindedService = false;
|
| 108 | + // notification for Foreground Service |
| 109 | + private int foregroundServiceNotificationId = 1; |
| 110 | + private Notification foregroundServiceNotification; |
106 | 111 |
|
107 | 112 | /**
|
108 | 113 | * 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
|
321 | 326 | if (mqttService == null) { // First time - must bind to the service
|
322 | 327 | Intent serviceStartIntent = new Intent();
|
323 | 328 | 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 | + |
325 | 351 | if (service == null) {
|
326 | 352 | IMqttActionListener listener = token.getActionCallback();
|
327 | 353 | if (listener != null) {
|
@@ -1421,6 +1447,30 @@ private synchronized IMqttToken getMqttToken(Bundle data) {
|
1421 | 1447 | return tokenMap.get(Integer.parseInt(activityToken));
|
1422 | 1448 | }
|
1423 | 1449 |
|
| 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 | + |
1424 | 1474 | /**
|
1425 | 1475 | * Sets the DisconnectedBufferOptions for this client
|
1426 | 1476 | *
|
|
0 commit comments