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

Android:如何在Firebase Functions中获取App卸载事件的设备ID

如何在Firebase Functions的应用卸载事件中正确获取设备ID?

你之前的代码错误在于直接尝试访问functions.analytics.DeviceInfo.deviceId——DeviceInfo是事件数据的类型定义,不是可以直接调用的静态属性。正确的做法是从触发函数的event参数中提取设备ID,具体修正如下:

  1. 先补全遗漏的firebase-functions模块引入(原代码缺少这一步,会导致API调用异常)
  2. 从event.data.deviceInfo中读取真实的设备ID值,这才是当前触发事件对应的设备信息实例

修正后的完整代码:

const admin = require("firebase-admin");
const functions = require("firebase-functions"); // 补全模块引入

admin.initializeApp();

const database = admin.firestore();

exports.appUninstall = functions.analytics.event("app_remove")
  .onLog((event) => {
  // 从事件数据中提取设备ID
  const deviceId = event.data.deviceInfo.deviceId;
  
  console.log("deviceId", deviceId);

  // 改用set+merge参数兼容文档存在/不存在的场景,避免update报错
  return database.doc(`userActivity/uninstalls${deviceId}`)
      .set({"device_id": deviceId}, { merge: true })
      .then(() => {
        console.log("uninstall event processed successfully");
      })
      .catch((error) => {
        console.error("Error processing uninstall event:", error);
      });
});

额外注意:

  • 云函数必须返回异步操作的Promise,确保数据库操作完成后再结束函数,原代码直接返回console.log会导致函数提前终止,可能丢失数据
  • 如果你的业务需要关联用户而非仅设备,还可以从event.data.userInfo中获取用户相关标识

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 09:16:36