Swift Server Push Notification 配置

Swift Server Push Notification 配置
Photo by Jakub Żerdzicki / Unsplash

获取证书

在 Apple Developer 开发者账号 Certificates, Identifiers & Profiles 里选择 Keys。新增一个 key, configure 里选择 Sandbox & Production。下载该 p8 证书,并且保存好(只能下载一次)。

终端 cd 到证书所在路径,输入下面指令。

openssl pkcs8 -nocrypt -in AuthKey_XXXXXXXXX.p8 -out ~/Downloads/key.pem

cat key.pem

得到 PRIVATE KEY 字符串,复制好。

服务端配置

服务端有多种技术栈方案,包括 Java、NodeJS、Swift、Python 等。这里采用 Swift Server 方案。

Swift Server 整体使用 Vapor 框架,具体实现可以参考官方文档。对于 Apple Push Notification 的实现使用 APNSwift 模块。

APNSwift 初始化需要配置推送证书等信息。

static let deviceToken = ""
static let appBundleID = ""
static let privateKey = """
"""
static let keyIdentifier = ""
static let teamIdentifier = ""
    
let client = APNSClient(
    configuration: .init(
        authenticationMethod: .jwt(
            privateKey: try .init(pemRepresentation: privateKey),
            keyIdentifier: keyIdentifier,
            teamIdentifier: teamIdentifier
        ),
        environment: .development
    ),
    eventLoopGroupProvider: .createNew,
    responseDecoder: JSONDecoder(),
    requestEncoder: JSONEncoder()
)

// Shutdown the client when done
try await client.shutdown()
  • privateKey 就是上面 cat key.pem 得到的 PRIVATE KEY 字符串,包含 -----BEGIN PRIVATE KEY----------END PRIVATE KEY-----
  • teamIdentifier 是 Apple Developer 的团队 id。
  • keyIdentifier 是上面在 Apple Developer 生成推送 p8 证书时,Key 对应的 KEY ID
  • deviceToken 是移动端 iOS app 生成的 push device token
Key ID

客户端配置

iOS 项目需要设置 Push Notification 的 Capability。

Push Notifications Capability

然后在 app 启动后获取通知权限,这样才能拿到 push device token。

import UIKit

final class AppDelegate: UIResponder, UIApplicationDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 请求推送通知权限
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            if granted {
                // 注册 APNs
                DispatchQueue.main.async {
                    application.registerForRemoteNotifications()
                }
            } else {
                print("User denied push notifications")
            }
        }
        return true
    }

    // 处理推送通知注册成功后的设备 token
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("Device Token: \(deviceToken)")
        let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("Device Token String: \(deviceTokenString)")
        // 将 deviceToken 发送到你的服务器
    }

    // 处理推送通知注册失败
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to register for remote notifications: \(error)")
    }
}

拿到 deviceToken 后,将其发送给服务器存储。服务器发送推送时,需要指定 deviceToken 才能知道是对哪一台手机发送推送。

尝试发送简单推送

Server 调用下面方法,传入初始化的 APNSClient 实例。不出意外的话,手机就能收到推送了。

func sendSimpleAlert(with client: some APNSClientProtocol) async throws {
    try await client.sendAlertNotification(
        .init(
            alert: .init(
                title: .raw("Simple Alert"),
                subtitle: .raw("Subtitle"),
                body: .raw("Body"),
                launchImage: nil
            ),
            expiration: .immediately,
            priority: .immediately,
            topic: self.appBundleID,
            payload: EmptyPayload()
        ),
        deviceToken: self.deviceToken
    )
}

Read more

2025 年度总结

2025 年度总结

今天是 2026 年 1 月 1 号,又是新的一年。这个元旦没有安排出行任务,就在家里休息休息,或者出门溜达溜达。昨天休了一天全薪病假,做了体检,写了年终绩效总结,晚上干了一顿烤肉,没有时间写个人的年度总结。今天起早写写总结。 以下「今年」指 2025 年。 职业发展 算起来,我已经毕业工作四年多了。职业发展整体上还算稳定,没有碰到过糟心事,遇到的领导们也都对我关怀有加。今年又晋升一次,薪资迈上新的台阶。越往上升,越觉得离职业生涯的终点越近,逼迫自己赶紧找个靠谱稳定的副业,到 35 岁没人要的时候能养活自己。 最近两年 AI 大模型的崛起,提高了许多行业的可替代性。码农虽然不是首当其冲的,但危机感已经弥漫在各个论坛博客公共平台上面。没有人能准确预测到未来发展,但做好两手准备是很有必要的。码农不能再只低着头守着自己的键盘和屏幕,也要往外看,接触社会上的各种信息,打破信息壁垒。掌握的信息越多,出路就越多。

By Gray
联通 FTTR 宽带从路由器设置自动重启和穿墙功率

联通 FTTR 宽带从路由器设置自动重启和穿墙功率

几个月前把家里宽带换成了联通的千兆 FTTR 宽带,包含一主一从两个点位。配套光猫设备是华为的星光 F50 尊享版。 主点位放置在客厅茶几上,方便连接电视。从点位放在卧室门口,那里恰好有一个不耽误过路的小拐角可以放路由器。平常我们基本不在客厅活动,其他区域最近的 Wi-Fi 信号源是从路由器,因此我们大多数的设备连接的都是从路由器。从路由器的工作负荷很大。 从路由器个头小主路由器很多,散热不咋地。工作时间久了发热就容易发生数据包堵塞,丢包延迟高。需要把它电源拔掉重启。从宽带开通到现在,数据包堵塞影响网络的情况每个月会发生一次。有一次还影响了居家办公的视频会议。宽带维修师傅也给不出有效的法子,建议就是定期插拔从路由器电源。 从路由器和书房之间隔了两堵墙。信号到我书桌那个位置时,千兆网速已经衰减到只有 400-500Mbps 了,折损将近一半。叠加路由器发热的 debuff,书桌位置的网速最差的时候几乎和百兆宽带差不多。 我尝试过在光猫后台管理将路由器功率设置到「穿墙」模式,但没有任何作用。今天在后台研究了一番发现,原来我之前设置的功率是仅对主路由器生效,从路由器还是标准功率。要修

By Gray