@fivesheepco/cloudflare-apns2

中文文档
13.0.0发布于 2025/01/04MITTypeScriptES Module近 30 天下载 3.9万

Client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens.

原始使用文档
文档目录6 节
  1. Fork Notice
  2. Create Client
  3. Sending Notifications
  4. Error Handling
  5. Environments
  6. Requirements

APNS2 for Cloudflare Workers

npm version jsr version

Client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens.

Fork Notice

This is a fork of the original apns2 package with support for Cloudflare Workers. The original package has multiple dependencies that are incompatible with the Cloudflare Workers runtime. This fork removes those dependencies and replaces them with native Cloudflare Workers APIs.


Create Client

Create an APNS client using a signing key:

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
})

Sending Notifications

Basic

Send a basic notification with message:

import { Notification } from 'apns2'

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

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

Send a basic notification with message and options:

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)
}

Silent

Send a silent notification using content-available key:

import { SilentNotification } from 'apns2'

const sn = new SilentNotification(deviceToken)

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

Note: Apple recommends that no options other than the content-available flag be sent in order for a notification to truly be silent and wake up your app in the background. Therefore this class does not accept any additional options in the constructor.

Many

Send multiple notifications concurrently:

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)
}

Advanced

For complete control over the push notification packet use the base Notification class:

import { Notification } from 'apns2'

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

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

Available options can be found at APNS Payload Options

Error Handling

All errors are defined in ./lib/errors.js and come directly from APNS Table 4

You can easily listen for these errors by attaching an error handler to the APNS client:

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)
})

Environments

By default the APNS client connects to the production push notification server. This is identical to passing in the options:

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

To connect to the development push notification server, pass the options:

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

Requirements

apns2 requires Node.js v16 or later

版本与发布记录

@fivesheepco/cloudflare-apns2 共发布 2 个版本,首次发布于 2025/01/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
13.0.0latest2025/01/041.9 年前
12.1.0-beta.22025/01/041.9 年前

关于 @fivesheepco/cloudflare-apns2 的常见问题

@fivesheepco/cloudflare-apns2 是什么?

Client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens.

如何安装 @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 和提交记录判断。