Skip to content

Comments

fix(app, android): avoid NullPointerException in isAppInForeground check#8877

Merged
mikehardy merged 2 commits intomainfrom
@mikehardy/avoid-null-pointer-exception-app-shared-utils
Feb 7, 2026
Merged

fix(app, android): avoid NullPointerException in isAppInForeground check#8877
mikehardy merged 2 commits intomainfrom
@mikehardy/avoid-null-pointer-exception-app-shared-utils

Conversation

@mikehardy
Copy link
Collaborator

@mikehardy mikehardy commented Feb 5, 2026

Description

In the kotlin documentation for this platform API, the return value is designated as nullable, but in the android documentation it is not

Java not noted as null: https://developer.android.com/reference/kotlin/android/app/TaskInfo#baseactivity
Kotlin indeed is ? return: https://developer.android.com/reference/kotlin/android/app/TaskInfo#baseactivity

We coded off the android API and expected it to be non-null but there are user reports of a crash here

So, make the access to this potentially null method result null-safe and return a reasonable default in that case

Related issues

Release Summary

fix release via conventional commit

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
    • Yes
  • My change supports the following platforms;
    • Android
    • iOS
    • Other (macOS, web)
  • My change includes tests;
    • e2e tests added or updated in packages/\*\*/e2e
    • jest tests added or updated in packages/\*\*/__tests__
  • I have updated TypeScript types that are affected by my change.
  • This is a breaking change;
    • Yes
    • No

Test Plan

  • passes lint locally
  • builds locally, and CI should build as well

Only way to cover it via tests is to receive a local FCM in foreground and background, I exercised that with my build demo which has that feature -- the code is exercised upon receipt of any FCM - while using locally created packages from this PR

https://github.com/mikehardy/rnfbdemo/blob/4ff72ba5b9894542e838b246562c9c14d285aef4/App.tsx#L276-L289

I had to fixup the test cloud function though, as it wasn't working since I converted to v2 / modular functions API and I haven't exercised it yet to notice! It works now though.

Testing indicated that the code path was definitely hit, as I received the test FCM, and the RNFirebaseMsgReceiver message indicates the code path was traveled since there is no possible way through the code without hitting that method, if you see this log message but not the one saying "broadcast intent received with no extras"

02-05 17:24:55.786  8417  8417 D RNFirebaseMsgReceiver: broadcast received for message
02-05 17:24:55.788  8417  9314 I ReactNativeJS: messaging.onMessage received: {"originalPriority":1,"priority":1,"sentTime":1770330296359,"data":{"message":"hello from data block"},"from":"448618578101","messageId":"0:1770330296368836%6b3c7cd2f9fd7ecd","ttl":2419200}

ref:

Log.d(TAG, "broadcast received for message");
if (ReactNativeFirebaseApp.getApplicationContext() == null) {
ReactNativeFirebaseApp.setApplicationContext(context.getApplicationContext());
}
if (intent.getExtras() == null) {
Log.e(TAG, "broadcast intent received with no extras");
return;
}
RemoteMessage remoteMessage = new RemoteMessage(intent.getExtras());
ReactNativeFirebaseEventEmitter emitter = ReactNativeFirebaseEventEmitter.getSharedInstance();
// Add a RemoteMessage if the message contains a notification payload
if (remoteMessage.getNotification() != null) {
notifications.put(remoteMessage.getMessageId(), remoteMessage);
ReactNativeFirebaseMessagingStoreHelper.getInstance()
.getMessagingStore()
.storeFirebaseMessage(remoteMessage);
}
// |-> ---------------------
// App in Foreground
// ------------------------
if (SharedUtils.isAppInForeground(context)) {


Think react-native-firebase is great? Please consider supporting the project with any of the below:

forgot to do that after converting to modular, so it was returning an
internal error, it works in testing (using rnfbdemo/make-demo.sh) with this
fix deployed

also made sure to deploy in correct project by adding a `firebase use` command
to the functions deploy package.json script
@vercel
Copy link

vercel bot commented Feb 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
react-native-firebase Ready Ready Preview, Comment Feb 5, 2026 10:34pm

Request Review

@mikehardy mikehardy merged commit 90e7c1e into main Feb 7, 2026
23 of 25 checks passed
@mikehardy mikehardy deleted the @mikehardy/avoid-null-pointer-exception-app-shared-utils branch February 7, 2026 00:54
@mikehardy mikehardy removed the Workflow: Needs Review Pending feedback or review from a maintainer. label Feb 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants