android - 如何在 setStyle Notification 中同时使用 BigTextStyle 和 BigPictureStyle?

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

我怎样才能同时使用两者?我想与图像一起显示文本(带换行符)!

最佳答案

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

因为我们不能同时使用 NotificationCompat.BuilderBigTextStyleBigPictureStyle 方法,所以我们可以创建 CustomView.

我们可以使用 NotificationCompat.BuildersetCustomBigContentView(RemoteViews) 方法并创建我们自己的 View 来显示带有大文本的大图像。

请检查以下代码:-

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

我已经创建了类似的自定义通知 Custom Notification example

关于android - 如何在 setStyle Notification 中同时使用 BigTextStyle 和 BigPictureStyle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30347842/

相关文章:

android - 使用 FileWriter 在开始时写入文本

android - 要求用户在没有 GoogleAPI 或 Google Play 服务的情况下在 android 中启用 GPS 定位

android - 用户必须购买硬币才能在我的 Android 应用程序中调用某人。对于我的场景,Google Pay 或 Google In-App Billing 哪个更好?

android - GCM : onMessage() from GCMIntentService is called very few times

ios - 自定义通知声音在使用 cordova 的 iOS 应用程序中不起作用

ios - 如何在 IOS 12 中用新收到的通知(包含相同的通知 ID)替换推送通知

android - 在哪里获取 GCM 注册 ID?

android - 如何从android中的 native 电话簿获取联系人

java - Build project containing lib project 包含 lib project with ant

android - 如何知道用户是否打开了应用程序