From 5e4d5deddf4788ffd2fe952dab545466c0289fe4 Mon Sep 17 00:00:00 2001 From: untidylamp <44341286+untidylamp@users.noreply.github.com> Date: Fri, 16 Nov 2018 15:37:11 -0500 Subject: [PATCH] Persistent Notification for Matrix Service Show a Persistent Notification while Matrix Service is running. --- .../eu/droogers/smsmatrix/MatrixService.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/app/src/main/java/eu/droogers/smsmatrix/MatrixService.java b/app/src/main/java/eu/droogers/smsmatrix/MatrixService.java index 9fbd89a..01daff1 100644 --- a/app/src/main/java/eu/droogers/smsmatrix/MatrixService.java +++ b/app/src/main/java/eu/droogers/smsmatrix/MatrixService.java @@ -1,11 +1,15 @@ package eu.droogers.smsmatrix; +import android.app.NotificationChannel; +import android.app.NotificationManager; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.os.Build; import android.os.IBinder; +import android.support.v4.app.NotificationCompat; import android.util.Log; import android.widget.Toast; @@ -24,6 +28,9 @@ public class MatrixService extends Service { private String syncDelay; private String syncTimeout; private MMSMonitor mms; + private NotificationManager notificationManager = null; + private final static int PERSISTENT_NOTIFICATION_ID = 001; + private final static String NOTIFICATION_CHANNEL_ID = "smsmatrix"; @Override public void onCreate() { @@ -44,6 +51,7 @@ public class MatrixService extends Service { if (mx == null && !botUsername.isEmpty() && !botPassword.isEmpty() && !username.isEmpty() && !device.isEmpty() && !hsUrl.isEmpty() && !syncDelay.isEmpty() && !syncTimeout.isEmpty()) { mx = new Matrix(getApplication(), hsUrl, botUsername, botPassword, username, device, syncDelay, syncTimeout); Log.e(TAG, "onStartCommand: " + hsUrl ); + persistentNotification(); Toast.makeText(this, "service starting:", Toast.LENGTH_SHORT).show(); } else if (mx == null) { Toast.makeText(this, "Missing Information", Toast.LENGTH_SHORT).show(); @@ -76,9 +84,29 @@ public class MatrixService extends Service { } + public void persistentNotification() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + CharSequence name = "Persistent"; + String description = "Sms Matrix Service is running"; + int importance = NotificationManager.IMPORTANCE_DEFAULT; + NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance); + channel.setDescription(description); + notificationManager = getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(channel); + } + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) + .setSmallIcon(R.drawable.matrix_user) + .setContentTitle("SmsMatrix") + .setContentText("Matrix service running") + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .setOngoing(true); + notificationManager.notify(PERSISTENT_NOTIFICATION_ID, mBuilder.build()); + } + @Override public void onDestroy() { mx.destroy(); + notificationManager.cancel(PERSISTENT_NOTIFICATION_ID); this.mms.stopMMSMonitoring(); this.mms = null; super.onDestroy();