You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中使用zo0r/react-native-push-notification能否展示推送消息图片?

没问题!zo0r/react-native-push-notification完全支持在推送消息里展示大图和小图,不过iOS和Android的实现逻辑有点不一样,我给你拆解清楚:

实现方案分平台说明

iOS平台:依赖通知扩展服务

iOS默认的推送通知只支持文本内容,要展示图片必须借助Notification Service Extension(通知扩展服务),它能在通知显示前修改内容并添加媒体附件。

具体步骤:

  1. 打开Xcode,给你的RN项目新增一个「Notification Service Extension」目标(记得和主APP的Bundle ID关联,版本号保持一致)。
  2. 在扩展的NotificationService.swift文件中,编写逻辑处理推送携带的图片URL:下载图片到临时目录,然后将其作为附件添加到通知内容里。
  3. 后端推送的Payload必须包含mutable-content: 1(告诉系统要调用扩展处理),同时携带图片URL(可以放在自定义字段里)。示例Payload如下:
    {
      "aps": {
        "alert": {
          "title": "收到新推送",
          "body": "快来看看这张图~"
        },
        "mutable-content": 1
      },
      "imageUrl": "https://你的图片地址.jpg"
    }
    
  4. 扩展核心代码示例(Swift):
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        if let bestAttemptContent = bestAttemptContent, let imageUrlString = bestAttemptContent.userInfo["imageUrl"] as? String, let imageUrl = URL(string: imageUrlString) {
            URLSession.shared.downloadTask(with: imageUrl) { (tempUrl, response, error) in
                if let tempUrl = tempUrl {
                    let fileManager = FileManager.default
                    let tempDir = NSTemporaryDirectory()
                    let fileName = imageUrl.lastPathComponent
                    let destUrl = URL(fileURLWithPath: tempDir).appendingPathComponent(fileName)
                    do {
                        try fileManager.moveItem(at: tempUrl, to: destUrl)
                        let attachment = try UNNotificationAttachment(identifier: "image", url: destUrl)
                        bestAttemptContent.attachments = [attachment]
                    } catch {
                        print("处理图片失败: \(error)")
                    }
                }
                self.contentHandler(bestAttemptContent)
            }.resume()
        }
    }
    

Android平台:直接通过自定义字段配置

Android的实现更直接,react-native-push-notification本身支持BigPictureStyle和小图标配置,只需在推送Payload里携带对应的图片URL,然后在通知回调中处理即可。

具体步骤:

  1. 确保你的Android项目已经完成基础配置:比如添加FCM依赖、配置通知渠道、声明必要权限(如INTERNET用于加载网络图片)。
  2. 后端推送的Payload中,将图片URL放在data字段里(FCM的notification字段会被系统直接处理,自定义字段建议放在data中)。
  3. 在RN代码的onNotification回调里,判断是否有图片URL,然后调用localNotification配置大图样式:
    import PushNotification from 'react-native-push-notification';
    
    PushNotification.configure({
      onNotification: (notification) => {
        // 处理远程推送的图片
        if (notification.data?.bigPictureUrl) {
          PushNotification.localNotification({
            title: notification.title,
            message: notification.message,
            // 配置大图
            bigPictureUrl: notification.data.bigPictureUrl,
            // 配置小图标(需将图标放在android/app/src/main/res/mipmap目录下)
            smallIcon: "ic_notification",
            // 可选:设置大图样式的摘要文本
            bigLargeIcon: "ic_notification"
          });
        }
      },
      // 其他配置项...
    });
    
  4. 示例Android推送Payload(FCM格式):
    {
      "to": "设备令牌",
      "data": {
        "title": "新推送提醒",
        "message": "这是带大图的推送",
        "bigPictureUrl": "https://你的图片地址.jpg"
      }
    }
    
关键注意事项
  • iOS端:Notification Service Extension需要单独打包,且版本号必须和主APP一致;图片URL必须是HTTPS或者本地路径,HTTP的话需要在Info.plist中配置ATS例外。
  • Android端:小图标必须是白色透明背景的矢量图(符合Android设计规范),否则显示会异常;大图支持网络URL和本地文件路径。
  • 远程推送:后端发送的Payload必须包含对应的字段(iOS的mutable-content和imageUrl,Android的data.bigPictureUrl),否则无法触发图片展示逻辑。

内容的提问来源于stack exchange,提问作者AMS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:07:16