【问题标题】:Implement expand and collapse notification android实现展开和折叠通知android
【发布时间】:2012-12-05 10:08:00
【问题描述】:

android 4.0 及以上版本我需要在状态栏中实现展开和折叠通知。我在谷歌上搜索过这个但没有得到任何实现的代码解决方案有人知道如何实现这个

提前谢谢你

【问题讨论】:

  • 您如何布置通知。用RemoteView自定义布局,还是默认NotificationBuilder?

标签: android notifications


【解决方案1】:

可扩展的Notification 是Notification Big 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 是一个教程。

【讨论】:

  • 感谢@Gunnar,它对我很有帮助
  • 我们可以在android 4.0版本中创建可扩展通知吗?
  • 这意味着在android 4.1之前没有办法增加通知高度,即使是自定义通知?
  • 我使用 BigView 以相同的方式实现它,我有一个问题,在某些手机上滑动通知以展开有点困难。我们必须用整个手指在通知区域上滚动(手指在通知上的整个表面区域然后向下滑动)。关闭应用程序使用 bigView,以便我可以比较它是依赖于设备还是存在一些代码错误。
【解决方案2】:
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)

这是一个例子:

Expandable Notifications and Notifications Groups

你可以设置代码

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 日:

您应该可以阅读详细信息:Expandable Notifications and Notifications Groups

【讨论】:

  • 我的 BigText 重叠很小。这不是点击展开
【解决方案3】:

我无法在 Notification 的 .setStyle() 方法中设置 new 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();

【讨论】:

    【解决方案4】:

    此功能有一种方法,您可以在通知折叠时显示一个小图标,并在通知展开时显示一个大图标。

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

    【讨论】:

      【解决方案5】:

      我们无法在 android 4.1 以下版本中创建可扩展通知。 但是我们可以这样做,而不是这样做,我们可以堆叠通知,然后我们可以为我们漂亮的自定义活动设置一个挂起的意图,它会在列表中显示所有通知。 用户会很高兴看到这个:)

      【讨论】:

        【解决方案6】:

        (2021) 当我在扩展具有较大文本的通知时遇到问题时偶然发现了这个问题。 参考Android docs on Expandable Notifications解决:Create an Expandable Notification

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

        【讨论】:

          猜你喜欢
          • 2011-09-17
          • 1970-01-01
          • 2017-04-09
          • 2015-12-25
          • 2018-12-13
          • 2013-06-06
          • 1970-01-01
          • 2015-04-07
          • 2017-03-09
          相关资源
          最近更新 更多