Skip to content

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

Open
@yusufkandemir

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions