ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.45k stars 977 forks source link

[Android] Can't use Capacitor plugin Push Notifications #1303

Closed TayKara closed 5 years ago

TayKara commented 5 years ago

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

OS of the development machine

Capacitor version: 1.0.0-beta.19

node version: v10.15.1

npm version: 6.7.0

jcesarmobile commented 5 years ago

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

TayKara commented 5 years ago

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 ! :)

ostno commented 4 years ago

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

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.