我试图在我的Android应用程序中实现电容器FCM插件通知。然而,当在Android Studio中运行以下代码时,我得到此错误信息:"TypeError:无法读取属性'getToken'的未定义"
我用的是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.
按以下步骤解决:
数据返回:
fcm: new FCM()
在Android Studio中增加了以下内容:MainActivity.java:import com.getcapacitor.community.fcm.FCMPlugin;
,然后在初始化回调add(FCMPlugin.class);