Skip to content

Make PushNotifications.register reject/throw instead of crashing the app #2370

Open

Description

@yusufkandemir

Feature Request

Plugin

PushNotifications

Description

When google-services.json is not present, calling PushNotifications.register crashes the app.

The app crashes with

java.lang.IllegalStateException:
Default FirebaseApp is not initialized in this process com.capacitor.app.
Make sure to call FirebaseApp.initializeApp(Context) first.

here:

public void register(PluginCall call) {
FirebaseMessaging.getInstance().setAutoInitEnabled(true);

build.gradle template contains this:

try {
    def servicesJSON = file('google-services.json')
    if (servicesJSON.text) {
        apply plugin: 'com.google.gms.google-services'
    }
} catch(Exception e) {
    logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work")
}

So, I would have expected it to automatically gracefully handle this, or at least throw an error we can handle from PushNotifications.register. That way, all environments, including dev environments, do not have to set up google-services.json and is able to run without push notifications.

Platform(s)

  • Android
  • iOS: should have a workaround, as it's called through the app delegate, but still prefer a solution

Preferred Solution

It should catch the error, then execute call.reject(error) to throw an error that can be handled in JS-side.

Alternatives

  • Gracefully handle the error by just logging a message, similar to what's done in build.gradle.
  • Offer a separate method to check if the requirements are present (e.g. google-services.json). It could be something like await PushNotifications.canRegister() or await PushNotifications.isSupported(). That way, the user could call it and choose to not call register. This can be the only solution or a supplementary one.
  • handle this via env variables, in a more manual manner

Additional Context

#1412

Activity

  1. kaypaerschFondo commented on Oct 12, 2026

    @kaypaerschFondo

    Still the case in 8.1.3 (Capacitor 8.5). Details and the local patch we use:

    If google-services.json is missing (or the google-services Gradle plugin is not applied),
    PushNotifications.register() calls FirebaseMessaging.getInstance(), which throws
    IllegalStateException: Default FirebaseApp is not initialized in this process. The exception
    is thrown on the CapacitorPlugins handler thread and Capacitor rethrows it as a
    RuntimeException, so the whole app is killed. JavaScript cannot catch it.

    Expected: the call is rejected (e.g. code FIREBASE_NOT_CONFIGURED) so apps can run without
    push, for example during development or in builds where push is not set up yet.

    Patch we apply locally:

    private boolean firebaseBereit(PluginCall call) {
        if (FirebaseApp.getApps(getContext()).isEmpty()) {
            call.reject("Firebase is not configured (google-services.json missing)", "FIREBASE_NOT_CONFIGURED");
            return false;
        }
        return true;
    }
    
    @PluginMethod
    public void register(PluginCall call) {
        if (!firebaseBereit(call)) return;
        ...
    }

    (same guard in unregister). Happy to open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions