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][Android] map Expo and Firebase notifications correctly #30615

Conversation

douglowder
Copy link
Contributor

@douglowder douglowder commented Jul 25, 2024

Why

Since #7280, some fields in Android notification content have been remapped. This is done in order to align the results seen in JS with the fields passed into the Expo push notification service when it sends notifications, and to align the API across the iOS and Android platforms.

However, this mapping is also happening when notifications are sent directly from Firebase. This leads to unexpected results where the data part of the notification payload does not appear correctly in the JS notification structures.

This PR fixes that by heuristically detecting whether a notification is sent from Expo or not, and performing the appropriate mapping in either case.

How

  • Modified code in NotificationSerializer to detect when the body attribute is a JSON string (indicating that the notification was sent from Expo), and remapping the resulting bundles as appropriate for both cases.
  • Modified NotificationsEmitter and mapNotificationResponse in the JS code to correctly unpack data strings in the case of Expo notifications.
  • Added extensive debug logging, wrapped by BuildConfig.DEBUG so the logging does not happen in release/production builds.

Test Plan

  • Both Firebase and Expo notifications should be mapped correctly in the test app
  • Notification responses should be mapped correctly in both cases when tapping on a notification while the app is in background or not running
  • CI should pass

Checklist

Copy link

linear bot commented Jul 25, 2024

Copy link
Contributor Author

douglowder commented Jul 25, 2024

@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label Jul 25, 2024
@expo-bot
Copy link
Collaborator

expo-bot commented Jul 25, 2024

The Pull Request introduced fingerprint changes against the base commit: e1ca69c

Fingerprint diff
[
  {
    "op": "changed",
    "source": {
      "type": "dir",
      "filePath": "../../packages/expo-notifications/android",
      "reasons": [
        "expoAutolinkingAndroid"
      ],
      "hash": "a12805410193866c27ccf23179a53f3f7902e174"
    }
  }
]

Generated by PR labeler 🤖

@douglowder douglowder marked this pull request as ready for review July 25, 2024 08:40
@douglowder douglowder requested a review from tsapeta as a code owner July 25, 2024 08:40
@douglowder douglowder force-pushed the doug/eng-12758-investigate-how-expo-is-remapping-notification-payload branch from 0acbab4 to cee2e3e Compare July 25, 2024 08:41
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Jul 25, 2024
@douglowder douglowder requested review from lukmccall and removed request for tsapeta July 25, 2024 08:45
@douglowder douglowder force-pushed the doug/eng-12758-investigate-how-expo-is-remapping-notification-payload branch from 7b3920e to fd3d9f8 Compare July 26, 2024 08:09
@douglowder
Copy link
Contributor Author

Manual testing with the test app looks good:

  • Notification data property appears the same whether originating from Expo or Firebase
  • Notification responses appear correctly handled when app in background or killed
  • Logging code working correctly when app built with BuildConfig.DEBUG

@douglowder douglowder force-pushed the doug/eng-12758-investigate-how-expo-is-remapping-notification-payload branch from b57b5dd to 2e0f92b Compare July 27, 2024 01:48
@douglowder douglowder merged commit 3eff758 into main Jul 27, 2024
6 of 8 checks passed
@douglowder douglowder deleted the doug/eng-12758-investigate-how-expo-is-remapping-notification-payload branch July 27, 2024 02:09
douglowder added a commit that referenced this pull request Jul 27, 2024
…rectly (#30615)

Since #7280, some fields in Android notification content have been remapped. This is done in order to align the results seen in JS with the fields passed into the Expo push notification service when it sends notifications, and to align the API across the iOS and Android platforms.

However, this mapping is also happening when notifications are sent directly from Firebase. This leads to unexpected results where the `data` part of the notification payload does not appear correctly in the JS notification structures.

This PR fixes that by heuristically detecting whether a notification is sent from Expo or not, and performing the appropriate mapping in either case.

- Modified code in `NotificationSerializer` to detect when the `body` attribute is a JSON string (indicating that the notification was sent from Expo), and remapping the resulting bundles as appropriate for both cases.
- Modified `NotificationsEmitter` and `mapNotificationResponse` in the JS code to correctly unpack data strings in the case of Expo notifications.
- Added extensive debug logging, wrapped by `BuildConfig.DEBUG` so the logging does not happen in release/production builds.

- Both Firebase and Expo notifications should be mapped correctly in the test app
- Notification responses should be mapped correctly in both cases when tapping on a notification while the app is in background or not running
- CI should pass

<!--
Please check the appropriate items below if they apply to your diff. This is required for changes to Expo modules.
-->

- [ ] Documentation is up to date to reflect these changes (eg: https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin).
Bundle serializedContent = new Bundle();
serializedContent.putString("title", extras.getString("title"));
String body = extras.getString("body");
String projectId = extras.getString("projectId");
Copy link
Member

Choose a reason for hiding this comment

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

If possible, don't special case for EAS Notify. Instead, special case for "JSON-like bodies".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: fingerprint changed bot: passed checks ExpoBot has nothing to complain about published Changes from the PR have been published to npm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants