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

feat: Add the ability to manage channels and send broadcasts #164

Open
wants to merge 80 commits into
base: master
Choose a base branch
from

Conversation

cbaker6
Copy link

@cbaker6 cbaker6 commented Dec 30, 2024

Issue

Continuation of #163 which allowed the additional properties needed for broadcast notifications, but doesn't modify the Client API to GET, POST, DELETE to the respective Apple paths.

Closes: #162
Closes: #146
Closes: #111
Closes: #116

Approach

Follow the documentation related to channel management and broadcasting. This enables the functionality described in Apple's WWDC 2024 videos: Broadcast Updates to Your Live Activities.

The newly added API methods to node-apn will have the following footprint (TypeScript):

export class Provider extends EventEmitter {
  /**
   * Manage channels using a specific action.
   *
   * @param notifications - A Notification or an Array of Notifications to send. Each notification should specify the respective channelId it's directed to.
   * @param bundleId - The bundleId for your application.
   * @param action - Specifies the action to perform on the channel(s).
   */
  manageChannels(notifications: Notification|Notification[], bundleId: string, action: ChannelAction): Promise<Responses<BroadcastResponse,BroadcastResponseFailure>>;

  /**
   * Broadcast notificaitons to channel(s).
   *
   * @param notifications - A Notification or an Array of Notifications to send. Each notification should specify the respective channelId it's directed to.
   * @param bundleId: The bundleId for your application.
   */
  broadcast(notifications: Notification|Notification[], bundleId: string): Promise<Responses<BroadcastResponse,BroadcastResponseFailure>>;
}

export type ChannelAction = 'create' | 'read' | 'readAll' | 'delete';

export interface ResponseSent {
  device: string;
}

export interface BroadcastResponse {
  bundleId: string;
  "apns-request-id"?: string;
  "apns-channel-id"?: string;
  "message-storage-policy"?: number;
  "push-type"?: string;
  "channels"?: string[];
}

export interface LoggerResponse extends Partial<ResponseSent>, Partial<BroadcastResponse> {}

export interface ResponseFailure {
  device: string;
  error?: Error;
  status?: number;
  response?: {
    reason: string;
    timestamp?: string;
  };
}

export interface BroadcastResponseFailure extends Omit<ResponseFailure, "device"> {
  bundleId: string;
}

export interface LoggerResponseFailure extends Partial<ResponseFailure>, Partial<BroadcastResponseFailure> {}

export interface Responses<R,F> {
  sent: R[];
  failed: F[];
}

Non-Breaking Changes

All changes are non-breaking as they are made to the client API, which isn't exposed to developers, and new methods that previously weren't available are added. The typescript changes are improvements but are non-breaking as well.

Additionally, the PR does the following:

  • Updates the majority of code to async/await and error throwing, leaving native promises where necessary
  • Modernize code and update TS to match latest code
  • Improve unit tests to capture networking errors better
  • Document new API's

Copy link

parse-github-assistant bot commented Dec 30, 2024

Thanks for opening this pull request!

@cbaker6 cbaker6 marked this pull request as draft December 30, 2024 01:53
@cbaker6 cbaker6 changed the title feat: Add the ability to use different http methods feat: Add the ability to setup and send broadcast notifications Dec 31, 2024
@cbaker6 cbaker6 changed the title feat: Add the ability to setup and send broadcast notifications feat: Add the ability to manage and send broadcast notifications Dec 31, 2024
@cbaker6 cbaker6 closed this Dec 31, 2024
@cbaker6 cbaker6 reopened this Dec 31, 2024
Copy link

codecov bot commented Jan 1, 2025

Codecov Report

Attention: Patch coverage is 94.53376% with 17 lines in your changes missing coverage. Please review.

Project coverage is 95.79%. Comparing base (4eb3686) to head (0482ac3).
Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
lib/client.js 92.01% 17 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #164      +/-   ##
==========================================
+ Coverage   93.53%   95.79%   +2.25%     
==========================================
  Files          23       23              
  Lines         588      832     +244     
==========================================
+ Hits          550      797     +247     
+ Misses         38       35       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cbaker6
Copy link
Author

cbaker6 commented Jan 2, 2025

@mtrezza and @dplewis, can you do an initial review of this PR when you have a chance? To enable broadcasting, a larger amount of code changes are needed than normal, in particular because client.js hasn't been updated in 3+ years, was designed for original style push notifications (can only POST to a single path), and was based purely on promise style as opposed to async/await.

I have slowly integrated the changes the client needs to support broadcasting, and hopefully, it will be future-proofed so changes can be added more easily with less code. The test cases have been updated with the async/await code, but all of the original tests still ensure their expected results.

I still have test cases to add, but I don't expect many changes to client.js and provider.js, with the exception of the new test cases catching bugs I introduced. I'll also add a self-review to explain some of my thinking regarding certain changes.

To help guide you through the code changes (again, my apologies for the large PR), I recommend the following:

  1. Go through the description of the PR update here: feat: Add the ability to manage channels and send broadcasts #164 (comment)
  2. Review changes in lib/config.js
  3. Review tests added to test/config.js
  4. Review changes in index.d.ts
  5. Review changes in lib/provider.js
  6. Review changes in test/provider.js
  7. Review changes in lib/proxy.js
  8. Review changes in lib/client.js (the largest changes and needed restructuring)
  9. Review changes in test/client.js (also a large number of updates, but original tests report original results. Also includes tests to verify new features added)
  10. The multiclient stuff, but those behave much like the original client

I am open to any feedback/suggestions...

@cbaker6
Copy link
Author

cbaker6 commented Jan 14, 2025

@mtrezza the code for this is finished and ready for a full review.

@@ -384,13 +384,6 @@ module.exports = function (dependencies) {
this.destroySession(session);
});

this.session.on('socketError', error => {
Copy link
Author

Choose a reason for hiding this comment

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

I removed this as it's no longer available in the older version of the node tested (version 14 LTS). The last time I see it is in node version 8.7.0.

I'm sure whatever it use to catch is still caught by the rest of the error handling in this promise

@cbaker6 cbaker6 requested a review from mtrezza January 16, 2025 04:32
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