Merge pull request #1852 from MilosKozak/sdk28-notification

Sdk28 notification
This commit is contained in:
Milos Kozak 2019-06-30 15:20:29 +02:00 committed by GitHub
commit 30eae0b456
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 15 deletions

View file

@ -31,6 +31,7 @@ public class DummyService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, PersistentNotificationPlugin.getPlugin().updateNotification());
return START_STICKY; return START_STICKY;
} }
@ -45,11 +46,9 @@ public class DummyService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
// TODO: I guess this was moved here in order to adhere to the 5 seconds rule to call "startForeground" after a Service was called as Foreground service?
// As onCreate() is not called every time a service is started, copied to onStartCommand().
Notification notification = PersistentNotificationPlugin.getPlugin().updateNotification(); Notification notification = PersistentNotificationPlugin.getPlugin().updateNotification();
if (notification == null) {
log.debug("notification==null");
notification = new Notification();
}
startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, notification); startForeground(PersistentNotificationPlugin.ONGOING_NOTIFICATION_ID, notification);
MainApp.bus().register(this); MainApp.bus().register(this);
} }

View file

@ -17,6 +17,8 @@ import androidx.core.app.TaskStackBuilder;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import javax.annotation.Nonnull;
import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainActivity; import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
@ -79,7 +81,8 @@ public class PersistentNotificationPlugin extends PluginBase {
.neverVisible(true) .neverVisible(true)
.pluginName(R.string.ongoingnotificaction) .pluginName(R.string.ongoingnotificaction)
.enableByDefault(true) .enableByDefault(true)
.alwaysEnabled(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) .alwaysEnabled(true)
.showInList(false)
.description(R.string.description_persistent_notification) .description(R.string.description_persistent_notification)
); );
this.ctx = ctx; this.ctx = ctx;
@ -87,8 +90,8 @@ public class PersistentNotificationPlugin extends PluginBase {
@Override @Override
protected void onStart() { protected void onStart() {
createNotificationChannel(); // make sure channels exist before triggering updates through the bus
MainApp.bus().register(this); MainApp.bus().register(this);
createNotificationChannel();
triggerNotificationUpdate(); triggerNotificationUpdate();
super.onStart(); super.onStart();
} }
@ -112,18 +115,14 @@ public class PersistentNotificationPlugin extends PluginBase {
} }
private void triggerNotificationUpdate() { private void triggerNotificationUpdate() {
if (updateNotification() != null) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) MainApp.instance().startForegroundService(new Intent(MainApp.instance(), DummyService.class));
MainApp.instance().startForegroundService(new Intent(MainApp.instance(), DummyService.class)); else
else MainApp.instance().startService(new Intent(MainApp.instance(), DummyService.class));
MainApp.instance().startService(new Intent(MainApp.instance(), DummyService.class));
} }
@Nonnull
Notification updateNotification() { Notification updateNotification() {
if (!isEnabled(PluginType.GENERAL)) {
return null;
}
String line1 = null; String line1 = null;
String line2 = null; String line2 = null;
String line3 = null; String line3 = null;