Capacitor Push Notification error

Hi, I am currently using Ionic React. I tried to register push notifications using capacitor’s push notification plugin with the following code

PushNotifications.requestPermission().then(result => {
			if (result.granted) {
			  // Register with Apple / Google to receive push via APNS/FCM
			  PushNotifications.register();
			} else {
			  // Show some error
			}
		  });

I am just copying code from the guide available on capacitor’s docs written in Angular, but i think the code above should run well in a React app. Running on android, i got the following error when the code reaches PushNotifications.register() part.

E/ViewRootImpl: sendUserActionEvent() returned.
E/Capacitor: Serious error executing plugin
    java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)
        at com.getcapacitor.Bridge$1.run(Bridge.java:520)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:65)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.jelena.meacham.v3. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@19.3.0:184)
        at com.google.firebase.messaging.FirebaseMessaging.getInstance(com.google.firebase:firebase-messaging@@20.1.2:1)
        at com.getcapacitor.plugin.PushNotifications.register(PushNotifications.java:82)
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99) 
        at com.getcapacitor.Bridge$1.run(Bridge.java:520) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.os.HandlerThread.run(HandlerThread.java:65) 
E/AndroidRuntime: FATAL EXCEPTION: CapacitorPlugins
    Process: com.jelena.meacham.v3, PID: 10808
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.getcapacitor.Bridge$1.run(Bridge.java:529)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:65)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)
        at com.getcapacitor.Bridge$1.run(Bridge.java:520)
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.os.HandlerThread.run(HandlerThread.java:65) 
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.jelena.meacham.v3. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@19.3.0:184)
        at com.google.firebase.messaging.FirebaseMessaging.getInstance(com.google.firebase:firebase-messaging@@20.1.2:1)
        at com.getcapacitor.plugin.PushNotifications.register(PushNotifications.java:82)
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99) 
        at com.getcapacitor.Bridge$1.run(Bridge.java:520) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.os.HandlerThread.run(HandlerThread.java:65) 

I couldn’t find a way to workthrough this error, so I hope someone could help me out. Here’s how my package.json look btw.

{
  "name": "Training app",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "@capacitor/android": "^2.0.2",
    "@capacitor/core": "2.0.2",
    "@capacitor/ios": "^2.0.2",
    "@ionic-native/core": "^5.24.0",
    "@ionic-native/http": "^5.24.0",
    "@ionic-native/image-picker": "^5.24.0",
    "@ionic-native/in-app-browser": "^5.24.0",
    "@ionic-native/in-app-purchase": "^5.24.0",
    "@ionic-native/screen-orientation": "^5.24.0",
    "@ionic/react": "^5.1.0",
    "@ionic/react-router": "^5.1.0",
    "@types/jest": "^25.2.1",
    "@types/node": "^13.13.4",
    "@types/react": "^16.9.34",
    "@types/react-dom": "^16.9.7",
    "@types/react-router": "^5.1.7",
    "@types/react-router-dom": "^5.1.5",
    "chart.js": "^2.9.3",
    "cordova-plugin-advanced-http": "^2.3.1",
    "cordova-plugin-file": "^6.0.2",
    "cordova-plugin-inappbrowser": "^3.1.0",
    "cordova-plugin-inapppurchase": "^1.2.0",
    "cordova-plugin-screen-orientation": "^3.0.2",
    "cordova-plugin-telerik-imagepicker": "^2.3.3",
    "es6-promise-plugin": "^4.2.2",
    "ionic-native": "^2.9.0",
    "jetifier": "^1.6.5",
    "react": "^16.13.1",
    "react-chartjs-2": "^2.8.0",
    "react-click-n-hold": "^1.0.7",
    "react-dom": "^16.13.1",
    "react-router": "^5.1.0",
    "react-router-dom": "^5.1.0",
    "react-shimmer": "^2.0.1",
    "react-zoom-pan-pinch": "^1.6.1",
    "typescript": "3.8.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "description": "An Ionic project",
  "devDependencies": {
    "@capacitor/cli": "2.0.2",
    "react-scripts": "^3.4.1"
  }
}

I solved it by including google-services.json in android/app directory. I get the wrong idea and include it in android library.

1 Like