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:
- Permitir cancelación de la notificación después de llamar a stopForeground (false)
- ¿Algún modo de vincular a la configuración de notificación de Android para mi aplicación?
- establecer un temporizador en Android basado en una fecha en la base de datos
- Detectar si se ha eliminado la notificación
- Android: administrar múltiples notificaciones push en el dispositivo de una aplicación
... 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?
- RemoteView no muestra botones en la notificación personalizada
- Tamaño grande del icono de la notificación del empuje de GCM
- Sonido de notificación de Android
- Actualización de GCM 7.5 a 8.3.0 excepción fatal
- Mensajes de Android - Desarrollo vs distribución
- StartActivity no funciona cuando llama de BroadcastReciever
- La notificación pasa Antiguo Intent Extras
- NotificationManager.cancel () no funciona para mí
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 .
- ListPopupWindow no obedeciendo WRAP_CONTENT width spec
- Compilar, Proporcionado, APK – Android dependencia alcance