| 1eda1afa | 16-Oct-2020 |
Stanisław Chmiela <[email protected]> |
[expo-notifications] Migrate NotificationsService to BroadcastReceiver (#10704)
# Why
While moving scheduled notifications handling to the new infrastructure I noticed that it doesn't work as we
[expo-notifications] Migrate NotificationsService to BroadcastReceiver (#10704)
# Why
While moving scheduled notifications handling to the new infrastructure I noticed that it doesn't work as we would expect. For example we weren't able to handle/receive the "setup" event as it would be received by `BroadcastReceiver` which would try to `context.startService` which is illegal while the app is in background.
To mitigate this issue I have researched the topic of background execution on Android even more and came up with a perfect solution — let's use `BroadcastReceiver` from the beginning!
**Advantages:**
- it still is overridable
- we can move even more stuff into this central piece of code (or to phrase it better — we can remove the need for `ScheduledAlarmReceiver` which becomes unnecessary)
- it runs even when the app is in doze mode
**Disadvantages:**
- `BroadcastReceivers` are allowed to execute for up to 10-30 seconds — this is relevant to planned support for image attachments to notifications — this will be the time we will have to download the image. While `Service`s could have executed for longer periods of time I think limiting image size to some small enough value and downloading it in the allowed time period should be feasible.
Reading through https://spin.atomicobject.com/2017/10/16/broadcast-receivers-android-background/:
> Services are meant for actions that need to be performed continuously, even while the app is backgrounded. Examples include playing music or providing GPS directions while the user is viewing other apps.
>
> A BroadcastReceiver is meant for a one-shot action in response to an intent. An intent is an asynchronous message that is broadcast when a certain action happens. Android apps can send or receive broadcast messages from the Android system and other Android apps in a “pub sub” type pattern.
it becomes clear that `BroadcastReceiver` are more than good for our kind of job.
# How
- extracted Firebase handling to a separate service
- changed `onHandleIntent` to `onReceive`
- changed implementation of `doWork` so it queries broadcast receivers and sends an explicit broadcast to the first service
# Test Plan
I have verified all relevant tests pass in `test-suite`.
show more ...
|