【问题标题】:Capacitor - TypeError: Cannot read property 'getToken' of undefined电容器 - 类型错误:无法读取未定义的属性“getToken”
【发布时间】:2021-02-15 10:44:11
【问题描述】:

我正在尝试在我的 Android 应用中使用 Capacitor FCM 插件实现通知。但是,在 Android Studio 中运行以下代码时,我收到此错误消息:“TypeError: Cannot read property 'getToken' of undefined”。

我将 Ionic 5 与 Vue 3 一起使用。

App.vue

async pushInit() {
      const fcm = new FCM();

      alert("...");
      try {
        PushNotifications.addListener("registrationError", (error) => {
          console.log(`error on register ${JSON.stringify(error)}`);
        }),
          PushNotifications.addListener(
            "pushNotificationReceived",
            (notification) => {
              console.log(`notification ${JSON.stringify(notification)}`);
            }
          ),
          PushNotifications.addListener(
            "pushNotificationActionPerformed",
            async (notification) => {
              console.log("notification succeeded");
            }
          ),
          PushNotifications.register();
        const fcmToken = await fcm.getToken();
        alert(fcmToken);
        console.log("token:" + fcmToken);
      } catch (e) {
        console.log(e);
      }
    }

我已经运行了 npx cap sync android。

【问题讨论】:

    标签: firebase vue.js ionic-framework capacitor


    【解决方案1】:

    通过以下步骤解决:

    在数据返回中添加:

    fcm: new FCM()
    

    在 Android Studio 中添加了以下内容: MainActivity.java: import com.getcapacitor.community.fcm.FCMPlugin; 然后在init回调里面add(FCMPlugin.class);

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 2021-12-31
      • 2021-12-03
      • 2021-11-22
      相关资源
      最近更新 更多