2012-12-05 41 views
23

Tôi cần triển khai thông báo mở rộng và thu gọn trong thanh trạng thái cho phiên bản Android 4.0 trở lên. Tôi đã tìm kiếm trên google cho điều này nhưng không nhận được bất kỳ giải pháp mã thực hiện không ai có ý tưởng làm thế nào để tôi thực hiện điều nàyTriển khai thông báo mở rộng và thu gọn android

cảm ơn trước

+0

How are you đặt ra Thông báo của bạn. Với bố cục tùy chỉnh 'RemoteView', hoặc' NotificationBuilder' mặc định? –

Trả lời

37

Một mở rộng Notification là một trường hợp đặc biệt của một NotificationBig View. Nếu Big View không ở trên cùng của ngăn thông báo, nó hiển thị 'đã đóng' và có thể mở rộng bằng cách vuốt. Trích dẫn từ nhà phát triển Android:

Chế độ xem lớn của thông báo chỉ xuất hiện khi thông báo được mở rộng, xảy ra khi thông báo ở đầu ngăn thông báo hoặc khi người dùng mở rộng thông báo bằng cử chỉ. Thông báo mở rộng có sẵn bắt đầu với Android 4.1.

Các Big ViewNotification thể được tạo ra như sau:

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

hoặc

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

Here là một hướng dẫn.

+0

Cảm ơn @Gunnar thực sự hữu ích với tôi – Pratik

+0

@Pratik Vui mừng nó đã giúp :) –

+0

chúng tôi có thể tạo thông báo có thể mở rộng trong các phiên bản Android 4.0 không? – prateek

0

Chúng tôi không thể tạo thông báo có thể mở rộng ở bên dưới phiên bản Android 4.1. Nhưng thay vì điều này chúng ta có thể làm điều đó chúng ta có thể xếp chồng các thông báo và sau đó chúng ta có thể đặt một mục đích đang chờ xử lý cho hoạt động khá tùy chỉnh của chúng ta, hiển thị tất cả thông báo trong danh sách. Người dùng sẽ vui mừng khi thấy này :)

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

Bạn thay đổi

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

cùng với việc công bố OK !!!

notification = new NotificationCompat.Builder(context) 

Dưới đây là một ví dụ:

enter image description here Bạn có thể đặt Mã

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

hoặc

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

Câu trả lời đúng và đầy đủ, cảm ơn bạn! –

+0

My BigText trùng lặp nhỏ. Điều này không mở rộng khi nhấp chuột – TeodorKolev

Các vấn đề liên quan