From 986ea05fb2aa6a3bfc979fe66984e2ae279c09d1 Mon Sep 17 00:00:00 2001 From: Torsten Grote <t@grobox.de> Date: Fri, 1 Dec 2017 12:49:14 -0200 Subject: [PATCH] Use NotificationChannel for foreground service to avoid crash on Android 8.1 This also seems to address #1075 at least on an emulator --- .../briarproject/briar/android/BriarService.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java index 0e6bb7c399..d3e26b3c22 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java @@ -1,5 +1,6 @@ package org.briarproject.briar.android; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -73,8 +74,20 @@ public class BriarService extends Service { stopSelf(); return; } + // Create mandatory notification channel + String channelId = "foregroundService"; + if (Build.VERSION.SDK_INT >= 26) { + NotificationChannel channel = new NotificationChannel(channelId, + getString(R.string.app_name), + NotificationManager.IMPORTANCE_NONE); + channel.setLockscreenVisibility(VISIBILITY_SECRET); + NotificationManager nm = + (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + nm.createNotificationChannel(channel); + } // Show an ongoing notification that the service is running - NotificationCompat.Builder b = new NotificationCompat.Builder(this); + NotificationCompat.Builder b = + new NotificationCompat.Builder(this, channelId); b.setSmallIcon(R.drawable.notification_ongoing); b.setColor(ContextCompat.getColor(this, R.color.briar_primary)); b.setContentTitle(getText(R.string.ongoing_notification_title)); -- GitLab