Android - 大图通知不显示,具体取决于图像大小
Android - Big picture notification not showing depending on image size
我正在尝试在 Android 上实现大图片通知,我发现似乎存在一些未记录的图像大小限制,这使得我的通知无法正常工作,具体取决于我使用的图像大小。
例如,如果我在 drawable-xxxhdpi
中放置一个 1024x512
图像(对于低密度设备应该适当缩放),则通知永远不会显示。如果我放一张 512x256
图片,它每次都能完美显示。这两个图像都比大图通知应该支持的 256 dp
高度小得多。
我已逐步完成代码,一切都按预期工作。在这两种情况下,通知都是正确创建和安排的(我没有收到任何错误或异常),它们只是永远不会出现。通知是在应用程序发送到后台时创建的,出于测试目的,我将它们设置为在几秒钟内触发。
我尝试了很多图像尺寸和宽高比,并缩小了范围,但不同设备的数字似乎不同。
有什么地方可以获取有关这些限制的信息?(请参阅编辑)。是否有可能我应该做些什么来确保无论图像大小如何都能触发我的通知?
这是我正在使用的代码。对不起,如果它看起来有点奇怪,这是一个很大的代码库,我从几个函数中复制并粘贴了代码。
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setContentTitle(title);
notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
notificationBuilder.setWhen(0);
notificationBuilder.setContentText(content);
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.ic_lollipop_noti);
notificationBuilder.setLargeIcon(notificationLargeIconBitmap);
notificationBuilder.setSmallIcon(R.drawable.ic_lollipop_statbar);
notificationBuilder.setColor(0xffe20b00);
Bitmap bigPicture = BitmapFactory.decodeResource(activity.getResources(), R.drawable.notif_image);
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.setBigContentTitle(p_title);
style.setSummaryText(p_content);
style.bigPicture(bigPicture);
notificationBuilder.setStyle(style);
Intent resultIntent = new Intent(context, Launcher.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
notificationBuilder.setContentIntent(pendingIntent);
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, LocalNotificationEmitter.class);
intent.setAction(NOTIFICATION);
intent.putExtra(LocalNotificationEmitter.NOTIFICATION_TAG, id);
intent.setData(Uri.parse(id));
intent.putExtra(LocalNotificationEmitter.NOTIFICATION, p_notification);
PendingIntent mAlarmIntent = PendingIntent.getBroadcast(contex, 0, intent,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
Long delay = Long.valueOf(delay) * 1000;
long futureInMillis = System.currentTimeMillis() + delay;
mAlarmManager.set(AlarmManager.RTC, futureInMillis, mAlarmIntent);
编辑:我制作了一个简单的应用程序来测试大图片通知,并且该应用程序可以处理任何图像尺寸。很明显,我们在主应用程序上使用的代码有问题。有人看到我发布的代码有什么问题吗?我会继续寻找...
如果这可以帮助任何人,我发现将通知创建移动到 LocalNotificationEmitter
class' onReceive
方法而不是将其捆绑到意图中使得它每次都工作。
我正在尝试在 Android 上实现大图片通知,我发现似乎存在一些未记录的图像大小限制,这使得我的通知无法正常工作,具体取决于我使用的图像大小。
例如,如果我在 drawable-xxxhdpi
中放置一个 1024x512
图像(对于低密度设备应该适当缩放),则通知永远不会显示。如果我放一张 512x256
图片,它每次都能完美显示。这两个图像都比大图通知应该支持的 256 dp
高度小得多。
我已逐步完成代码,一切都按预期工作。在这两种情况下,通知都是正确创建和安排的(我没有收到任何错误或异常),它们只是永远不会出现。通知是在应用程序发送到后台时创建的,出于测试目的,我将它们设置为在几秒钟内触发。
我尝试了很多图像尺寸和宽高比,并缩小了范围,但不同设备的数字似乎不同。
有什么地方可以获取有关这些限制的信息?(请参阅编辑)。是否有可能我应该做些什么来确保无论图像大小如何都能触发我的通知?
这是我正在使用的代码。对不起,如果它看起来有点奇怪,这是一个很大的代码库,我从几个函数中复制并粘贴了代码。
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setContentTitle(title);
notificationBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
notificationBuilder.setWhen(0);
notificationBuilder.setContentText(content);
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.ic_lollipop_noti);
notificationBuilder.setLargeIcon(notificationLargeIconBitmap);
notificationBuilder.setSmallIcon(R.drawable.ic_lollipop_statbar);
notificationBuilder.setColor(0xffe20b00);
Bitmap bigPicture = BitmapFactory.decodeResource(activity.getResources(), R.drawable.notif_image);
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
style.setBigContentTitle(p_title);
style.setSummaryText(p_content);
style.bigPicture(bigPicture);
notificationBuilder.setStyle(style);
Intent resultIntent = new Intent(context, Launcher.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
notificationBuilder.setContentIntent(pendingIntent);
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, LocalNotificationEmitter.class);
intent.setAction(NOTIFICATION);
intent.putExtra(LocalNotificationEmitter.NOTIFICATION_TAG, id);
intent.setData(Uri.parse(id));
intent.putExtra(LocalNotificationEmitter.NOTIFICATION, p_notification);
PendingIntent mAlarmIntent = PendingIntent.getBroadcast(contex, 0, intent,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
Long delay = Long.valueOf(delay) * 1000;
long futureInMillis = System.currentTimeMillis() + delay;
mAlarmManager.set(AlarmManager.RTC, futureInMillis, mAlarmIntent);
编辑:我制作了一个简单的应用程序来测试大图片通知,并且该应用程序可以处理任何图像尺寸。很明显,我们在主应用程序上使用的代码有问题。有人看到我发布的代码有什么问题吗?我会继续寻找...
如果这可以帮助任何人,我发现将通知创建移动到 LocalNotificationEmitter
class' onReceive
方法而不是将其捆绑到意图中使得它每次都工作。