在某些 Android 设备中收到空白通知
Posted
技术标签:
【中文标题】在某些 Android 设备中收到空白通知【英文标题】:Getting blank notification in some Android device 【发布时间】:2017-08-21 04:57:19 【问题描述】:在某些设备中,我收到一个空白(白色)通知,如附加的屏幕截图。在某些设备中,它运行良好。请帮我解决这个问题。
Intent intent = new Intent(ctx, NotificationDetailActivity.class);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setTicker(getResources().getString(R.string.app_name));
// Sets the small icon for the ticker
builder.setSmallIcon(getNotificationIcon());
builder.setLargeIcon(result);
builder.setColor(getResources().getColor(R.color.colorPrimary));
builder.setContentTitle(title);
builder.setContentText(messageBody);
builder.setSound(defaultSoundUri);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
RemoteViews expandedView =
new RemoteViews(ctx.getPackageName(), R.layout.custom_notification);
if (Build.VERSION.SDK_INT >= 16)
// Inflate and set the layout for the expanded notification view
expandedView.setImageViewBitmap(R.id.imgBigImage, result);
notification.bigContentView = expandedView;
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Integer.parseInt(id), notification);
else
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(getNotificationIcon())
.setLargeIcon(result)
.setColor(getResources().getColor(R.color.colorPrimary))
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(id), notificationBuilder.build());
【问题讨论】:
Lolipop 及以上版本您遇到了问题。不是吗? 在此处添加您的通知代码.. 请添加您的代码 @NilamVaddoriya 我添加了代码。请检查它 @AnkitaGuna 出现空白通知的那个设备的安卓版本是什么? 【参考方案1】:试试这个方法setBigContentView
到NotificationCompat.Builder
,检查它是否有效:-
Intent intent = new Intent(ctx, NotificationDetailActivity.class);
intent.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Notification notification = builder.build();
RemoteViews expandedView =
new RemoteViews(ctx.getPackageName(), R.layout.custom_notification);
if (Build.VERSION.SDK_INT >= 16)
builder.setTicker(getResources().getString(R.string.app_name));
// Sets the small icon for the ticker
builder.setSmallIcon(getNotificationIcon());
builder.setLargeIcon(result);
builder.setColor(getResources().getColor(R.color.colorPrimary));
builder.setContentTitle(title);
builder.setContentText(messageBody);
builder.setSound(defaultSoundUri);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
// Inflate and set the layout for the expanded notification view
expandedView.setImageViewBitmap(R.id.imgBigImage, result);
// notification.bigContentView = expandedView;
builder.setCustomBigContentView(expandedView);
else
builder.setSmallIcon(getNotificationIcon());
builder.setLargeIcon(result);
builder.setColor(getResources().getColor(R.color.colorPrimary));
builder.setContentTitle(title);
builder.setContentText(messageBody);
builder.setAutoCancel(true);
builder.setSound(defaultSoundUri);
builder.setContentIntent(pendingIntent);
nm.notify(Integer.parseInt(id), builder.build());
【讨论】:
@AnkitaGuna 现在问题可能与expandedView
有关。所以先评论builder.setCustomBigContentView(expandedView);
这一行,试试看。
@AnkitaGuna 好的。然后找到并正确设置RemoteViews
将解决您的问题。
@NitinPatel 当我评论那行时它对我有用,但为什么呢?以上是关于在某些 Android 设备中收到空白通知的主要内容,如果未能解决你的问题,请参考以下文章