2012-12-05 109 views
23

我需要在狀態欄中爲Android 4.0及以上版本實現展開和摺疊通知。我已經在谷歌搜索這一點,但並沒有得到執行任何代碼解決方案沒有任何人有我的想法如何實現這一執行展開和摺疊通知android

預先感謝您

+0

你如何佈置您的通知。使用'RemoteView'自定義佈局,或默認'NotificationBuilder'? –

回答

37

的可擴張的NotificationNotificationBig View的一個特例。如果Big View不在通知抽屜的頂部,則它顯示爲「關閉」並可通過滑動進行擴展。來自Android開發人員的報價:

通知的大視圖僅在通知展開時纔會出現,當通知位於通知抽屜頂部時,或用戶通過手勢展開通知時發生。擴展通知從Android 4.1開始。

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

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

Here是一個教程:

Big ViewNotification可以如下創建。

+0

Thanks @Gunnar對我真的很有幫助 – Pratik

+0

@Pratik很高興幫助:) –

+0

我們可以在Android 4.0版本中創建可擴展通知嗎? – prateek

0

我們無法在Android 4.1以下版本中創建可擴展通知。 但是,我們可以做,而不是我們可以堆疊的通知,然後我們可以設置一個掛起的意向我們漂亮的自定義活動,它顯示列表中的所有通知。 用戶會樂意看到這個:)

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

您更改

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

與公告OK一起!

notification = new NotificationCompat.Builder(context) 

下面是一個例子:

enter image description here您可以設置代碼

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

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

正確和完整的答案,謝謝! –

+0

我的BigText重疊小。這不會在點擊時展開 – TeodorKolev