Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expo-notifications] Fix exception sometimes being thrown if observer is cleared before it's registered #10640

Merged
merged 3 commits into from Oct 13, 2020

Conversation

sjchmiela
Copy link
Contributor

@sjchmiela sjchmiela commented Oct 12, 2020

Why

Probably fixes #10610.

How

Looked into the stacktrace, noticed that we may be trying to add a null observer to the registry (origin, the observer is saved in a WeakReference).

Since we really want the observer to be saved and we don't have infrastructure to notify NotificationsHelper of its destroy we need to leak the observer (a refactor removing need for this is currently in the works). If we need to leak the observer lets leak just as little as we can (just the observer, not the helper). So I created a simple class forwarding interesting calls to the helper.

Test Plan

I have tested the code manually (verified that the callbacks are called correctly).

@sjchmiela sjchmiela changed the title @sjchmiela/fix lifecycle observer [expo-notifications] Fix exception sometimes being thrown if observer is cleared before it's registered Oct 12, 2020
@sjchmiela sjchmiela marked this pull request as ready for review October 12, 2020 12:45
@@ -121,7 +121,7 @@ public static void addListener(NotificationManager listener) {
}
}

private boolean mIsAppInForeground = false;
private boolean mIsAppInForeground = ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

@RodolfoGS
Copy link
Contributor

Hi @sjchmiela, thank you so much for the PR 🎉

I'm not an Android developer, so sorry if this question is dummy. I'm trying to test this PR but I'm getting the following error:

node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/notifications/service/NotificationsHelper.java:70: error: cannot find symbol
  private NotificationsHelperLifecycleObserver mLifecycleObserver;
          ^
  symbol:   class NotificationsHelperLifecycleObserver
  location: class NotificationsHelper

I tested adding the import but I get the same error.

import expo.modules.notifications.notifications.service.NotificationsHelperLifecycleObserver;
node_modules/expo-notifications/android/src/main/java/expo/modules/notifications/notifications/service/NotificationsHelper.java:43: error: cannot find symbol
import expo.modules.notifications.notifications.service.NotificationsHelperLifecycleObserver;
                                                       ^
  symbol:   class NotificationsHelperLifecycleObserver
  location: package expo.modules.notifications.notifications.service

Of course I created NotificationsHelperLifecycleObserver.kt file. Do you know what I'm doing bad? Maybe it's something related with Kotlin?

Sorry if this question is dummy and again, thank you so much for your support!

@sjchmiela
Copy link
Contributor Author

Hey @RodolfoGS! Thank you so much for trying to test this PR! It it indeed related to Kotlin — on master we have Kotlin introduced in expo-notifications already which is not the case in your copy of expo-notifications. The offending commit was — 0ed719b — if you want to try to apply it to your project. 🙂

@sjchmiela sjchmiela merged commit a10ddc6 into master Oct 13, 2020
@sjchmiela sjchmiela deleted the @sjchmiela/fix-lifecycle-observer branch October 13, 2020 08:21
sjchmiela added a commit that referenced this pull request Oct 14, 2020
…vice delegate (#10666)

# Why

A prerequisite to single-threaded notifications handling.

# How

More refactoring, moving staff from `NotificationsHelper` to appropriate `NotificationsService`' delegates. This time to `ExpoHandlingDelegate`.

In the meantime I have also removed a memory leak introduced in #10640 — we don't need to _observe_ the state, we can check for the state every time we need to access it.

# Test Plan

Push and scheduled notifications are presented.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants