Implement expand and collapse notification android

后端 未结 5 1151
余生分开走
余生分开走 2021-02-01 15:44

I need to implement expand and collapse notification in status bar for android 4.0 and above version. I have search on google for this but didn\'t getting any code solution for

5条回答
  •  耶瑟儿~
    2021-02-01 16:43

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

    You change

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

    along with the announcement OK !!!

    notification = new NotificationCompat.Builder(context)
    

    Here is an example:

    You can set Code

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

    or

     private void sendNotification(RemoteMessage.Notification notification, Map 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());
        }
    

提交回复
热议问题