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

fix: APN notification topic not composed based on push type #347

Merged
merged 6 commits into from
Jan 1, 2025

Conversation

cbaker6
Copy link
Contributor

@cbaker6 cbaker6 commented Dec 29, 2024

New Pull Request Checklist

Issue Description

An APN notification topic currently stays as the topic initialized by the Parse-Server (_Installation.appIdentifier) and isn't updated based on pushType. Based on the Apple Documentation, a specific pushType may require a topic to change.

In addition, the notification header determined by parse-server-push-adapter currently has the highest priority. This makes it harder for the developer to specify particular header information in Cloud Code to prioritize over parse-server-push-adapter header information (parse-server running in memory). This is also useful when Apple updates/adds new notification types and allows devs to add updates to their respective Cloud Code dynamically and adapt the latest features.

Closes: #348

Approach

  • Update notification topic based on pushType
  • Prioritize notification information passed as data over default header information without breaking the current API (see explanation above)
  • Clean up code/errors

TODOs before merging

  • Add tests

Copy link

parse-github-assistant bot commented Dec 29, 2024

Thanks for opening this pull request!

  • 🎉 We are excited about your hands-on contribution!

src/APNS.js Show resolved Hide resolved
src/APNS.js Show resolved Hide resolved
@cbaker6
Copy link
Contributor Author

cbaker6 commented Dec 29, 2024

@mtrezza this PR is ready for review. After you approve the workflow to run, I'll address any test-passing issues. The tests currently pass on my local machine, but it's possible there can be failures since the CI uses older versions of Node.

@cbaker6
Copy link
Contributor Author

cbaker6 commented Dec 29, 2024

Don’t merge yet, should have an additional commit up within the next hour

@cbaker6
Copy link
Contributor Author

cbaker6 commented Dec 29, 2024

@mtrezza I'm finished with my updates, ready for your review

@cbaker6 cbaker6 changed the title feat: Update APN notification topic update based on pushType feat: Update APN notification topic based on pushType Dec 29, 2024
Copy link
Member

@mtrezza mtrezza left a comment

Choose a reason for hiding this comment

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

Is there a breaking change in this, since it now updates the topic based on push type?

@cbaker6
Copy link
Contributor Author

cbaker6 commented Dec 31, 2024

Is there a breaking change in this since it now updates the topic based on push type?

I don't believe the changes are breaking, but let me know what you think based on my explanation below:

The current topic comes from the first device (_Installation) appIdentifier property in the list of devices passed to the Parse send method here:

// Start by clustering the devices per appIdentifier
allDevices.forEach(device => {
const appIdentifier = device.appIdentifier;
devicesPerAppIdentifier[appIdentifier] = devicesPerAppIdentifier[appIdentifier] || [];
devicesPerAppIdentifier[appIdentifier].push(device);
});
for (const key in devicesPerAppIdentifier) {
const devices = devicesPerAppIdentifier[key];
const appIdentifier = devices[0].appIdentifier;
const providers = this._chooseProviders(appIdentifier);
// No Providers found
if (!providers || providers.length === 0) {
const errorPromises = devices.map(device => APNS._createErrorPromise(device.deviceToken, 'No Provider found'));
allPromises = allPromises.concat(errorPromises);
continue;
}
const headers = { expirationTime: expirationTime, topic: appIdentifier, collapseId: collapseId, pushType: pushType, priority: priority }

This method is called from the PushAdapter here:

send(data, installations) {
const deviceMap = classifyInstallations(installations, this.validPushTypes);
const sendPromises = [];
for (const pushType in deviceMap) {
const sender = this.senderMap[pushType];
const devices = deviceMap[pushType];
if(Array.isArray(devices) && devices.length > 0) {
if (!sender) {
log.verbose(LOG_PREFIX, `Can not find sender for push type ${pushType}, ${data}`)
const results = devices.map((device) => {
return Promise.resolve({
device,
transmitted: false,
response: {'error': `Can not find sender for push type ${pushType}, ${data}`}
})
});
sendPromises.push(Promise.all(results));
} else {
sendPromises.push(sender.send(data, devices));

My thought is, "technically", this value comes directly from the first _Installation and remains constant for a particular app. Before the current PR, there wasn't a way to replace a different value for topic via this repo or node-apn (not even through rawPayload as this is a header value and rawPayload has no effect on header values). Of course, a "savvy" Parse developer may have decided to modify the "first" appIdentifier of a list of _Installation's before calling the send method, but this would be a hack IMO, so I don't particularly count breaking the hack as a "breaking change".

I guess that other developers who have LiveActivity or other pushTypes that require changes to the topic working are either not using the ParsePushAdapter (using another package or custom code to send them) or APN isn't enforcing the topic changes per pushType, but may start enforcing it in the future since it's documented.

Definitely let me know if you agree/disagree, as this is just my opinion.

@cbaker6
Copy link
Contributor Author

cbaker6 commented Dec 31, 2024

In addition, the code changes pass all the old tests without modification. Which can be used as a lightweight indicator of "non-breaking," but that depends on the test suite's robustness in capturing the library's current expectations.

Copy link
Member

@mtrezza mtrezza left a comment

Choose a reason for hiding this comment

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

I think we can merge as non-breaking, with a proper description in the changelog. I actually looks like a bug fix, so how about:

fix: APN notification topic not composed based on push type

@cbaker6
Copy link
Contributor Author

cbaker6 commented Jan 1, 2025

LGTM!

@cbaker6 cbaker6 changed the title feat: Update APN notification topic based on pushType fix: APN notification topic not composed based on push type Jan 1, 2025
@mtrezza mtrezza merged commit 9905c34 into parse-community:master Jan 1, 2025
6 checks passed
parseplatformorg pushed a commit that referenced this pull request Jan 1, 2025
## [6.9.1](6.9.0...6.9.1) (2025-01-01)

### Bug Fixes

* APN notification topic not composed based on push type ([#347](#347)) ([9905c34](9905c34))
@parseplatformorg
Copy link
Contributor

🎉 This change has been released in version 6.9.1

@cbaker6 cbaker6 deleted the improveTopic branch January 1, 2025 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

APN Topic should update based on push type
3 participants