channel bullshit

This commit is contained in:
Mike Schwörer 2018-11-12 18:25:41 +01:00
parent 6cb2aa00fb
commit 66f82f26d5
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 103 additions and 84 deletions

View File

@ -118,6 +118,8 @@ public class SCNApp extends Application implements LifecycleObserver
[X] - verify recieve [X] - verify recieve
[ ] - Android O repeat sound
[ ] - test notification channels [ ] - test notification channels
[ ] - publish (+ HN post ?) [ ] - publish (+ HN post ?)

View File

@ -1,17 +1,29 @@
package com.blackforestbytes.simplecloudnotifier.model; package com.blackforestbytes.simplecloudnotifier.model;
import android.graphics.Color; import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
public class NotificationSettings public class NotificationSettings
{ {
public boolean EnableSound = false; public boolean EnableSound;
public String SoundName = ""; public String SoundName;
public String SoundSource = Uri.EMPTY.toString(); public String SoundSource;
public boolean RepeatSound = false; public boolean RepeatSound;
public boolean EnableLED = false; public boolean EnableLED;
public int LEDColor = Color.BLUE; public int LEDColor;
public boolean EnableVibration = false; public boolean EnableVibration;
public NotificationSettings(PriorityEnum p)
{
EnableSound = (p == PriorityEnum.HIGH);
SoundName = (p == PriorityEnum.HIGH) ? "Default" : "";
SoundSource = (p == PriorityEnum.HIGH) ? RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString() : Uri.EMPTY.toString();
RepeatSound = false;
EnableLED = (p == PriorityEnum.HIGH) || (p == PriorityEnum.NORMAL);
LEDColor = Color.BLUE;
EnableVibration = (p == PriorityEnum.HIGH) || (p == PriorityEnum.NORMAL);
}
} }

View File

@ -48,9 +48,9 @@ public class SCNSettings
public boolean Enabled = true; public boolean Enabled = true;
public int LocalCacheSize = 500; public int LocalCacheSize = 500;
public final NotificationSettings PriorityLow = new NotificationSettings(); public final NotificationSettings PriorityLow = new NotificationSettings(PriorityEnum.LOW);
public final NotificationSettings PriorityNorm = new NotificationSettings(); public final NotificationSettings PriorityNorm = new NotificationSettings(PriorityEnum.NORMAL);
public final NotificationSettings PriorityHigh = new NotificationSettings(); public final NotificationSettings PriorityHigh = new NotificationSettings(PriorityEnum.HIGH);
// ------------------------------------------------------------ // ------------------------------------------------------------

View File

@ -28,9 +28,7 @@ import androidx.core.app.NotificationCompat;
public class NotificationService public class NotificationService
{ {
private final static String CHANNEL_ID_LOW = "CHAN_BFB_SCN_MESSAGES_LOW"; private final static String CHANNEL_ID = "CHAN_BFB_SCN_MESSAGES";
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 final static Object _lock = new Object();
private static NotificationService _inst = null; private static NotificationService _inst = null;
@ -56,49 +54,15 @@ public class NotificationService
NotificationManager notifman = ctxt.getSystemService(NotificationManager.class); NotificationManager notifman = ctxt.getSystemService(NotificationManager.class);
if (notifman == null) return; if (notifman == null) return;
NotificationChannel channelLow = notifman.getNotificationChannel(CHANNEL_ID_LOW); NotificationChannel channel = notifman.getNotificationChannel(CHANNEL_ID);
if (channelLow == null) notifman.createNotificationChannel(channelLow = new NotificationChannel(CHANNEL_ID_LOW, "Push notifications (0 - low)", NotificationManager.IMPORTANCE_LOW)); if (channel == null)
NotificationChannel channelNorm = notifman.getNotificationChannel(CHANNEL_ID_NORM);
if (channelNorm == null) notifman.createNotificationChannel(channelNorm = new NotificationChannel(CHANNEL_ID_NORM, "Push notifications (1 - normal)", NotificationManager.IMPORTANCE_DEFAULT));
NotificationChannel channelHigh = notifman.getNotificationChannel(CHANNEL_ID_HIGH);
if (channelHigh == null) notifman.createNotificationChannel(channelHigh = new NotificationChannel(CHANNEL_ID_HIGH, "Push notifications (2 - high)", NotificationManager.IMPORTANCE_HIGH));
channelLow.setDescription("Messages from the API with priority set to low");
updateSingleChannel(channelLow, SCNSettings.inst().PriorityLow, PriorityEnum.LOW);
channelNorm.setDescription("Messages from the API with priority set to normal");
updateSingleChannel(channelNorm, SCNSettings.inst().PriorityNorm, PriorityEnum.NORMAL);
channelHigh.setDescription("Messages from the API with priority set to high");
updateSingleChannel(channelHigh, SCNSettings.inst().PriorityHigh, PriorityEnum.HIGH);
}
private void updateSingleChannel(NotificationChannel c, NotificationSettings s, PriorityEnum p)
{ {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return; channel = new NotificationChannel(CHANNEL_ID, "Push notifications", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Push notifications from the server");
c.enableLights(s.EnableLED); channel.setSound(null, null);
c.setLightColor(s.LEDColor); channel.setVibrationPattern(null);
c.enableVibration(s.EnableVibration); notifman.createNotificationChannel(channel);
c.setVibrationPattern(new long[]{500});
if (s.EnableSound)
{
AudioAttributes attr = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
.build();
c.setSound(Uri.parse(s.SoundSource), attr);
} }
else
{
c.setSound(null, null);
}
if (p == PriorityEnum.HIGH) c.setBypassDnd(true);
if (p == PriorityEnum.HIGH) c.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
} }
public void showForeground(CMessage msg) public void showForeground(CMessage msg)
@ -120,16 +84,16 @@ public class NotificationService
Ringtone rt = RingtoneManager.getRingtone(SCNApp.getContext(), Uri.parse(ns.SoundSource)); Ringtone rt = RingtoneManager.getRingtone(SCNApp.getContext(), Uri.parse(ns.SoundSource));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) rt.setLooping(false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) rt.setLooping(false);
rt.play(); rt.play();
new Thread(() -> { try { Thread.sleep(2*1000); } catch (InterruptedException e) { /* */ } rt.stop(); }).start(); new Thread(() -> { try { Thread.sleep(5*1000); } catch (InterruptedException e) { /* */ } rt.stop(); }).start();
} }
if (ns.EnableVibration) if (ns.EnableVibration)
{ {
Vibrator v = (Vibrator) SCNApp.getContext().getSystemService(Context.VIBRATOR_SERVICE); Vibrator v = (Vibrator) SCNApp.getContext().getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)); v.vibrate(VibrationEffect.createOneShot(1500, VibrationEffect.DEFAULT_AMPLITUDE));
} else { } else {
v.vibrate(500); v.vibrate(1500);
} }
} }
} }
@ -143,35 +107,33 @@ public class NotificationService
{ {
Context ctxt = SCNApp.getContext(); Context ctxt = SCNApp.getContext();
String channel = CHANNEL_ID_NORM;
NotificationSettings ns = SCNSettings.inst().PriorityNorm; NotificationSettings ns = SCNSettings.inst().PriorityNorm;
switch (msg.Priority) switch (msg.Priority)
{ {
case LOW: ns = SCNSettings.inst().PriorityLow; channel = CHANNEL_ID_LOW; break; case LOW: ns = SCNSettings.inst().PriorityLow; break;
case NORMAL: ns = SCNSettings.inst().PriorityNorm; channel = CHANNEL_ID_NORM; break; case NORMAL: ns = SCNSettings.inst().PriorityNorm; break;
case HIGH: ns = SCNSettings.inst().PriorityHigh; channel = CHANNEL_ID_HIGH; break; case HIGH: ns = SCNSettings.inst().PriorityHigh; break;
} }
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctxt, channel); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
{
// old
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctxt, CHANNEL_ID);
mBuilder.setSmallIcon(R.drawable.ic_bfb); mBuilder.setSmallIcon(R.drawable.ic_bfb);
mBuilder.setContentTitle(msg.Title); mBuilder.setContentTitle(msg.Title);
mBuilder.setContentText(msg.Content); mBuilder.setContentText(msg.Content);
mBuilder.setShowWhen(true); mBuilder.setShowWhen(true);
mBuilder.setWhen(msg.Timestamp); mBuilder.setWhen(msg.Timestamp * 1000);
mBuilder.setAutoCancel(true); 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.LOW) mBuilder.setPriority(NotificationCompat.PRIORITY_LOW);
if (msg.Priority == PriorityEnum.NORMAL) mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT); if (msg.Priority == PriorityEnum.NORMAL) mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (msg.Priority == PriorityEnum.HIGH) mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH); if (msg.Priority == PriorityEnum.HIGH) mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
if (ns.EnableVibration) mBuilder.setVibrate(new long[]{500}); if (ns.EnableVibration) mBuilder.setVibrate(new long[]{500});
if (ns.EnableLED) mBuilder.setLights(ns.LEDColor, 500, 500); if (ns.EnableLED) mBuilder.setLights(ns.LEDColor, 500, 500);
}
if (ns.EnableSound && !ns.SoundSource.isEmpty()) if (ns.EnableSound && !ns.SoundSource.isEmpty()) mBuilder.setSound(Uri.parse(ns.SoundSource), AudioManager.STREAM_NOTIFICATION);
{
mBuilder.setSound(Uri.parse(ns.SoundSource), AudioManager.STREAM_NOTIFICATION);
}
Intent intent = new Intent(ctxt, MainActivity.class); Intent intent = new Intent(ctxt, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(ctxt, 0, intent, 0); PendingIntent pi = PendingIntent.getActivity(ctxt, 0, intent, 0);
@ -183,4 +145,48 @@ public class NotificationService
if (mNotificationManager != null) mNotificationManager.notify(0, n); if (mNotificationManager != null) mNotificationManager.notify(0, n);
} }
else
{
// new
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctxt, CHANNEL_ID);
mBuilder.setSmallIcon(R.drawable.ic_bfb);
mBuilder.setContentTitle(msg.Title);
mBuilder.setContentText(msg.Content);
mBuilder.setShowWhen(true);
mBuilder.setWhen(msg.Timestamp * 1000);
mBuilder.setAutoCancel(true);
if (ns.EnableLED) mBuilder.setLights(ns.LEDColor, 500, 500);
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);
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) return;
Notification n = mBuilder.build();
n.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, n);
if (ns.EnableSound && !ns.SoundSource.isEmpty())
{
Ringtone rt = RingtoneManager.getRingtone(SCNApp.getContext(), Uri.parse(ns.SoundSource));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) rt.setLooping(false);
rt.play();
new Thread(() -> { try { Thread.sleep(5*1000); } catch (InterruptedException e) { /* */ } rt.stop(); }).start();
}
if (ns.EnableVibration)
{
Vibrator v = (Vibrator) SCNApp.getContext().getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(VibrationEffect.createOneShot(1500, VibrationEffect.DEFAULT_AMPLITUDE));
}
}
}
} }

View File

@ -201,7 +201,6 @@ public class SettingsFragment extends Fragment implements MusicPickerListener
{ {
SCNSettings.inst().save(); SCNSettings.inst().save();
updateUI(); updateUI();
NotificationService.inst().updateChannels();
} }
private void onUpgradeAccount() private void onUpgradeAccount()