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

ionic v1中使用FCM遇FCMPlugin未定义问题求助

Fixing "FCMPlugin is not defined" in Ionic v1 with cordova-plugin-fcm

Hey there! Let's work through this error you're facing. I've dealt with similar Cordova plugin issues in Ionic v1 projects before, so here are the most likely fixes to try:

1. Verify the plugin is properly installed and platform is set up

Cordova plugins sometimes fail to register correctly, especially if you add them after setting up your platform. Here's how to check:

  • Run ionic cordova plugin list in your terminal. Look for cordova-plugin-fcm in the output.
  • If it's missing, uninstall and reinstall the plugin properly:
    # Uninstall first
    ionic cordova plugin remove cordova-plugin-fcm
    # Reinstall
    ionic cordova plugin add cordova-plugin-fcm
    # Refresh your platform (critical step!)
    ionic cordova platform remove android && ionic cordova platform add android
    # Do the same for iOS if needed
    ionic cordova platform remove ios && ionic cordova platform add ios
    

2. Ensure you're accessing FCMPlugin correctly in Ionic v1

In Ionic v1, Cordova plugin objects are attached to the global window object, but they might not be immediately available even inside $ionicPlatform.ready(). Wrap your code in a check for window.FCMPlugin to avoid the undefined error:

$ionicPlatform.ready(function() {
  // Check if FCMPlugin is available before using it
  if (window.FCMPlugin) {
    FCMPlugin.getToken(
      function(token) {
        alert('Token: ' + token);
        console.log('Token: ' + token);
      },
      function(err) {
        // Note: You had a typo here earlier - using "token" instead of "err"
        alert('error retrieving token: ' + err);
        console.log('error retrieving token: ' + err);
      }
    );
  } else {
    console.log('FCMPlugin not found - make sure you\'re running on a device/emulator');
  }
});

Also fixed a small typo in your error callback: you were referencing token instead of err in the alert!

3. You can't test this in the browser!

This is a super common gotcha. cordova-plugin-fcm is a native-only plugin - it won't work in a browser tab when you run ionic serve. You need to test on a physical device or emulator:

  • For Android: Run ionic cordova run android (connect a device via USB or use an emulator)
  • For iOS: Run ionic cordova run ios (requires a Mac and Xcode)

4. Try a compatible plugin version

Ionic v1 is an older framework, and newer versions of cordova-plugin-fcm might have compatibility issues. Try installing a stable older version:

ionic cordova plugin add cordova-plugin-fcm@2.1.2

Then re-add your platform as mentioned in step 1.

Give these steps a try, and let me know if you still run into issues!

内容的提问来源于stack exchange,提问作者B.maiTi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:12:36