Skip to content

Commit

Permalink
Use Notification channel (required for API 27, but backwards compatible)
Browse files Browse the repository at this point in the history
See:
https://developer.android.com/guide/topics/ui/notifiers/notifications.html

At some point, it may also be good to set those channel strings as a
resource.
  • Loading branch information
fat-tire committed Dec 24, 2017
1 parent a46937a commit 3e2231c
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/main/java/org/havenapp/main/service/MonitorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@


import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
Expand All @@ -25,12 +28,8 @@
import android.text.TextUtils;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;

import org.havenapp.main.MonitorActivity;
import org.havenapp.main.HavenApp;
import org.havenapp.main.MonitorActivity;
import org.havenapp.main.PreferenceManager;
import org.havenapp.main.R;
import org.havenapp.main.model.Event;
Expand All @@ -40,6 +39,10 @@
import org.havenapp.main.sensors.BarometerMonitor;
import org.havenapp.main.sensors.MicrophoneMonitor;

import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;

@SuppressLint("HandlerLeak")
public class MonitorService extends Service {

Expand All @@ -51,7 +54,11 @@ public class MonitorService extends Service {
/**
* To show a notification on service start
*/
private NotificationManager manager;
NotificationManager manager;
NotificationChannel mChannel;
final static String channelId = "monitor_id";
final static CharSequence channelName = "Haven notifications";
final static String channelDescription= "Important messages from Haven";

/**
* True only if service has been alerted by the accelerometer
Expand Down Expand Up @@ -122,6 +129,14 @@ public void onCreate() {
manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mPrefs = new PreferenceManager(this);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(channelId, channelName,
NotificationManager.IMPORTANCE_HIGH);
mChannel.setDescription(channelDescription);
mChannel.setLightColor(Color.RED);
manager.createNotificationChannel(mChannel);
}

startSensors();

showNotification();
Expand Down Expand Up @@ -184,7 +199,7 @@ private void showNotification() {
CharSequence text = getText(R.string.secure_service_started);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_haven)
.setContentTitle(getString(R.string.app_name))
.setContentText(text);
Expand Down

0 comments on commit 3e2231c

Please sign in to comment.