Capacitor: [Android] Can't use Capacitor plugin Push Notifications

Created on 11 Mar 2019  路  3Comments  路  Source: ionic-team/capacitor

Description of the problem:
I can't use Push Notifications plugin, it causes error in execution.
The only thing I did was to create an Ionic project (ionic start testNotifications blank --capacitor), init cap, and modified home.page.ts and then build/open in android studio.

In home.page.ts :

import { Component, OnInit } from '@angular/core';
import { Plugins } from '@capacitor/core';

const { PushNotifications } = Plugins;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {

ngOnInit(): void {
PushNotifications.register();
}

}

The error I have in Android Studio (when executing in my phone (Honor 9 Lite - Android 8.0.0) connected by USB in debug mode :

2019-03-11 10:10:36.034 21074-21274/be.notifications.test E/Capacitor: Unable to execute plugin method
com.getcapacitor.PluginInvocationException: Unable to invoke method register on plugin com.getcapacitor.plugin.PushNotifications
at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:101)
at com.getcapacitor.Bridge$2.run(Bridge.java:447)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
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$2.run(Bridge.java:447)聽
at android.os.Handler.handleCallback(Handler.java:808)聽
at android.os.Handler.dispatchMessage(Handler.java:101)聽
at android.os.Looper.loop(Looper.java:166)聽
at android.os.HandlerThread.run(HandlerThread.java:65)聽
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process be.notifications.test. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.2:240)
at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source:1)
at com.google.firebase.messaging.FirebaseMessaging.getInstance(Unknown Source:2)
at com.getcapacitor.plugin.PushNotifications.register(PushNotifications.java:57)
at java.lang.reflect.Method.invoke(Native Method)聽
at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)聽
at com.getcapacitor.Bridge$2.run(Bridge.java:447)聽
at android.os.Handler.handleCallback(Handler.java:808)聽
at android.os.Handler.dispatchMessage(Handler.java:101)聽
at android.os.Looper.loop(Looper.java:166)聽
at android.os.HandlerThread.run(HandlerThread.java:65)聽

Am I doing something something wrong, or am I missing a step to make it works ?
Affected platform

  • [x] Android

OS of the development machine

  • [x] Windows

Capacitor version: 1.0.0-beta.19

node version: v10.15.1

npm version: 6.7.0

Most helpful comment

It was indeed the problem !
I putted my google-services.json file in the wrong directory.
For the ones who want to know, google-services.json file has to be add in ./android/app/ and the package name of your project has to be the same as the package name in your google-services.json

EDIT : I no longer have an error, but I can't register.
Nothing is happening when I do :

PushNotifications.register();
PushNotifications.addListener('registration', (token: PushNotificationToken) => {
  console.log('token ' + token.value);
});

EDIT 2 : forgot to activate wifi, forgive my idiocy...

Many thanks @jcesarmobile ! :)

All 3 comments

Did you add the google-services.json file to your app?

It was indeed the problem !
I putted my google-services.json file in the wrong directory.
For the ones who want to know, google-services.json file has to be add in ./android/app/ and the package name of your project has to be the same as the package name in your google-services.json

EDIT : I no longer have an error, but I can't register.
Nothing is happening when I do :

PushNotifications.register();
PushNotifications.addListener('registration', (token: PushNotificationToken) => {
  console.log('token ' + token.value);
});

EDIT 2 : forgot to activate wifi, forgive my idiocy...

Many thanks @jcesarmobile ! :)

You need to add google-services.json file to yourapp/android/app/ not simply to yourapp/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielsogl picture danielsogl  路  3Comments

json-derulo picture json-derulo  路  3Comments

alexcroox picture alexcroox  路  3Comments

moberwasserlechner picture moberwasserlechner  路  3Comments

stripathix picture stripathix  路  3Comments