From 90d8b4617998afb7a64dbe3f0cc76f48444fc613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 22 Oct 2018 16:29:37 +0200 Subject: [PATCH] actually use settings --- .../model/CMessageList.java | 3 + .../service/FBMService.java | 6 +- .../service/NotificationService.java | 115 ++++++++++++++---- .../view/SettingsFragment.java | 3 + 4 files changed, 99 insertions(+), 28 deletions(-) diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java index a15fe5c..3614148 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/model/CMessageList.java @@ -55,6 +55,9 @@ public class CMessageList SharedPreferences.Editor e = sharedPref.edit(); Messages.add(msg); + + while (Messages.size()>SCNSettings.inst().LocalCacheSize) Messages.remove(0); + e.putInt("message_count", count+1); e.putLong("message["+count+"].timestamp", time); e.putString("message["+count+"].title", title); diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/FBMService.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/FBMService.java index 5efe70a..d5150a2 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/FBMService.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/FBMService.java @@ -25,6 +25,8 @@ public class FBMService extends FirebaseMessagingService { try { + if (!SCNSettings.inst().Enabled) return; + Log.i("FB::MessageReceived", "From: " + remoteMessage.getFrom()); Log.i("FB::MessageReceived", "Payload: " + remoteMessage.getData()); if (remoteMessage.getNotification() != null) Log.i("FB::MessageReceived", "Notify_Title: " + remoteMessage.getNotification().getTitle()); @@ -40,11 +42,11 @@ public class FBMService extends FirebaseMessagingService if (SCNApp.isBackground()) { - NotificationService.inst().show(msg); + NotificationService.inst().showBackground(msg); } else { - SCNApp.showToast("Message recieved: " + title, Toast.LENGTH_LONG); + NotificationService.inst().showForeground(msg); } } catch (Exception e) diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/NotificationService.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/NotificationService.java index 24983f0..51dd89a 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/NotificationService.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/service/NotificationService.java @@ -1,23 +1,33 @@ package com.blackforestbytes.simplecloudnotifier.service; +import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Color; +import android.media.AudioAttributes; +import android.media.AudioManager; +import android.net.Uri; import android.os.Build; +import android.widget.Toast; import com.blackforestbytes.simplecloudnotifier.R; import com.blackforestbytes.simplecloudnotifier.SCNApp; import com.blackforestbytes.simplecloudnotifier.model.CMessage; +import com.blackforestbytes.simplecloudnotifier.model.NotificationSettings; +import com.blackforestbytes.simplecloudnotifier.model.PriorityEnum; +import com.blackforestbytes.simplecloudnotifier.model.SCNSettings; import com.blackforestbytes.simplecloudnotifier.view.MainActivity; import androidx.core.app.NotificationCompat; public class NotificationService { - private final static String CHANNEL_ID = "CHAN_BFB_SCN_MESSAGES"; + private final static String CHANNEL_ID_LOW = "CHAN_BFB_SCN_MESSAGES_LOW"; + private final static String CHANNEL_ID_NORM = "CHAN_BFB_SCN_MESSAGES_NORM"; + private final static String CHANNEL_ID_HIGH = "CHAN_BFB_SCN_MESSAGES_HIGH"; private final static Object _lock = new Object(); private static NotificationService _inst = null; @@ -32,39 +42,92 @@ public class NotificationService private NotificationService() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - { - Context ctxt = SCNApp.getContext(); - - NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Push notifications", NotificationManager.IMPORTANCE_HIGH); - channel.setDescription("Messages from the API"); - channel.setLightColor(Color.rgb(255, 0, 0)); - channel.setVibrationPattern(new long[]{200}); - channel.enableLights(true); - channel.enableVibration(true); - - NotificationManager notificationManager = ctxt.getSystemService(NotificationManager.class); - if (notificationManager != null) notificationManager.createNotificationChannel(channel); - } - + updateChannels(); } - public void show(CMessage msg) + public void updateChannels() + { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return; + + Context ctxt = SCNApp.getContext(); + NotificationManager notifman = ctxt.getSystemService(NotificationManager.class); + if (notifman == null) return; + + NotificationChannel channelLow = notifman.getNotificationChannel(CHANNEL_ID_LOW); + if (channelLow == null) notifman.createNotificationChannel(channelLow = new NotificationChannel(CHANNEL_ID_LOW, "Push notifications (low priority)", NotificationManager.IMPORTANCE_LOW)); + NotificationChannel channelNorm = notifman.getNotificationChannel(CHANNEL_ID_NORM); + if (channelNorm == null) notifman.createNotificationChannel(channelNorm = new NotificationChannel(CHANNEL_ID_NORM, "Push notifications (normal priority)", NotificationManager.IMPORTANCE_DEFAULT)); + NotificationChannel channelHigh = notifman.getNotificationChannel(CHANNEL_ID_HIGH); + if (channelHigh == null) notifman.createNotificationChannel(channelHigh = new NotificationChannel(CHANNEL_ID_HIGH, "Push notifications (high priority)", NotificationManager.IMPORTANCE_HIGH)); + + channelLow.setDescription("Messages from the API with priority set to low"); + channelLow.enableLights(SCNSettings.inst().PriorityLow.EnableLED); + channelLow.setLightColor(SCNSettings.inst().PriorityLow.LEDColor); + channelLow.enableVibration(SCNSettings.inst().PriorityLow.EnableVibration); + channelLow.setVibrationPattern(new long[]{200}); + + channelNorm.setDescription("Messages from the API with priority set to normal"); + channelNorm.enableLights(SCNSettings.inst().PriorityNorm.EnableLED); + channelNorm.setLightColor(SCNSettings.inst().PriorityNorm.LEDColor); + channelNorm.enableVibration(SCNSettings.inst().PriorityNorm.EnableVibration); + channelNorm.setVibrationPattern(new long[]{200}); + + channelHigh.setDescription("Messages from the API with priority set to high"); + channelHigh.enableLights(SCNSettings.inst().PriorityHigh.EnableLED); + channelHigh.setLightColor(SCNSettings.inst().PriorityHigh.LEDColor); + channelHigh.enableVibration(SCNSettings.inst().PriorityHigh.EnableVibration); + channelHigh.setVibrationPattern(new long[]{200}); + channelLow.setBypassDnd(true); + channelLow.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); + } + + public void showForeground(CMessage msg) + { + SCNApp.showToast("Message recieved: " + msg.Title, Toast.LENGTH_LONG); + } + + public void showBackground(CMessage msg) { Context ctxt = SCNApp.getContext(); - NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctxt, CHANNEL_ID) - .setSmallIcon(R.drawable.ic_bfb) - .setContentTitle(msg.Title) - .setContentText(msg.Content) - .setShowWhen(true) - .setWhen(msg.Timestamp) - .setPriority(NotificationCompat.PRIORITY_HIGH) - .setAutoCancel(true); + String channel = CHANNEL_ID_NORM; + NotificationSettings ns = SCNSettings.inst().PriorityNorm; + switch (msg.Priority) + { + case LOW: ns = SCNSettings.inst().PriorityLow; channel = CHANNEL_ID_LOW; break; + case NORMAL: ns = SCNSettings.inst().PriorityNorm; channel = CHANNEL_ID_NORM; break; + case HIGH: ns = SCNSettings.inst().PriorityHigh; channel = CHANNEL_ID_HIGH; break; + } + + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctxt, channel); + mBuilder.setSmallIcon(R.drawable.ic_bfb); + mBuilder.setContentTitle(msg.Title); + mBuilder.setContentText(msg.Content); + mBuilder.setShowWhen(true); + mBuilder.setWhen(msg.Timestamp); + mBuilder.setAutoCancel(true); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) + { + if (msg.Priority == PriorityEnum.LOW) mBuilder.setPriority(NotificationCompat.PRIORITY_LOW); + if (msg.Priority == PriorityEnum.NORMAL) mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT); + if (msg.Priority == PriorityEnum.HIGH) mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH); + if (ns.EnableVibration) mBuilder.setVibrate(new long[]{200}); + if (ns.EnableLED) mBuilder.setLights(ns.LEDColor, 500, 500); + } + + if (ns.EnableSound && !ns.SoundSource.isEmpty()) + { + mBuilder.setSound(Uri.parse(ns.SoundSource), AudioManager.STREAM_ALARM); + } + Intent intent = new Intent(ctxt, MainActivity.class); PendingIntent pi = PendingIntent.getActivity(ctxt, 0, intent, 0); mBuilder.setContentIntent(pi); NotificationManager mNotificationManager = (NotificationManager) ctxt.getSystemService(Context.NOTIFICATION_SERVICE); - if (mNotificationManager != null) mNotificationManager.notify(0, mBuilder.build()); + + Notification n = mBuilder.build(); + if (ns.EnableSound && !ns.SoundSource.isEmpty() && ns.RepeatSound) n.flags |= Notification.FLAG_INSISTENT; + + if (mNotificationManager != null) mNotificationManager.notify(0, n); } } diff --git a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/SettingsFragment.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/SettingsFragment.java index a5285d8..935aac3 100644 --- a/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/SettingsFragment.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/view/SettingsFragment.java @@ -1,5 +1,6 @@ package com.blackforestbytes.simplecloudnotifier.view; +import android.app.NotificationManager; import android.content.Context; import android.media.AudioManager; import android.net.Uri; @@ -17,6 +18,7 @@ import android.widget.TextView; import com.blackforestbytes.simplecloudnotifier.R; import com.blackforestbytes.simplecloudnotifier.model.SCNSettings; +import com.blackforestbytes.simplecloudnotifier.service.NotificationService; import org.jetbrains.annotations.NotNull; @@ -190,6 +192,7 @@ public class SettingsFragment extends Fragment implements MusicPickerListener { SCNSettings.inst().save(); updateUI(); + NotificationService.inst().updateChannels(); } private void onUpgradeAccount()