用途:指导在 CometChat iOS 应用中接入推送通知,包括 APNs 配置、令牌注册与通知处理。
前提:需要 Apple 开发者账号、CometChat 后台权限,且推送需在真机测试,模拟器不支持。
主要能力:
- APNs 配置:在 Apple 开发者门户创建启用 APNs 的 .p8 密钥(仅可下载一次),记录 Key ID 与 Team ID,并在 CometChat 后台 Notifications → Push Notifications 的 iOS 标签上传密钥、填写 ID 与选择环境。
- Xcode 配置:添加 Push Notifications 能力,开启 Background Modes 的 Remote notifications 与 Voice over IP,并在 Info.plist 添加 UIBackgroundModes 的 remote-notification 与 voip。
- 令牌注册:使用 CometChatNotifications.registerPushToken(pushToken:platform:providerId:),平台枚举为 .APNS_IOS_DEVICE、.APNS_IOS_VOIP 或 .FCM_IOS;providerId 传 nil 使用默认。旧版 CometChat.registerTokenForPushNotification 已过时。
- 生命周期:令牌应在登录成功后注册,登出前调用 unregisterPushToken 注销,否则设备会继续收到上一用户推送。
- 通知处理:通过 didReceiveRemoteNotification 与 UNUserNotificationCenterDelegate 解析 message 数据,按 type(chat/call)分发并跳转会话。
- VoIP 推送:使用 PushKit 的 PKPushRegistry 注册 .voIP,在 didUpdate pushCredentials 中以 .APNS_IOS_VOIP 注册令牌,收到来电推送后建议配合 CallKit 展示。
- 通知服务扩展:可创建 Notification Service Extension 定制标题、正文并下载图片附件。
- 角标:通过 UIApplication.shared.applicationIconBadgeNumber 设置或清除未读数。
限制:推送在模拟器不可用;.p8 密钥仅能下载一次;需在依赖安装后核对符号,官方文档见 https://www.cometchat.com/docs/notifications/overview,也可通过 claude mcp add --transport http cometchat-docs https://www.cometchat.com/docs/mcp 接入文档 MCP。