servicio saber notification intent implement how example esta ejemplo corriendo como android service kotlin android-notifications background-service

saber - intentservice android ejemplo



startForeground falla después de actualizar a Android 8.1 (7)

Después de actualizar mi teléfono a 8.1 Developer Preview, mi servicio en segundo plano ya no se inicia correctamente.

En mi servicio de larga ejecución, he implementado un método startForeground para iniciar la notificación en curso que se llama al crear.

@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) }

Mensaje de error:

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)

canal no válido para notificación de servicio , aparentemente mi canal anterior, el DEFAULT_CHANNEL_ID, ya no es apropiado para API 27, supongo. ¿Cuál sería el canal adecuado? He intentado revisar la documentación.


Solución Java (Android 9.0, API 28)

En su clase de Service , agregue esto:

@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); }

ACTUALIZACIÓN: ANDROID 9.0 PIE (API 28)

Agregue este permiso a su archivo AndroidManifest.xml :

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />


Después de algunos retoques con una solución diferente, parece que en 8.1 debe crear su propio canal de notificación.

private fun startForeground() { val channelId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel("my_service", "My Background Service") } 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) } @RequiresApi(Build.VERSION_CODES.O) private fun createNotificationChannel(channelId: String, channelName: String): String{ 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 }

Según tengo entendido, los servicios en segundo plano ahora se muestran como notificaciones normales que el usuario puede seleccionar para no mostrar anulando la selección del canal de notificación.

Actualización : Tampoco olvides agregar el permiso de primer plano según sea necesario Android P:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />


En mi caso, es porque intentamos publicar una notificación sin especificar el 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)); } }

El mejor lugar para poner el código anterior es en el método onCreate() en la clase Application , por lo que solo tenemos que declararlo de una vez por todas:

public class App extends Application { @Override public void onCreate() { super.onCreate(); initChannel(); } }

Después de configurar esto, podemos usar la notificación con el channelId que acabamos de especificar:

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);

Luego, podemos usarlo para publicar una notificación:

int notifId = 45; NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(notifId, builder.build());

Si desea utilizarlo como notificación de servicio en primer plano:

startForeground(notifId, builder.build());


Esto funcionó para mí. En mi clase de servicio, creé el canal de notificación para Android 8.1 de la siguiente manera:

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); } } }

Nota: Cree el canal donde está creando la Notificación para Build.VERSION.SDK_INT >= Build.VERSION_CODES.O


Funciona correctamente en Andorid 8.1:

Muestra actualizada (sin ningún código obsoleto):

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); } }

Viejo cortado (es una aplicación diferente, no relacionada con el código anterior):

@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; }


Gracias a @CopsOnRoad, su solución fue de gran ayuda pero solo funciona para SDK 26 y superior. Mi aplicación apunta a 24 y más.

Para evitar que Android Studio se queje, necesita un condicional directamente alrededor de la notificación. No es lo suficientemente inteligente como para saber que el código está en un método condicional a VERSION_CODE.O.

~~~ @Override public void onCreate () {super.onCreate (); if (Build.VERSION.SDK_INT> = Build.VERSION_CODES.O) startMyOwnForeground (); de lo contrario, startForeground (1, nueva notificación ()); }

private void startMyOwnForeground(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ 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(AppSpecific.SMALL_ICON) .setContentTitle("App is running in background") .setPriority(NotificationManager.IMPORTANCE_MIN) .setCategory(Notification.CATEGORY_SERVICE) .build(); startForeground(2, notification); } }

~~~


La primera respuesta es excelente solo para aquellas personas que conocen kotlin, para aquellos que todavía usan Java aquí traduzco la primera respuesta

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"; }

Para Android, P no olvide incluir este permiso

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />