31

android4.0以降のバージョンのステータスバーに展開と折りたたみの通知を実装する必要があります。私はこれをグーグルで検索しましたが、実装のためのコードソリューションを取得していませんでした誰かがこれを実装する方法を考えていますか

前もって感謝します

4

6 に答える 6

47

拡張可能Notificationは、の特殊なケースですNotification Big View。が通知ドロワーの上部にない場合は、Big View「閉じた」と表示され、スワイプで展開できます。Android開発者からの引用:

通知の大きなビューは、通知が展開された場合にのみ表示されます。これは、通知が通知ドロワーの上部にある場合、またはユーザーがジェスチャーで通知を展開した場合に発生します。Android 4.1以降、拡張通知を利用できます。

Big View Notification次のように作成できます。

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

また

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

これがチュートリアルです。

于 2012-12-05T10:40:09.413 に答える
43
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)

次に例を示します。

拡張可能な通知および通知グループ

コードを設定できます

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

2021年7月27日:

あなたはこれを詳細に読むことができるはずです:拡張可能な通知と通知グループ

于 2015-09-28T20:09:15.800 に答える
3

Notificationの.setStyle()メソッドで新しいNotificationCompat.BigTextStyle()の新しいインスタンスを設定できませんでした。そこで、以下の.setStyle()のnew Notification.BigTextStyle()の新しいインスタンスを使用しました。

      Notification builder =new Notification.Builder(this)
                    .setSmallIcon(Notification_icons[icon])
                    .setContentTitle(title)
                    .setContentText(description)
                    .setChannelId(channelID_Default)
                    .setOngoing(true)
                    .setStyle(new Notification.BigTextStyle()
                            .bigText(description))
                    .build();
于 2019-05-15T15:26:22.173 に答える
1

この機能には、通知が折りたたまれたときに小さなアイコンを表示し、通知が展開されたときに大きなアイコンを表示する方法があります。

val bitmap = BitmapFactory.decodeResource(resources, R.drawable.notification)

var notification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.new_post)
    .setContentTitle(imageTitle)
    .setContentText(imageDescription)
    .setLargeIcon(bitmap)
    .setStyle(NotificationCompat.BigPictureStyle()
            .bigPicture(bitmap)
            .bigLargeIcon(null))
    .build()
于 2019-03-05T05:51:23.057 に答える
0

以下のAndroid4.1バージョンでは、拡張可能な通知を作成できません。しかし、これの代わりに、通知を積み重ねて、すべての通知をリストに表示するかなりカスタムなアクティビティに保留中のインテントを設定することができます。ユーザーはこれを見て喜ぶでしょう:)

于 2014-02-07T11:39:16.303 に答える
0

(2021)より大きなテキストを持つ通知の拡張で問題が発生したときに、この質問に遭遇しました。拡張可能な通知に関するAndroidドキュメントを参照して解決:拡張可能な通知を作成する

Nimisha Vの回答は、通知で大きな画像を拡大する方法を示しています。以下のコードは、通知の大きなテキストを展開するために使用されます

var notification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.icon)
    .setContentTitle(notification_title)
    .setContentText(notification_message)
    .setStyle(NotificationCompat.BigTextStyle()
            .bigText(notification_message))
    .build()
于 2021-07-20T02:13:28.067 に答える