Merge pull request #834 from MilosKozak/notif-channels

Notification channels
This commit is contained in:
Milos Kozak 2018-04-04 16:46:25 +02:00 committed by GitHub
commit bf32b1e065
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 3 deletions

View file

@ -1,11 +1,14 @@
package info.nightscout.androidaps.plugins.Loop; package info.nightscout.androidaps.plugins.Loop;
import android.annotation.SuppressLint;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.TaskStackBuilder; import android.app.TaskStackBuilder;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import com.crashlytics.android.answers.CustomEvent; import com.crashlytics.android.answers.CustomEvent;
@ -48,6 +51,9 @@ import info.nightscout.utils.SP;
public class LoopPlugin extends PluginBase { public class LoopPlugin extends PluginBase {
private static Logger log = LoggerFactory.getLogger(LoopPlugin.class); private static Logger log = LoggerFactory.getLogger(LoopPlugin.class);
public static final String CHANNEL_ID = "AndroidAPS-Openloop";
protected static LoopPlugin loopPlugin; protected static LoopPlugin loopPlugin;
public static LoopPlugin getPlugin() { public static LoopPlugin getPlugin() {
@ -90,9 +96,22 @@ public class LoopPlugin extends PluginBase {
@Override @Override
protected void onStart() { protected void onStart() {
MainApp.bus().register(this); MainApp.bus().register(this);
createNotificationChannel();
super.onStart(); super.onStart();
} }
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager =
(NotificationManager) MainApp.instance().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
@SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_ID,
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
}
}
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
@ -322,7 +341,7 @@ public class LoopPlugin extends PluginBase {
} else { } else {
if (result.isChangeRequested() && allowNotification) { if (result.isChangeRequested() && allowNotification) {
NotificationCompat.Builder builder = NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainApp.instance().getApplicationContext()); new NotificationCompat.Builder(MainApp.instance().getApplicationContext(), CHANNEL_ID);
builder.setSmallIcon(R.drawable.notif_icon) builder.setSmallIcon(R.drawable.notif_icon)
.setContentTitle(MainApp.sResources.getString(R.string.openloop_newsuggestion)) .setContentTitle(MainApp.sResources.getString(R.string.openloop_newsuggestion))
.setContentText(resultAfterConstraints.toString()) .setContentText(resultAfterConstraints.toString())

View file

@ -1,5 +1,7 @@
package info.nightscout.androidaps.plugins.Overview.notifications; package info.nightscout.androidaps.plugins.Overview.notifications;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -8,6 +10,7 @@ import android.graphics.BitmapFactory;
import android.media.AudioAttributes; import android.media.AudioAttributes;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -28,11 +31,15 @@ import info.nightscout.utils.SP;
*/ */
public class NotificationStore { public class NotificationStore {
public static final String CHANNEL_ID = "AndroidAPS-Overview";
private static Logger log = LoggerFactory.getLogger(NotificationStore.class); private static Logger log = LoggerFactory.getLogger(NotificationStore.class);
public List<Notification> store = new ArrayList<Notification>(); public List<Notification> store = new ArrayList<Notification>();
public long snoozedUntil = 0L; public long snoozedUntil = 0L;
public NotificationStore() { public NotificationStore() {
createNotificationChannel();
} }
public class NotificationComparator implements Comparator<Notification> { public class NotificationComparator implements Comparator<Notification> {
@ -110,7 +117,7 @@ public class NotificationStore {
Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.blueowl); Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.blueowl);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
NotificationCompat.Builder notificationBuilder = NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context) new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(largeIcon) .setLargeIcon(largeIcon)
.setContentText(n.text) .setContentText(n.text)
@ -127,4 +134,17 @@ public class NotificationStore {
} }
mgr.notify(n.id, notificationBuilder.build()); mgr.notify(n.id, notificationBuilder.build());
} }
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager =
(NotificationManager) MainApp.instance().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
@SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_ID,
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
}
}
} }

View file

@ -1,11 +1,14 @@
package info.nightscout.androidaps.plugins.Persistentnotification; package info.nightscout.androidaps.plugins.Persistentnotification;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder; import android.support.v4.app.TaskStackBuilder;
@ -41,6 +44,8 @@ import info.nightscout.utils.DecimalFormatter;
public class PersistentNotificationPlugin extends PluginBase { public class PersistentNotificationPlugin extends PluginBase {
public static final String CHANNEL_ID = "AndroidAPS-Ongoing";
private static final int ONGOING_NOTIFICATION_ID = 4711; private static final int ONGOING_NOTIFICATION_ID = 4711;
private final Context ctx; private final Context ctx;
@ -57,10 +62,23 @@ public class PersistentNotificationPlugin extends PluginBase {
@Override @Override
protected void onStart() { protected void onStart() {
MainApp.bus().register(this); MainApp.bus().register(this);
createNotificationChannel();
updateNotification(); updateNotification();
super.onStart(); super.onStart();
} }
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager mNotificationManager =
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
@SuppressLint("WrongConstant") NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_ID,
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
}
}
@Override @Override
protected void onStop() { protected void onStop() {
MainApp.bus().unregister(this); MainApp.bus().unregister(this);
@ -118,7 +136,7 @@ public class PersistentNotificationPlugin extends PluginBase {
line3 += " - " + MainApp.getConfigBuilder().getProfileName(); line3 += " - " + MainApp.getConfigBuilder().getProfileName();
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx); NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
builder.setOngoing(true); builder.setOngoing(true);
builder.setCategory(NotificationCompat.CATEGORY_STATUS); builder.setCategory(NotificationCompat.CATEGORY_STATUS);
builder.setSmallIcon(R.drawable.ic_notification); builder.setSmallIcon(R.drawable.ic_notification);