Pro 免费试用 15 天 · 无需信用卡立即注册 →

移动端 SDK

推送通知

STINGIM 移动端 SDK 自 1.0.0-rc.4 起支持推送通知(FCM / APNs):控制台凭证、Android 与 iOS 接入、常见问题。

Android 和 iOS SDK 自 1.0.0-rc.4 起支持推送通知。App 在后台或已关闭时,客服或 AI 智能体回复后,STINGIM 会向访客手机发送系统通知;点击通知即可打开对应会话。

需要两步:App 把设备 token 交给 SDK,并在控制台配置 FCM / APNs 凭证。未配置凭证时 token 仍会登记,但不会发送通知。

工作方式

AndroidiOS
推送服务Firebase Cloud Messaging(FCM)APNs(基于 token 的 .p8 密钥);已集成 Firebase 的 App 也可用 FCM
登记 tokenStingimPush.setPushToken(context, fcmToken)Stingim.shared.registerPushToken(deviceToken) 或 registerFCMToken(fcmToken)
展示 / 打开通知StingimPush.handleRemoteMessage、StingimPush.handleOpenIntentStingimPush.presentationOptions(for:)、StingimPush.handle(response:)、StingimPushUI
控制台凭证FCM 服务账号 JSONAPNs .p8 密钥(Key ID、Team ID、Bundle ID)或 FCM 服务账号 JSON
  • SDK 会保存 token 并为当前访客上报;登录、切换身份和 token 恢复后会自动重新上报,接口幂等。
  • disconnect() 会在清除访客 token 之前删除本设备的推送注册。
  • 访客在线(已连接)时不发送推送,消息已实时送达;投递日志中显示为 已跳过(Skipped)。登记了 token 时,App 进入后台不久后 SDK 会断开连接,回到前台时补齐错过的消息。
  • 聊天界面可见时不弹出通知。

1. 在控制台配置凭证

  1. 打开 设置 → 渠道 → App SDK,在你的 App 上点击 推送通知。
  2. 上传凭证: - FCM(Android 必需):在 Firebase 控制台打开 项目设置 → 服务账号 → 生成新的私钥,上传下载的 JSON 文件。不要使用 google-services.json。 - APNs(iOS):在 Apple Developer 打开 Certificates, IDs & Profiles → Keys → +,勾选 Apple Push Notifications service (APNs) 并下载 .p8 密钥。上传时填写 Key ID、Team ID 和 App 的 Bundle ID,并选择 Sandbox(Xcode 直接运行的包)或 Production(TestFlight / App Store)。
  3. 打开 发送推送通知。可按需关闭 显示消息预览,关闭后通知只显示“您有一条新消息”。
  4. 用 测试推送 填入 App 日志中的设备 token 检查凭证,在 投递日志 中查看最近的发送结果。

密钥加密保存且不会再次显示,控制台只显示指纹。

2. Android(FCM)

SDK 不依赖 Firebase:在你的 App 中引入 firebase-messaging 和 google-services.json,再把 token 和消息转交给 StingimPush。

Java
public class MyMessagingService extends FirebaseMessagingService {
    @Override
    public void onNewToken(@NonNull String token) {
        StingimPush.setPushToken(this, token);
    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {
        RemoteMessage.Notification n = message.getNotification();
        if (StingimPush.handleRemoteMessage(this, message.getData(),
                n != null ? n.getTitle() : null, n != null ? n.getBody() : null)) {
            return; // STINGIM 推送已处理
        }
        // 你自己的推送
    }
}

在启动页 Activity 的 onCreate(savedInstanceState == null 时)调用 StingimPush.handleOpenIntent(this, getIntent()),在 onNewIntent 中调用 StingimPush.handleOpenIntent(this, intent),点击通知即可打开聊天。Android 13+ 需申请 POST_NOTIFICATIONS 权限。用户在 App 内关闭通知时调用 StingimPush.clearPushToken(context)。

3. iOS(APNs)

在 Xcode 中开启 Push Notifications 能力,申请通知权限,并把设备 token 交给 SDK:

Swift
func application(_ application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Stingim.shared.registerPushToken(deviceToken)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    if StingimPush.isStingimNotification(notification.request.content.userInfo) {
        completionHandler(StingimPush.presentationOptions(for: notification))
    } else {
        completionHandler([.banner, .list, .sound])
    }
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    _ = StingimPush.handle(response: response)
    completionHandler()
}

registerPushToken 可以在 Stingim.shared.start(...) 之前调用。使用内置界面时,StingimPushUI.installDefaultOpenHandler(title:) 会在点击通知后打开聊天界面。SDK 会自动上报 APNs 环境(DEBUG 构建为 sandbox,其余为 production),可通过 StingimConfig.apnsEnvironment 覆盖。已集成 Firebase 的 App 也可以改为调用 Stingim.shared.registerFCMToken(fcmToken) 并上传 FCM 凭证。unregisterPushToken() 可关闭本设备的推送。

常见问题

  • 收不到通知:确认控制台中该 App 的推送状态为 已启用(已上传凭证并打开“发送推送通知”),并且对同一 token 的测试发送成功。
  • BadDeviceToken(APNs):环境与构建不匹配——Xcode 构建用 Sandbox,TestFlight / App Store 用 Production。
  • DeviceTokenNotForTopic(APNs):控制台中的 Bundle ID 与 App 不一致。
  • 投递日志中的 已跳过(Skipped) 属正常情况:访客当时在线,已在 App 内收到消息。

完整的推送 API 请参阅更新日志。

发现错误?请发邮件至 [email protected]