ionic-team / ionic-team/capacitor-plugins

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

Open
#2,370 0 comments 1 reaction 0 assignees View on GitHub
platform: android
Dominant language
Java
Stars
678
Forks
685
Avg merge
4d 22h
Merged PRs (30d)
3

Description

## 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:
https://github.com/ionic-team/capacitor-plugins/blob/10cf0ed6c943b3e7b35daeea20dfdbbce37206b2/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java#L102-L103

`build.gradle` template contains this:
```gradle
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.