Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

android - How to use both BigTextStyle and BigPictureStyle in setStyle Notification?

I am trying to use both BigTextStyle and BigPictureStyle in my notification.But setStyle accepts only one style.

My code:

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
mBuilder.setVisibility(1);
mBuilder.setSmallIcon(R.drawable.app_icon1);
mBuilder.setContentTitle(title.toString());
bigTextStyle.bigText(description.toString());
//mBuilder.setSubText(bigText.toString());
if (bigImage != null && !bigImage.toString().equals("")) {
    mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(ImageUtil.getBitmapFromUrl(bigImage.toString())));
}
mBuilder.setStyle(bigTextStyle);
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setContentIntent(contentIntent);

How can i use both ?. I want to show text(with line breaks) along with the image!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sorry for late reply..Actually i was also faced the same problem and got the solution, so i am thinking that it can help for other user.

As we can NOT use the both BigTextStyle and BigPictureStyle method of the NotificationCompat.Builder than we can create the CustomView.

We can use the setCustomBigContentView(RemoteViews) method of NotificationCompat.Builder and create our own view to show the Big Image with Big text.

Please check the below code for it:-

PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), i,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle("YOUR_APP_NAME");
        notificationBuilder.setContentText(body);
        notificationBuilder.setTicker("YOUR_APP_NAME");
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(defaultSoundUri);
        notificationBuilder.setCustomBigContentView(remoteView("YOUR_MESSAGE_TO_SHOW"));///IT IS THE MAIN METHOD WHICH WE USE TO INFLATE OR CREATE THE CUSTOM VIEW
        notificationBuilder.setSmallIcon(getNotificationIcon(notificationBuilder));
        notificationBuilder.setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());

Below is the RemoteViews which we have called from our setCustomBigContentView() method

 private RemoteViews remoteView(String message)
    {
        RemoteViews views;
        views = new RemoteViews(getPackageName(), R.layout.YOUR_LAYOUT_HERE);
        views.setImageViewBitmap(R.id.YOUR_BIG_IMAGE_ID_FROM_LAYOUT, bitmap);
        views.setImageViewBitmap(R.id.YOUR_APP_ID_FROM_LAYOUT, BitmapFactory.decodeResource(getResources(), R.drawable.APP_ICON_OF_YOUR_APP));
        views.setTextViewText(R.id.YOUR_BIG_TEXTVIEW_ID_FROM_LAYOUT, message);
        return views;
    }

I have created the custom notification like itCustom Notification example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...