如何用Notification.setStyle创建类WhatsApp/Telegram样式的Android通知
实现类WhatsApp/Telegram样式的Android聊天通知
需求
不用RemoteViews,通过Notification.MessagingStyle或系统原生样式,实现类似WhatsApp、Telegram的聊天通知(带发送者头像、会话消息的形态)。
现有尝试的问题
之前尝试用Notification.CallStyle实现,代码如下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { Person.Builder personBuilder = new Person.Builder(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.angry_bird); Icon icon = Icon.createWithBitmap(bitmap); personBuilder.setIcon(icon); personBuilder.setName("It's Me"); personBuilder.setImportant(true); Person person = personBuilder.build(); Notification.CallStyle style = Notification.CallStyle.forOngoingCall(person,pendingIntent); notificationBuilder.setStyle(style); notificationBuilder.setContentText("Hello"); }
但这种方式会默认显示不需要的挂断按钮,且无法自定义该按钮的文本、图标及颜色,不符合普通聊天通知的需求。
正确方案:用MessagingStyle实现
MessagingStyle是Android官方为聊天场景量身打造的通知样式,完全适配WhatsApp/Telegram这类聊天APP的通知形态,无需自定义RemoteViews,还支持多消息展示、快速回复等功能。
完整实现代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 构建消息发送者信息 Person sender = new Person.Builder() .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.angry_bird))) .setName("It's Me") .build(); // 创建单条消息 Notification.MessagingStyle.Message chatMessage = new Notification.MessagingStyle.Message( "Hello", // 消息内容 System.currentTimeMillis(), // 消息发送时间 sender // 发送者 ); // 构建MessagingStyle,支持添加多条消息 Notification.MessagingStyle messagingStyle = new Notification.MessagingStyle(sender) .addMessage(chatMessage) // 添加单条消息,可多次调用添加历史消息 .setConversationTitle("好友会话"); // 设置会话标题 // 构建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CHAT_NOTIFY_CHANNEL") .setSmallIcon(R.drawable.ic_notify_small) // 必须设置的小图标 .setStyle(messagingStyle) .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_MESSAGE); // 处理Android O及以上的通知通道 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( "CHAT_NOTIFY_CHANNEL", "聊天通知", NotificationManager.IMPORTANCE_HIGH ); notificationManager.createNotificationChannel(channel); } // 发送通知 notificationManager.notify(1001, builder.build()); }
关键说明
- Person对象:用来定义发送者的头像、名称,对应通知里的圆形头像展示。
- MessagingStyle.Message:每条消息的实体,包含内容、时间、发送者,多次调用
addMessage()可以展示多条历史聊天记录。 - 会话标题:通过
setConversationTitle()设置,对应通知顶部的会话名称。 - 通知通道:Android 8.0及以上必须创建,否则通知无法正常显示。
可选:添加快速回复功能
如果需要像WhatsApp那样的快速回复按钮,可以添加如下代码:
// 创建回复的PendingIntent,指向处理回复的组件(如BroadcastReceiver) PendingIntent replyIntent = PendingIntent.getBroadcast( this, 0, new Intent(this, ReplyReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE ); // 构建回复动作 NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder( R.drawable.ic_reply, "回复", replyIntent ).setAllowGeneratedReplies(true) // 允许系统生成智能回复建议 .build(); // 添加到通知 builder builder.addAction(replyAction);
内容的提问来源于stack exchange,提问作者IronFist
相关产品推荐
相关产品推荐

