# @fivesheepco/cloudflare-apns2 中文文档

规范地址：https://npmcn.com/package/%40fivesheepco/cloudflare-apns2/

版本：13\.0\.0
npm 元数据更新时间：2025-01-04T12:54:25.853Z
中文译文更新时间：2026-11-14T20:42:06.191Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40fivesheepco/cloudflare-apns2
原文对照：https://npmcn.com/package/%40fivesheepco/cloudflare-apns2/?lang=original
项目主页：https://github.com/FiveSheepCo/cloudflare-apns2#readme
源码仓库：https://github.com/FiveSheepCo/cloudflare-apns2

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：20.8 kB，17 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=20
- 运行依赖：1 个
- 首次发布：2025-01-03T16:35:19.278Z
- 最新版本发布：2025-01-04T08:54:25.701Z
- 近 30 天下载量：38759（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 0 个）

## 中文简介

使用新的HTTP/2协议与JSON网络代号连接到苹果的Push通知服务的客户端.

## 安装

```sh
npm install @fivesheepco/cloudflare-apns2
```

也可以使用：`pnpm add @fivesheepco/cloudflare-apns2`、`yarn add @fivesheepco/cloudflare-apns2`、`bun add @fivesheepco/cloudflare-apns2`

## README

# 云旗工人APNS2

[![npm version](https://badgers.space/npm/version/@fivesheepco/cloudflare-apns2)](https://npmjs.com/package/@fivesheepco/cloudflare-apns2) [![jsr version](https://jsr.io/badges/@fivesheepco/cloudflare-apns2)](https://jsr.io/@fivesheepco/cloudflare-apns2)

使用新的HTTP/2协议与JSON网络代号连接到苹果的Push通知服务的客户端.

## 分叉通知

这是原作的叉子 [apns2 (韩语)](https://github.com/AndrewBarba/apns2) 支持 Cloudflare 工人的软件包。 原包有多个依赖性,与Cloudflare工人运行时间不相容. 这种叉去除这些依赖,取而代之的是本地Cloudflare工人API.

* * *

## 创建客户端

使用签名密钥创建APNS客户端 :

```typescript
import { ApnsClient } from 'apns2'

const client = new ApnsClient({
  team: `TFLP87PW54`,
  keyId: `123ABC456`,
  signingKey: fs.readFileSync(`${__dirname}/path/to/auth.p8`),
  defaultTopic: `com.tablelist.Tablelist`,
  requestTimeout: 0, // optional, Default: 0 (without timeout)
  keepAlive: true, // optional, Default: 5000
})
```

## 发送通知

#### 基础

发送带信息的基本通知 :

```typescript
import { Notification } from 'apns2'

const bn = new Notification(deviceToken, { alert: 'Hello, World' })

try {
  await client.send(bn)
} catch (err) {
  console.error(err.reason)
}
```

发送带有信息和选项的基本通知:

```typescript
import { Notification } from 'apns2'

const bn = new Notification(deviceToken, {
  alert: 'Hello, World',
  badge: 4,
  data: {
    userId: user.getUserId
  }
})

try {
  await client.send(bn)
} catch (err) {
  console.error(err.reason)
}
```

#### 安静

使用 `content-available` 键 :

```typescript
import { SilentNotification } from 'apns2'

const sn = new SilentNotification(deviceToken)

try {
  await client.send(sn)
} catch (err) {
  console.error(err.reason)
}
```

说明: [苹果建议](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app#2980040) 选择 `content-available` 发去旗帜是为了让一个通知真正沉默,并在背景中唤醒您的应用程序. 因此,该类不接受构造器中的任何附加选项.

#### 许多

同时发送多个通知 :

```typescript
import { Notification } from 'apns2'

const notifications = [
  new Notification(deviceToken1, { alert: 'Hello, World' }),
  new Notification(deviceToken2, { alert: 'Hello, World' })
]

try {
  await client.sendMany(notifications)
} catch (err) {
  console.error(err.reason)
}
```

#### 高级

要完全控制驱动通知包,请使用底盘 `Notification` 类 :

```typescript
import { Notification } from 'apns2'

const notification = new Notification(deviceToken, {
  aps: { ... }
})

try {
  await client.send(notification)
} catch(err) {
  console.error(err.reason)
}
```

可用选项可见于 [APNS 有效载荷选项](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification#2943363)

## 处理错误

所有错误定义在 `./lib/errors.js` 直接从 [APNS 表4 数据](https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/handling_notification_responses_from_apns#3394535)

您可以在 APNS 客户端中附加一个错误处理器来方便地聆听这些错误 :

```typescript
import { Errors } from 'apns2'

// Listen for a specific error
client.on(Errors.badDeviceToken, (err) => {
  // Handle accordingly...
  // Perhaps delete token from your database
  console.error(err.reason, err.statusCode, err.notification.deviceToken)
})

// Listen for any error
client.on(Errors.error, (err) => {
  console.error(err.reason, err.statusCode, err.notification.deviceToken)
})
```

## 环境

默认情况下,APNS客户端会连接到生产推取通知服务器. 这与通过选项相同:

```typescript
const client = new ApnsClient({
  host: 'api.push.apple.com'
  ...
})
```

要连接到开发驱动通知服务器, 请通过选项 :

```typescript
const client = new ApnsClient({
  host: 'api.sandbox.push.apple.com'
  ...
})
```

## 所需资源

`apns2` 需要节点.js v16 或稍后

## 常见问题

### @fivesheepco/cloudflare-apns2 是什么？

使用新的HTTP/2协议与JSON网络代号连接到苹果的Push通知服务的客户端\.

### 如何安装 @fivesheepco/cloudflare-apns2？

在项目目录运行 npm install @fivesheepco/cloudflare-apns2，或使用 pnpm add / yarn add / bun add。

### @fivesheepco/cloudflare-apns2 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @fivesheepco/cloudflare-apns2 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @fivesheepco/cloudflare-apns2 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=20。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @fivesheepco/cloudflare-apns2 可以商用吗？

@fivesheepco/cloudflare-apns2 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @fivesheepco/cloudflare-apns2 的下载量有多少？

根据 npm 官方下载统计，@fivesheepco/cloudflare-apns2 在 2026/10/15 – 2026/11/13 这 30 天内被下载 38,759 次，日均约 1,292 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @fivesheepco/cloudflare-apns2 还在维护吗？

最新版本 13\.0\.0 发布于 2025/01/04（1\.9 年前）；至今共发布 2 个版本，其中过去 12 个月发布了 0 个；首次发布于 2025/01/04。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

