Reputation: 75
I'm trying to implement notifications with Capacitor FCM plugin in my Android app. However, when running the following code in Android Studio, I get this error message: "TypeError: Cannot read property 'getToken' of undefined".
I'm using Ionic 5 with 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);
}
}
I already ran npx cap sync android.
Upvotes: 1
Views: 1967
Reputation: 75
Solved it by the following steps:
Added in data return:
fcm: new FCM()
Added the following in Android Studio:
MainActivity.java: import com.getcapacitor.community.fcm.FCMPlugin;
and then inside the init callback add(FCMPlugin.class);
Upvotes: 1