2012-12-05 8 views
23

アンドロイド4.0以上のステータスバーに展開と折りたたみの通知を実装する必要があります。私はこのためにGoogleで検索しましたが、誰もが展開と折りたたみの通知を実装するandroid

+0

を設定することができます。 'RemoteView'カスタムレイアウト、またはデフォルトの' NotificationBuilder'? –

答えて

37

拡張NotificationNotificationBig Viewの特殊なケースである。この

は、事前にありがとう実装する方法私のアイデアを持っていない実装のための任意のコードソリューションを取得しませんでした。 Big Viewが通知トレイの上部にない場合は、「閉じた」と表示され、スワイプで拡張可能です。 Androidデベロッパーからのお見積もり:

通知が拡大された場合、通知が通知トレイの一番上にあるとき、またはユーザーがジェスチャーを使用して通知を展開した場合にのみ表示されます。拡張された通知は、Android 4.1から利用可能です。次のように

Big ViewNotificationを作成することができる。

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チュートリアルです。

+0

ありがとう@Gunnarその本当に私に役立ちます – Pratik

+0

@Pratik喜んでそれを助けました:) –

+0

我々はアンドロイド4.0のバージョンで拡張可能な通知を作成することはできますか? – prateek

0

アンドロイド4.1の下に拡張可能な通知を作成することはできません。 しかし、これに代えて、通知を積み重ねることができます。次に、すべての通知をリストに表示するわかりやすいカスタムアクティビティに保留中のインテントを設定することができます。 ユーザーはこれを見て幸せだろう:)

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

あなたがOKの発表とともに

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

を変更!ここで

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

関連する問題