实现扩展和折叠通知android

Pra*_*tik 29 notifications android

我需要在Android 4.0及更高版本的状态栏中实现扩展和折叠通知.我在google上搜索了这个,但没有得到任何代码解决方案实现有没有人知道如何实现这一点

先感谢您

Gun*_*son 42

可扩展Notification是一个特殊情况Notification Big View.如果Big View它不在通知抽屉的顶部,则它显示为"已关闭"并且可以通过滑动进行扩展.来自Android开发者的报价:

通知的大视图仅在通知展开时出现,当通知位于通知抽屉的顶部时,或者当用户使用手势展开通知时,会发生通知.从Android 4.1开始提供扩展通知.

Big View Notification可创建如下:

Notification notification = new Notification.BigTextStyle(builder)
.bigText(myText).build();
Run Code Online (Sandbox Code Playgroud)

要么

Notification notification = new Notification.BigPictureStyle(builder)
.bigPicture(
  BitmapFactory.decodeResource(getResources(),
    R.drawable.my_picture)).build();
Run Code Online (Sandbox Code Playgroud)

是一个教程.


Pon*_*ung 35

Notification noti = new Notification.Builder()
... // The same notification properties as the others
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))
.build();
Run Code Online (Sandbox Code Playgroud)

你改变了

.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
Run Code Online (Sandbox Code Playgroud)

随着公告OK !!!

notification = new NotificationCompat.Builder(context)
Run Code Online (Sandbox Code Playgroud)

这是一个例子:

在此输入图像描述您可以设置代码

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);
Run Code Online (Sandbox Code Playgroud)

要么

 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());
    }
Run Code Online (Sandbox Code Playgroud)