Notificación de Android .addAction obsoleta en api 23
¿Cuál es la forma correcta de añadir una acción a la notificación en la API 23
ya que addAction(int icon, CharSequence title, PendingIntent intent)
está obsoleta? No se pudo encontrar ningún ejemplo, gracias.
Mi acción antigua: .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)
En lugar de éste:
AddAction (icono int, título CharSequence, intención PendingIntent)
Este método fue obsoleto en API nivel 23.
Utilizar:
AddAction (acción de notificación.Acción)
¡Está todo en los documentos del desarrollador!
Así que para usar esto:
Primero construya su acción con el NotificationCompat.Action.Builder
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_prev, "Previous", prevPendingIntent).build();
Nota: Utilice NotificationCompat.Action
Y que añádalo a tu notificación:
yournotification.addAction(action);
Utilice la clase Icon
en el primer parámetro para Drawable si api level> = 23 (marshmallow)
https://developer.android.com/reference/android/app/Notification.Action.Builder.html
https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.Action.Builder.html
ejemplo)
Notification.Action action = new Notification.Action.Builder( Icon.createWithResource(this, R.drawable.ic_prev), "action string", pendingIntent).build();
//Exemple of notification with Button private void scheduleNotificationWithButton(String message) { int notifReCode = 1; //What happen when you will click on button Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT); //Button NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Go", pendingIntent).build(); //Notification Notification notification = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentText("Back to Application ?") .setContentTitle("Amazing news") .addAction(action) //add buton .build(); //Send notification NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notification); }
Sólo tienes que usar el constructor NotificationCompat.Builder en lugar del constructor Notification.Builder porque NotificationCompat.Builder te permite crear tu aplicación debajo de Android Version 4.1
Resuelto mediante NotificationCompat.builder:
String strTitle = getString(R.string.new_message); String strText = getString(R.string.hi_whats_up); Intent intent = new Intent(this, NotificationView.class); intent.putExtra("title", strTitle); intent.putExtra("text", strText); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setTicker("Notification Ticker") .setContentTitle("Notification Title") .setContentText("Notification Text") .addAction(R.mipmap.ic_launcher, "Notification Action", pendingIntent) .setContentIntent(pendingIntent) .setAutoCancel(true); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(0, builder.build());