2012-12-05 12 views
23

Devo implementare la notifica di espansione e compressione nella barra di stato per Android 4.0 e versioni successive. Ho una ricerca su Google per questo, ma non ricevendo alcuna soluzione codice per l'implementazione Qualcuno ha ho idea di come implementare questaImplementa espansione e comprimi notifica android

ringraziare in anticipo

+0

Come si posa la vostra notifica. Con il layout personalizzato di 'RemoteView', o il predefinito' NotificationBuilder'? –

risposta

37

Un espandibile Notification è un caso particolare di un NotificationBig View. Se lo Big View non si trova nella parte superiore del cassetto delle notifiche, viene visualizzato "chiuso" ed è espandibile tramite scorrimento. Citare da Sviluppatori Android: grande vista

A di notifica appare solo quando la notifica viene espanso, che avviene quando la notifica è nella parte superiore del cassetto notifica, o quando l'utente espande la notifica con un gesto. Le notifiche espanse sono disponibili a partire da Android 4.1.

Il Big ViewNotification può essere creato come segue:

Notification notification = new Notification.BigTextStyle(builder) 
.bigText(myText).build(); 

o

Notification notification = new Notification.BigPictureStyle(builder) 
.bigPicture(
    BitmapFactory.decodeResource(getResources(), 
    R.drawable.my_picture)).build(); 

Here è un tutorial.

+0

Grazie a @Gunnar è stato molto utile per me – Pratik

+0

@ Pratik Sono contento che mi abbia aiutato :) –

+0

possiamo creare una notifica espandibile nelle versioni di Android 4.0? – prateek

0

Non è possibile creare una notifica espandibile nelle seguenti versioni di Android 4.1. Ma invece di questo possiamo fare che possiamo impilare le notifiche e quindi possiamo impostare un intento in sospeso per la nostra attività piuttosto personalizzata che mostra tutte le notifiche in lista. Utente sarà felice di vedere questo :)

30
Notification noti = new Notification.Builder() 
... // The same notification properties as the others 
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap)) 
.build(); 

si cambia

.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert)) 

insieme con l'annuncio OK !!!

notification = new NotificationCompat.Builder(context) 

Ecco un esempio:

enter image description here È possibile impostare Codice

Intent intent = new Intent(context, ReserveStatusActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
NotificationManager notificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
intent = new Intent(String.valueOf(PushActivity.class)); 
intent.putExtra("message", MESSAGE); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
stackBuilder.addParentStack(PushActivity.class); 
stackBuilder.addNextIntent(intent); 
// PendingIntent pendingIntent = 
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

// android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new  NotificationCompat.BigTextStyle(); 
// bigStyle.bigText((CharSequence) context); 

notification = new NotificationCompat.Builder(context) 
    .setSmallIcon(R.mipmap.ic_launcher) 
    .setContentTitle(th_title) 
    .setContentText(th_alert) 
    .setAutoCancel(true) 
// .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า 
// .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title)) 
    .setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert)) 
    .setContentIntent(pendingIntent) 
    .setNumber(++numMessages) 
    .build(); 

notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
notificationManager.notify(1000, notification); 

o

private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) { 
     Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.logo); 

     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       // .setContentTitle(notification.getTitle()) 
       .setContentTitle(getResources().getText(R.string.app_name)) 
       .setContentText(notification.getBody()) 
       .setAutoCancel(true) 
       .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
       .setContentIntent(pendingIntent) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(notification.getBody())) 
       .setContentInfo(notification.getTitle()) 
       .setLargeIcon(icon) 
       .setColor(Color.RED) 
       .setSmallIcon(R.drawable.logo); 

     try { 
      String picture_url = data.get("picture_url"); 
      if (picture_url != null && !"".equals(picture_url)) { 
       URL url = new URL(picture_url); 
       Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
       notificationBuilder.setStyle(
         new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody()) 
       ); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE); 
     notificationBuilder.setLights(Color.YELLOW, 1000, 300); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationBuilder.build()); 
    } 
+0

Risposta corretta e completa, grazie! –

+0

My BigText si sovrappone piccolo. Questo non è espandibile al clic – TeodorKolev