Cómo implementar los métodos obsoletos de Notificación

Tengo un pequeño problema, pero no entiendo cómo salir de esto.

Creé una clase para proporcionar Notificaciones, pero estas líneas están marcadas como obsoletas:

... Notification notification = new Notification(icon, text, time); // deprecated in API level 11 ... notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11 ... 

Los métodos alternativos son:

 ... Notification noti = new Notification.Builder(mContext) .setContentTitle("New mail from " + sender.toString()) .setContentText(subject) .setSmallIcon(R.drawable.new_mail) .setLargeIcon(aBitmap) .build(); // available from API level 11 and onwards ... 

¿Puedo escribir un código como:

 if(API_level < 11) { ... Notification notification = new Notification(icon, text, time); // deprecated in API level 11 ... notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11 ... } else { ... Notification noti = new Notification.Builder(mContext) .setContentTitle("New mail from " + sender.toString()) .setContentText(subject) .setSmallIcon(R.drawable.new_mail) .setLargeIcon(aBitmap) .build(); // available from API level 11 and onwards ... } 

Proporciono la versión mínima de sdk como "8".

Editar:

Me gustó más abajo:

 int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){ Notification notification = new Notification(icon, text, time); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0); notification.setLatestEventInfo(this, title, text, contentIntent); notification.flags |= Notification.FLAG_AUTO_CANCEL; mNM.notify(NOTIFICATION, notification); } else { // what to write here } 

¿Qué puedo escribir para else porción?

Así es como terminé con la solución:

 if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { notification = new Notification(icon, text, time); notification.setLatestEventInfo(this, title, text, contentIntent); // This method is removed from the Android 6.0 notification.flags |= Notification.FLAG_AUTO_CANCEL; mNM.notify(NOTIFICATION, notification); } else { NotificationCompat.Builder builder = new NotificationCompat.Builder( this); notification = builder.setContentIntent(contentIntent) .setSmallIcon(icon).setTicker(text).setWhen(time) .setAutoCancel(true).setContentTitle(title) .setContentText(text).build(); mNM.notify(NOTIFICATION, notification); } 

Editar:

La solución anterior funciona. Sin embargo, desde que se introdujo la clase NotificationCompat.Builder , podemos omitir la condición if para comprobar que compara la versión actual de la API. Por lo tanto, podemos simplemente eliminar la condición if...else , e ir con:

 NotificationCompat.Builder builder = new NotificationCompat.Builder( this); notification = builder.setContentIntent(contentIntent) .setSmallIcon(icon).setTicker(text).setWhen(time) .setAutoCancel(true).setContentTitle(title) .setContentText(text).build(); mNM.notify(NOTIFICATION, notification); 

Esta es la forma correcta de obtener el nivel.

  final int sdkVersion = Integer.parseInt(Build.VERSION.SDK); if (sdkVersion < Build.VERSION_CODES.HONEYCOMB) { ... Notification notification = new Notification(icon, text, time); // deprecated in API level 11 ... notification.setLatestEventInfo(this, title, text, contentIntent); // deprecated in API level 11 ... } else { Notification noti = new Notification.Builder(mContext) .setContentTitle("New mail from " + sender.toString()) .setContentText(subject) .setSmallIcon(R.drawable.new_mail) .setLargeIcon(aBitmap) .build(); // available from API level 11 and onwards } 

Todos los códigos de versión se pueden encontrar en este enlace de desarrollador .

  • ¿Cómo encuestar un webservice en el intervalo finito de androide?
  • Icono de acción de notificación de Android Tamaños en diseño de material
  • Android, Mostrar alertaDialog en lugar de notificación cuando la aplicación está abierta
  • Android Parse Push Notification No se muestra Icono en Marshmallow
  • Analizar notificaciones push en Android
  • Notificaciones de la base de datos SQLite de Android
  • Adición de un botón de reproducción de terminación mediante Android L Notification.MediaStyle
  • Intención con el extra antiguo en onCreate () para singleTask Actividad
  • Obtenga el valor de RemoteMessage del método FCM onMessageReceived
  • Eliminar todas las notificaciones de la barra de notificaciones
  • Actualizar el texto de la notificación, no la notificación completa
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.