如何在setStyle通知中使用BigTextStyle和BigPictureStyle?

时间:2015-05-20 11:05:06

标签: android push-notification

我试图在我的通知中同时使用BigTextStyle和BigPictureStyle。但是setStyle只接受一种样式。

我的代码:

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

我怎样才能同时使用?我想显示文字(带换行符)和图像!

1 个答案:

答案 0 :(得分:2)

很抱歉迟到的回复..实际上我也遇到了同样的问题并得到了解决方案,所以我认为它可以帮助其他用户。

由于我们无法使用BigTextStyle的{​​{1}}和BigPictureStyle方法,因此我们可以创建 NotificationCompat.Builder

我们可以使用CustomView的{​​{1}}方法创建我们自己的视图,以显示带有大文字的大图片。

请检查以下代码: -

setCustomBigContentView(RemoteViews)

以下是我们从NotificationCompat.Builder方法

调用的RemoteView
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());

我已经创建了类似Custom Notification example

的自定义通知