Firebase通知未显示接收时间问题排查(附实现代码)
解决Firebase通知不显示接收时间(小时)的问题
嘿哥们,我来帮你搞定这个Firebase通知不显示时间的问题!Android通知默认是会自动展示接收时间的,大概率是你的代码里不小心把时间显示给关了,或者通知渠道的配置有问题,咱们一步步排查:
1. 检查是否主动隐藏了通知时间
在构建NotificationCompat.Builder的时候,如果调用了setShowWhen(false),通知就会隐藏时间。你得确保没加这个方法,或者显式设置成setShowWhen(true)(其实默认就是true,但写出来更保险)。
举个正确的Builder示例:
NotificationCompat.Builder builder = new NotificationCompat.Builder(mCtx, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification_icon) .setContentTitle(title) .setContentText(message) .setStyle(bigPictureStyle) .setContentIntent(resultPendingIntent) .setPriority(NotificationCompat.PRIORITY_HIGH) .setShowWhen(true); // 确保开启时间显示
2. 检查通知渠道的重要性配置
从Android O(API 26)开始,所有通知必须归属一个渠道。如果渠道的重要性设置太低(比如IMPORTANCE_LOW及以下),系统会隐藏时间这类细节。你需要把渠道重要性至少设为IMPORTANCE_DEFAULT:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "通知渠道名称", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("用于展示Firebase推送通知"); NotificationManager notificationManager = mCtx.getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); }
3. 自定义布局需包含时间控件
如果你给通知加了自定义布局(比如用setCustomContentView/setCustomBigContentView),那得在布局里手动添加系统默认的时间控件,系统会自动填充时间:
<TextView android:id="@android:id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.Notification.Time" />
4. 排查Firebase消息的时间配置
最后可以检查下Firebase推送的payload,有没有设置timestamp字段?不过一般Firebase会自动处理发送时间,这个情况比较少见,先优先排查前面的代码问题。
按上面的步骤检查完,应该就能让通知正常显示小时时间啦!
内容的提问来源于stack exchange,提问作者Cris
相关产品推荐
相关产品推荐

