You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS真机USB调试下Nativescript+Vue应用无法获取FCM Token求助

解决iOS端NativeScript Vue无法获取FCM Token的问题

核心问题分析

报错APNS device token not set before retrieving FCM Token说明FCM在尝试生成Token时,APNS的设备Token还未成功获取,导致FCM Token无法绑定APNS推送通道,最终无法生成可用的FCM Token。


具体解决步骤

1. 确认APNS配置完全合规

  • 苹果开发者后台:检查App ID已开启Push Notifications权限,且上传了对应环境(开发/生产)的APNS证书。
  • Xcode项目配置:在Signing & Capabilities中添加Push Notifications能力,同时在Background Modes里勾选Remote notifications。

2. 调整代码逻辑:等待APNS Token生成后再获取FCM Token

当前代码仅触发了远程消息注册,但未监听APNS Token的回调事件。需先监听APNS Token的获取通知,再在回调中请求FCM Token:

import { firebase } from '@nativescript/firebase-core'
import '@nativescript/firebase-messaging'
import { messaging } from '@nativescript/firebase-messaging'

async function requestUserPermission() {
  const authStatus = await firebase()
    .messaging()
    .requestPermission({
      ios: {
        alert: true,
        badge: true,
        sound: true
      }
    })

  const enabled =
    authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
    authStatus === messaging.AuthorizationStatus.PROVISIONAL

  if (enabled) {
    console.log('Authorization status:', authStatus)
    // 监听APNS Token获取事件
    firebase().messaging().onApnsTokenReceived((token) => {
      console.log('APNS Token received:', token)
      // 此时再获取FCM Token
      getFCMToken()
    })
    await firebase().messaging().registerDeviceForRemoteMessages()
  }
}

async function getFCMToken() {
  try {
    const token = await firebase().messaging().getToken()
    console.log('FCM Token obtained:', token)
    // 可在此处将Token上传至你的业务服务器
  } catch (err) {
    console.error('Error getting FCM Token:', err)
  }
}

3. 检查Firebase配置文件有效性

验证iOS项目中的GoogleService-Info.plist:

  • APNS_ENVIRONMENT字段需与当前测试环境匹配(开发环境填development,生产环境填production)。
  • 确认文件已通过Xcode导入项目,且在Build Phases的Copy Bundle Resources列表中存在。

4. 清理项目并重新构建

  • 执行ns clean清理NativeScript项目缓存。
  • 删除Xcode的Derived Data:打开Xcode → 点击顶部菜单Xcode→Preferences→Locations→ 点击Derived Data旁的箭头,删除对应项目的文件夹。
  • 卸载设备上的App,重新执行ns run ios --device安装测试。

5. 验证设备网络环境

确保测试设备能正常访问外网(Firebase服务需要网络支持),且未开启飞行模式、网络状态稳定。


内容的提问来源于stack exchange,提问作者Neiluj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 10:50:22