ionic-team / capacitor-plugins

Official plugins for Capacitor ⚡️
485 stars 562 forks source link

App crashes on PushNotifications.register #1412

Closed mvaljento closed 1 year ago

mvaljento commented 1 year ago

Bug Report

Plugin(s)

@capacitor/push-notifications: 4.1.2

Capacitor Version

Latest Dependencies:

@capacitor/cli: 4.6.3 @capacitor/core: 4.6.3 @capacitor/android: 4.6.3 @capacitor/ios: 4.6.3

Installed Dependencies:

@capacitor/cli: 4.6.3 @capacitor/core: 4.6.3 @capacitor/ios: 4.6.3 @capacitor/android: 4.6.3

Platform(s)

Android (Pixel_4_API_Tiramisu) emulator

Current Behavior

I have a new Capacitor application with Angular and Ionic. I installed Capacitor push notifications plugin as described here:

https://capacitorjs.com/docs/apis/push-notifications

PushNotifications.requestPermissions() works as expected and the app receives "request: granted", but calling PushNotifications.register() afterwards crashes the application with the following output:

VM3:246 native PushNotifications.register (#24258324)

Expected Behavior

The application should register the push notifications (or at least give an error message instead of crashing). This worked in my previous Cordova-plugin based application with the same google json.

Code Reproduction

I have a code which requests FCM permissions and then attempts to register the device like so:

this.platform.ready().then(() => {
        PushNotifications.requestPermissions().then(result => {
          console.error('[FCM] Permissions result', result);
          if (result.receive === 'granted') {
            PushNotifications.register(); // THIS LINE CAUSES THE APP TO CRASH
          } else {
            console.error('[FCM] Error on permissions', result);
          }
        });
});

I tried adding the following to capacitor.config.ts, but it made no difference:

plugins: {
    PushNotifications: {
      presentationOptions: ["badge", "sound", "alert"],
    },
  },

Other Technical Details

Development platform: Macbook Pro M1 (2021), Ventura 13.1

java -version

openjdk version "19.0.2" 2023-01-17 OpenJDK Runtime Environment Homebrew (build 19.0.2) OpenJDK 64-Bit Server VM Homebrew (build 19.0.2, mixed mode, sharing)

gradle -version

Gradle 7.6

Build time: 2022-11-25 13:35:10 UTC Revision: daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8

Kotlin: 1.7.10 Groovy: 3.0.13 Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021 JVM: 19.0.2 (Homebrew 19.0.2) OS: Mac OS X 13.1 aarch64

ng version

Angular CLI: 15.1.4 Node: 16.15.0 Package Manager: npm 9.1.1 OS: darwin arm64

@angular/core: 15.1.3

ionic --version

Ionic 6.20.8

An update: I created a brand new app with no other libraries or plugins and the same thing happens.

By mistake at first I thought it worked, but I hadn't subscribed to an observable that handles the registration thus the register function never got triggered. When it triggers however, the app still crashes.

I created a repo with the test app. Please note that you might have to update the android/gradle/wrapper/gradle-wrapper.properties file with the correct gradle version if you get a "BUG" error notice when building. the correct distribution url should be:

distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-all.zip

https://github.com/mvaljento/fcm-test

I'm running the app with:

npx cap sync
ionic cap run android --external --livereload --port=8010 --host=192.168.1.135 --target=Pixel_4_API_Tiramisu

Here's the logcat output of the crash:


02-08 06:34:02.756 20513 20513 I Capacitor/Console: File: http://192.168.1.135:8010/main.js - Line 105 - Msg: [FCM] Permissions result {"receive":"granted"}
02-08 06:34:02.756 20513 20513 I Capacitor/Console: File: http://192.168.1.135:8010/main.js - Line 107 - Msg: Registering push notifications...
02-08 06:34:02.757 20513 20513 V Capacitor/Plugin: To native (Capacitor plugin): callbackId: 33071558, pluginId: PushNotifications, methodName: register
02-08 06:34:02.757 20513 20513 V Capacitor: callback: 33071558, pluginId: PushNotifications, methodName: register, methodData: {}
02-08 06:34:02.774 20513 20610 E Capacitor: Serious error executing plugin
02-08 06:34:02.774 20513 20610 E Capacitor: java.lang.reflect.InvocationTargetException
02-08 06:34:02.774 20513 20610 E Capacitor:     at java.lang.reflect.Method.invoke(Native Method)
02-08 06:34:02.774 20513 20610 E Capacitor:     at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)
02-08 06:34:02.774 20513 20610 E Capacitor:     at com.getcapacitor.Bridge.lambda$callPluginMethod$0$com-getcapacitor-Bridge(Bridge.java:763)
02-08 06:34:02.774 20513 20610 E Capacitor:     at com.getcapacitor.Bridge$$ExternalSyntheticLambda5.run(Unknown Source:8)
02-08 06:34:02.774 20513 20610 E Capacitor:     at android.os.Handler.handleCallback(Handler.java:942)
02-08 06:34:02.774 20513 20610 E Capacitor:     at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 06:34:02.774 20513 20610 E Capacitor:     at android.os.Looper.loopOnce(Looper.java:201)
02-08 06:34:02.774 20513 20610 E Capacitor:     at android.os.Looper.loop(Looper.java:288)
02-08 06:34:02.774 20513 20610 E Capacitor:     at android.os.HandlerThread.run(HandlerThread.java:67)
02-08 06:34:02.774 20513 20610 E Capacitor: Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process io.unitt.messenger. Make sure to call FirebaseApp.initializeApp(Context) first.
02-08 06:34:02.774 20513 20610 E Capacitor:     at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:184)
02-08 06:34:02.774 20513 20610 E Capacitor:     at com.google.firebase.messaging.FirebaseMessaging.getInstance(FirebaseMessaging.java:137)
02-08 06:34:02.774 20513 20610 E Capacitor:     at com.capacitorjs.plugins.pushnotifications.PushNotificationsPlugin.register(PushNotificationsPlugin.java:76)
02-08 06:34:02.774 20513 20610 E Capacitor:     ... 9 more
--------- beginning of crash
02-08 06:34:02.776 20513 20610 E AndroidRuntime: FATAL EXCEPTION: CapacitorPlugins
02-08 06:34:02.776 20513 20610 E AndroidRuntime: Process: io.unitt.messenger, PID: 20513
02-08 06:34:02.776 20513 20610 E AndroidRuntime: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at com.getcapacitor.Bridge.lambda$callPluginMethod$0$com-getcapacitor-Bridge(Bridge.java:772)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at com.getcapacitor.Bridge$$ExternalSyntheticLambda5.run(Unknown Source:8)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:942)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at android.os.Looper.loopOnce(Looper.java:201)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:288)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at android.os.HandlerThread.run(HandlerThread.java:67)
02-08 06:34:02.776 20513 20610 E AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at com.getcapacitor.Bridge.lambda$callPluginMethod$0$com-getcapacitor-Bridge(Bridge.java:763)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        ... 6 more
02-08 06:34:02.776 20513 20610 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process io.unitt.messenger. Make sure to call FirebaseApp.initializeApp(Context) first.
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:184)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at com.google.firebase.messaging.FirebaseMessaging.getInstance(FirebaseMessaging.java:137)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        at com.capacitorjs.plugins.pushnotifications.PushNotificationsPlugin.register(PushNotificationsPlugin.java:76)
02-08 06:34:02.776 20513 20610 E AndroidRuntime:        ... 9 more
02-08 06:34:02.785   602   964 I am_crash: [20513,0,io.unitt.messenger,952680006,java.lang.IllegalStateException,Default FirebaseApp is not initialized in this process io.unitt.messenger. Make sure to call FirebaseApp.initializeApp(Context) first.,FirebaseApp.java,184]
02-08 06:34:02.800   602   964 W ActivityTaskManager:   Force finishing activity io.unitt.messenger/.MainActivity
02-08 06:34:02.805   602   964 I wm_finish_activity: [0,53714175,142,io.unitt.messenger/.MainActivity,force-crash]
02-08 06:34:02.805   602   964 I wm_task_moved: [1,1,3]
02-08 06:34:02.805   602   964 I wm_task_moved: [84,1,2147483647]
02-08 06:34:02.806   602   964 I wm_task_to_front: [0,84]
02-08 06:34:02.806   602   964 I wm_focused_root_task: [0,0,1,142,finish-top adjustFocusToNextFocusableTask]
02-08 06:34:02.806   602   964 I wm_set_resumed_activity: [0,com.google.android.apps.nexuslauncher/.NexusLauncherActivity,finish-top adjustFocusToNextFocusableTask]
02-08 06:34:02.806   602 20645 I DropBoxManagerService: add tag=data_app_crash isTagEnabled=true flags=0x2
02-08 06:34:02.809 20513 20513 I wm_on_top_resumed_lost_called: [53714175,io.unitt.messenger.MainActivity,topStateChangedWhenResumed]
02-08 06:34:02.815   402   489 D goldfish-address-space: claimShared: Ask to claim region [0x1ed66c000 0x1edc70000]
02-08 06:34:02.817   826  1020 D EGL_emulation: app_time_stats: avg=15.71ms min=0.85ms max=220.10ms count=35
02-08 06:34:02.833   602   964 I wm_pause_activity: [0,53714175,io.unitt.messenger/.MainActivity,userLeaving=false,finish]
02-08 06:34:02.836   602   628 I input_focus: [Requesting to set focus to null window,reason=UpdateInputWindows]
02-08 06:34:02.841   602   964 I sysui_multi_action: [757,316,758,4,759,-1]
02-08 06:34:02.841 20513 20610 I Process : Sending signal. PID: 20513 SIG: 9
mvaljento commented 1 year ago

An update: I finally got it working by adding the following to beginning of the android/build.gradle file:

apply plugin: 'com.google.gms.google-services'

Also I needed to manually copy the google-services.json file to the android/app folder.

I guess something in the installation or build process of the plugin is not working properly?

mohit-codes commented 1 year ago

Hey @mvaljento i also faced the same issue, Thanks for opening it also thanks for providing the solution 👍

jcesarmobile commented 1 year ago

copying the google-services.json to android/app folder is the only step needed, it's documented here

the apply plugin: 'com.google.gms.google-services' is already there, but runs only if google-services.json file is present https://github.com/mvaljento/fcm-test/blob/main/android/app/build.gradle#L49

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 the plugin, please create a new issue and ensure the template is fully filled out.