通知内容标题和内容文本未显示
Posted
技术标签:
【中文标题】通知内容标题和内容文本未显示【英文标题】:Notification Content Title and Content Text are not shwoing 【发布时间】:2018-06-07 05:15:37 【问题描述】:我正在生成包含多行的本地通知。所有行都正常显示,但 contentTitle 和 contentText 没有显示。
这是我生成通知的代码:
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
int num = (int) System.currentTimeMillis();
PendingIntent pendingIntent = stackBuilder.getPendingIntent(num, PendingIntent.FLAG_UPDATE_CURRENT);
String id = "notification_channel_id";
CharSequence name = "Message_Notification";
Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setContentText("time")
.setAutoCancel(true)
.setGroupSummary(true)
.setGroup(GROUP_KEY)
.setContentIntent(pendingIntent)
.setChannelId(id);
if (subTitle != null)
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("Dose Reminder");
if (subTitle.contains("---"))
String[] splitStrings = subTitle.split("---");
for (int i = 0; i < splitStrings.length; i++)
inboxStyle.addLine(splitStrings[i]);
else
inboxStyle.addLine(subTitle);
// Moves the expanded layout object into the notification object.
notificationBuilder.setStyle(inboxStyle);
// Moves the expanded layout object into the notification object.
notificationBuilder.setStyle(inboxStyle);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
notificationBuilder.setColor(ContextCompat.getColor(context, R.color.colorAccent));
else
notificationBuilder.setLargeIcon(bm);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// The user.-visible description of the channel.
String description = "Notifications contains messages which was sent by dev team.";
int importance = NotificationManager.IMPORTANCE_MAX;
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
if (notificationManager != null)
notificationManager.createNotificationChannel(mChannel);
// Sets an ID for the notification
// Gets an instance of the NotificationManager service
final NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
if (mNotifyMgr != null)
mNotifyMgr.notify(num, notificationBuilder.build());
当通知到来时,“标题”和“时间”不显示。它在多行中显示“剂量提醒”和子标题。
我错过了什么吗?
【问题讨论】:
您可以使用inboxStyle.setSummaryText("Summary");
将摘要文本添加到您的收件箱样式吗?
@Sagar 有什么不同
存在一些问题,如果您不设置摘要,则通知设置为展开模式。添加摘要使用来修复它
@Sagar 让我试试这个
@Sagar 它不工作
【参考方案1】:
setBigContentTitle(CharSequence title)
覆盖模板大表单中的 ContentTitle。
https://developer.android.com/reference/android/app/Notification.InboxStyle
检查使用MessagingStyle部分
https://developer.android.com/training/notify-user/build-notification
JFYI 默认时间戳在推送通知中消失了,Android 7 (Nougat) 开始
【讨论】:
我尝试不设置 setBigContentTitle 然后当通知出现“标题”并且显示多行但未显示“时间”时。以上是关于通知内容标题和内容文本未显示的主要内容,如果未能解决你的问题,请参考以下文章
通知未显示 - 已设置 chennel 以及小图标、标题和文本