startForeground сбой после обновления до Android 8.1
после обновления моего телефона до 8.1 Developer Preview моя фоновая служба больше не запускается должным образом.
в моей длительной службе я реализовал метод startForeground для запуска текущего уведомления,которое вызывается при создании.
@TargetApi(Build.VERSION_CODES.O)
private fun startForeground() {
// Safe call, handled by compat lib.
val notificationBuilder = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
val notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.build()
startForeground(101, notification)
}
сообщение об ошибке:
11-28 11:47:53.349 24704-24704/$PACKAGE_NAMEE/AndroidRuntime: FATAL EXCEPTION: main
Process: $PACKAGE_NAME, PID: 24704
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My channel pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
неверный канал для уведомления службы, видимо мой старый канал DEFAULT_CHANNEL_ID больше не подходит для API 27 I предположить. Какой будет правильный канал? Я попытался просмотреть документацию
6 ответов:
после некоторой возни с другим решением кажется, что вы в 8.1 должны создать свой собственный канал уведомлений.
private fun startForeground() { val channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel() } else { // If earlier version channel ID is not used // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) "" } val notificationBuilder = NotificationCompat.Builder(this, channelId ) val notification = notificationBuilder.setOngoing(true) .setSmallIcon(R.mipmap.ic_launcher) .setPriority(PRIORITY_MIN) .setCategory(Notification.CATEGORY_SERVICE) .build() startForeground(101, notification) } private fun createNotificationChannel(): String{ val channelId = "my_service" val channelName = "My Background Service" val chan = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE) chan.lightColor = Color.BLUE chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager service.createNotificationChannel(chan) return channelId }из моего понимания фоновые службы теперь отображаются как обычные уведомления, которые пользователь затем может выбрать, чтобы не показывать, сняв флажок с канала уведомлений.
обновление: Также не забудьте добавить разрешение переднего плана по мере необходимости Android P:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
работает на Andorid 8.1:
обновленный образец (без какого-либо устаревшего кода):
public NotificationBattery(Context context) { this.mCtx = context; mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID) .setContentTitle(context.getString(R.string.notification_title_battery)) .setSmallIcon(R.drawable.ic_launcher) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setChannelId(CHANNEL_ID) .setOnlyAlertOnce(true) .setPriority(NotificationCompat.PRIORITY_MAX) .setWhen(System.currentTimeMillis() + 500) .setGroup(GROUP) .setOngoing(true); mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_view_battery); initBatteryNotificationIntent(); mBuilder.setContent(mRemoteViews); mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (AesPrefs.getBooleanRes(R.string.SHOW_BATTERY_NOTIFICATION, true)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, context.getString(R.string.notification_title_battery), NotificationManager.IMPORTANCE_DEFAULT); channel.setShowBadge(false); channel.setSound(null, null); mNotificationManager.createNotificationChannel(channel); } } else { mNotificationManager.cancel(Const.NOTIFICATION_CLIPBOARD); } }старый отрезал (это другое приложение -не связано с кодом выше):
@Override public int onStartCommand(Intent intent, int flags, final int startId) { Log.d(TAG, "onStartCommand"); String CHANNEL_ONE_ID = "com.kjtech.app.N1"; String CHANNEL_ONE_NAME = "Channel One"; NotificationChannel notificationChannel = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, IMPORTANCE_HIGH); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.setShowBadge(true); notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.createNotificationChannel(notificationChannel); } Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); Notification notification = new Notification.Builder(getApplicationContext()) .setChannelId(CHANNEL_ONE_ID) .setContentTitle(getString(R.string.obd_service_notification_title)) .setContentText(getString(R.string.service_notification_content)) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(icon) .build(); Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); notification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); startForeground(START_FOREGROUND_ID, notification); return START_STICKY; }
(включает в себя Android 9.0 Pie (API 28) обновление) Простое решение в Java
на
onCreate()методService@Override public void onCreate(){ super.onCreate(); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) startMyOwnForeground(); else startForeground(1, new Notification()); }определите свой собственный передний план здесь
private void startMyOwnForeground(){ String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp"; String channelName = "My Background Service"; NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE); chan.setLightColor(Color.BLUE); chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); assert manager != null; manager.createNotificationChannel(chan); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID); Notification notification = notificationBuilder.setOngoing(true) .setSmallIcon(R.drawable.icon_1) .setContentTitle("App is running in background") .setPriority(NotificationManager.IMPORTANCE_MIN) .setCategory(Notification.CATEGORY_SERVICE) .build(); startForeground(2, notification); }ОБНОВЛЕНИЕ: ANDORID 9.0 PIE (API 28)
приложения, предназначенные для Android 9 (уровень API 28) или выше и использующие службы переднего плана, должны запрашивать разрешение FOREGROUND_SERVICE. Это нормальное разрешение, поэтому система автоматически предоставляет его запрашивающему приложению.
если приложение, предназначенное для API уровня 28 или выше, пытается создать службу переднего плана без запроса FOREGROUND_SERVICE, система создает исключение SecurityException.
просто добавьте это разрешение в свой .
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
первый ответ Отлично подходит только для тех людей, которые знают Котлин, для тех, кто все еще использует java здесь я перевожу первый ответ
public Notification getNotification() { String channel; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) channel = createChannel(); else { channel = ""; } NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channel).setSmallIcon(android.R.drawable.ic_menu_mylocation).setContentTitle("snap map fake location"); Notification notification = mBuilder .setPriority(PRIORITY_LOW) .setCategory(Notification.CATEGORY_SERVICE) .build(); return notification; } @NonNull @TargetApi(26) private synchronized String createChannel() { NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); String name = "snap map fake location "; int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel mChannel = new NotificationChannel("snap map channel", name, importance); mChannel.enableLights(true); mChannel.setLightColor(Color.BLUE); if (mNotificationManager != null) { mNotificationManager.createNotificationChannel(mChannel); } else { stopSelf(); } return "snap map channel"; }
в моем случае, это потому, что мы пытались разместить уведомление без указания
NotificationChannel:public static final String NOTIFICATION_CHANNEL_ID_SERVICE = "com.mypackage.service"; public static final String NOTIFICATION_CHANNEL_ID_TASK = "com.mypackage.download_info"; public void initChannel(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "App Service", NotificationManager.IMPORTANCE_DEFAULT)); nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "Download Info", NotificationManager.IMPORTANCE_DEFAULT)); } }лучшее место, чтобы положить выше код находится в
onCreate()методApplicationкласс, так что нам просто нужно объявить его раз и навсегда:public class App extends Application { @Override public void onCreate() { super.onCreate(); initChannel(); } }после этого мы можем использовать уведомление с
channelIdмы только что указали:Intent i = new Intent(this, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pi = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID_INFO); .setContentIntent(pi) .setWhen(System.currentTimeMillis()) .setContentTitle("VirtualBox.exe") .setContentText("Download completed") .setSmallIcon(R.mipmap.ic_launcher);затем мы можем использовать его для отправки уведомления:
int notifId = 45; NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(notifId, builder.build());если вы хотите использовать его в качестве уведомление службы переднего плана:
startForeground(notifId, builder.build());
это работает для меня. В моем классе обслуживания, я создал канал уведомлений для Android 8.1, как показано ниже:
public class Service extends Service { public static final String NOTIFICATION_CHANNEL_ID_SERVICE = "com.package.MyService"; public static final String NOTIFICATION_CHANNEL_ID_INFO = "com.package.download_info"; @Override public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "App Service", NotificationManager.IMPORTANCE_DEFAULT)); nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "Download Info", NotificationManager.IMPORTANCE_DEFAULT)); } else { Notification notification = new Notification(); startForeground(1, notification); } } }Примечание: создайте канал, где вы создаете сообщение для
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
Comments