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

4

我正在尝试在我的通知中同时使用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);

我该如何同时使用这两个元素?我想要在图片旁展示带有换行的文本!


这个话题有任何更新吗?使用BigPictureStyle和BigTextStyle... - Ashish Sajwan
1
是的,这很容易。创建您自己的通知布局! 您可以设计您的XML并将其添加到通知中。 更多详情请参见: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomNotification - Daniel Sagayaraj
1个回答

3

抱歉回复晚了,实际上我也遇到了同样的问题并找到了解决方法,因此我认为它可以帮助其他用户。

由于我们不能同时使用BigTextStyleBigPictureStyle方法来构建NotificationCompat.Builder,因此我们需要创建自定义视图CustomView.

我们可以使用NotificationCompat.BuildersetCustomBigContentView(RemoteViews)方法,创建我们自己的视图以显示大图和大文本。

请查看以下代码:

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

以下是我们从setCustomBigContentView()方法中调用的RemoteViews。
 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;
    }

我已经创建了类似于自定义通知示例的自定义通知。


这是一个很棒的解决方案。 你如何在服务类中从资源中获取布局文件? - Dhara Vamja
我刚刚使用RemoteViews进行了解释。 - Ravindra Kushwaha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接