首页
/ Notifee 库中 Android 消息样式通知的深度解析与实现方案

Notifee 库中 Android 消息样式通知的深度解析与实现方案

2025-07-05 09:19:30作者:盛欣凯Ernestine

背景介绍

Notifee 是一个强大的 React Native 通知库,它提供了跨平台的通知功能支持。在 Android 平台上,Notifee 已经支持 MessagingStyle 通知样式,但开发者发现它无法像 iOS 上的通信通知那样完整显示。经过调研发现,要实现类似 iOS 通信通知的效果,需要设置 ShortcutInfo 并将其 longLived 选项设为 true。

技术实现方案

核心修改点

为了实现类似 iOS 通信通知的效果,需要对 Notifee 的核心代码进行以下修改:

  1. ShortcutInfo 支持:在 Android 通知中添加对 ShortcutInfo 的完整支持
  2. longLived 选项:确保 ShortcutInfo 可以设置为长期存活的
  3. 通知样式增强:完善 MessagingStyle 通知的显示效果

代码实现细节

在 NotificationManager.java 中,关键的修改是添加了一个 shortcutContinuation 任务,它负责处理 ShortcutInfo 的构建和设置:

Continuation<NotificationCompat.Builder, NotificationCompat.Builder> shortcutContinuation = task -> {
    NotificationCompat.Builder builder = task.getResult();
    if (androidModel.hasShortcutInfo()) {
        Bundle shortcutInfoBundle = androidModel.getShortcutInfo();
        String id = shortcutInfoBundle.getString("id");
        
        // 构建 ShortcutInfoCompat
        ShortcutInfoCompat.Builder shortcutInfoBuilder = new ShortcutInfoCompat.Builder(getApplicationContext(), id);
        
        // 设置图标
        if (shortcutInfoBundle.containsKey("icon")) {
            // 处理图标位图
        }
        
        // 设置标签
        if (shortcutInfoBundle.containsKey("shortLabel")) {
            shortcutInfoBuilder.setShortLabel(shortcutInfoBundle.getString("shortLabel"));
        }
        
        // 设置长期存活选项
        if (shortcutInfoBundle.containsKey("longLived")) {
            shortcutInfoBuilder.setLongLived(shortcutInfoBundle.getBoolean("longLived"));
        }
        
        // 设置点击动作
        if (shortcutInfoBundle.containsKey("action")) {
            // 构建点击意图
        }
        
        // 设置相关联系人
        if (shortcutInfoBundle.containsKey("persons")) {
            // 处理联系人列表
        }
        
        ShortcutInfoCompat shortcutInfo = shortcutInfoBuilder.build();
        ShortcutManagerCompat.pushDynamicShortcut(getApplicationContext(), shortcutInfo);
        builder.setShortcutInfo(shortcutInfo);
    }
    return builder;
};

使用示例

开发者可以通过以下配置来创建具有通信风格的 MessagingStyle 通知:

{
    channelId: "default",
    smallIcon: 'ic_notification',
    showTimestamp: true,
    category: 'msg',
    color: '#000000',
    shortcutInfo: {
        id: 'channel-id',
        shortLabel: '联系人姓名',
        icon: '图标URL',
        action: {
            title: '聊天',
            pressAction: {
                id: '导航动作ID'
            }
        },
        persons: [{
            id: '用户ID',
            name: '联系人姓名',
            icon: '联系人图标URL',
        }],
        longLived: true,
    },
    actions: [
      {
        title: '回复',
        pressAction: {
          id: '回复动作ID'
        },
        input: {
          choices: ['你好', '嗨'],
          placeholder: '输入消息...',
        },
      },
    ],
    style: {
        type: 3, // MessagingStyle
        person: {
            name: '我',
            icon: '我的头像',
        },
        messages: [{
            text: '消息内容',
            timestamp: Date.now(),
            person: {
                id: '用户ID',
                name: '联系人姓名',
                icon: '联系人头像URL',
            },
        }]
    }
}

技术要点解析

  1. ShortcutInfo 的重要性

    • 在 Android 上实现类似 iOS 通信通知的关键在于正确配置 ShortcutInfo
    • ShortcutInfo 允许通知与应用快捷方式集成,提供更丰富的交互体验
  2. longLived 选项的作用

    • 设置为 true 可以让快捷方式长期保留在系统中
    • 这对于频繁通信的场景特别重要,可以提升用户体验
  3. MessagingStyle 的增强

    • 结合 ShortcutInfo 和 MessagingStyle 可以实现更丰富的消息通知
    • 支持显示对话历史、联系人头像等元素
  4. 跨平台一致性

    • 通过这些修改,Android 上的通知体验可以更接近 iOS 的通信通知
    • 有助于保持应用在不同平台上的一致性

总结与展望

通过对 Notifee 库的 Android 实现进行增强,特别是完善 ShortcutInfo 和 MessagingStyle 的支持,可以显著提升通信类应用的通知体验。这种实现方式不仅使 Android 通知更接近 iOS 的通信通知效果,还为开发者提供了更灵活的通知定制能力。

未来可以考虑进一步优化以下方面:

  1. 更完善的快捷方式管理
  2. 通知分组和排序的增强
  3. 更丰富的消息交互选项
  4. 性能优化和内存管理

这些改进将使 Notifee 成为 React Native 生态中更强大的通知解决方案,特别是对于需要丰富通信功能的应用程序。

登录后查看全文
热门项目推荐

热门内容推荐

最新内容推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
176
262
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
863
511
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
182
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
259
300
kernelkernel
deepin linux kernel
C
22
5
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
596
57
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
398
371
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
332
1.08 K