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 64 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
b35fbb8
add TS missing properties
cbaker6 Dec 29, 2024
71f2cf1
feat: Add the ability to use different http methods
cbaker6 Dec 30, 2024
0b4022f
Merge branch 'master' into useBroadcastPath
cbaker6 Dec 30, 2024
49b5bf5
lint
cbaker6 Dec 30, 2024
5e83b2a
Merge branch 'useBroadcastPath' of https://github.com/cbaker6/node-ap…
cbaker6 Dec 30, 2024
e5bf411
test v2 as provider
cbaker6 Dec 30, 2024
5b029b7
replace original client with V2
cbaker6 Dec 30, 2024
755aeec
lint
cbaker6 Dec 30, 2024
058070c
create manageBroadcastSession
cbaker6 Dec 31, 2024
8136a8c
nit
cbaker6 Dec 31, 2024
3d348fc
nit
cbaker6 Dec 31, 2024
a1263c7
add channels method
cbaker6 Dec 31, 2024
36b3f35
nit
cbaker6 Dec 31, 2024
8ec017d
update typescript
cbaker6 Dec 31, 2024
53393f0
make JS match TS
cbaker6 Jan 1, 2025
6cf1299
delete non channel related headers when necessary
cbaker6 Jan 1, 2025
5193a14
Merge branch 'master' into useBroadcastPath
cbaker6 Jan 1, 2025
c21e682
use async/await
cbaker6 Jan 2, 2025
b7a5bfe
Merge branch 'useBroadcastPath' of https://github.com/cbaker6/node-ap…
cbaker6 Jan 2, 2025
dee2882
lint
cbaker6 Jan 2, 2025
576a62d
fix code on older versions of node
cbaker6 Jan 2, 2025
d006ded
add initial tests for manageBroadcastSession
cbaker6 Jan 2, 2025
6318b98
throw error if reached connectionRetryLimit
cbaker6 Jan 2, 2025
bcd1364
improve usage of connectionRetryLimit, defaults to 2 instead of 10
cbaker6 Jan 2, 2025
446c8f2
manageBroadcast -> manageChannels
cbaker6 Jan 2, 2025
2f814c9
make code for connectionRetryLimit match documentation
cbaker6 Jan 2, 2025
b280af9
convert provider tests to async/await
cbaker6 Jan 4, 2025
551e686
lint
cbaker6 Jan 4, 2025
46c3df2
add more provider tests
cbaker6 Jan 4, 2025
7202f41
add more notification tests
cbaker6 Jan 4, 2025
58e6a86
retry on http codes 408, 429, 500, 502, 503, 504
cbaker6 Jan 4, 2025
56d7337
add proxy test
cbaker6 Jan 4, 2025
4905239
add some client tests
cbaker6 Jan 5, 2025
e0bb26d
fix manage channels test
cbaker6 Jan 10, 2025
5519fa4
more tests
cbaker6 Jan 10, 2025
2f2027d
improve logging pings
cbaker6 Jan 10, 2025
8b75d4e
revert
cbaker6 Jan 10, 2025
db39674
add ping tests
cbaker6 Jan 11, 2025
12d7968
more logger coverage
cbaker6 Jan 11, 2025
84a0736
modify tests for older node
cbaker6 Jan 11, 2025
b6c149b
remove unnecessary check
cbaker6 Jan 11, 2025
a561daa
make shutdown async
cbaker6 Jan 13, 2025
4ddf509
lint
cbaker6 Jan 13, 2025
af39226
test unref
cbaker6 Jan 13, 2025
77f117d
try without JSON error test
cbaker6 Jan 13, 2025
854338e
xit some tests for now
cbaker6 Jan 13, 2025
07a8afe
modify logger tests to run on older node
cbaker6 Jan 13, 2025
b092680
run json tests
cbaker6 Jan 13, 2025
5f8f975
lint
cbaker6 Jan 13, 2025
8a926c3
test
cbaker6 Jan 13, 2025
694b04d
remove close connection tests
cbaker6 Jan 13, 2025
39027cf
don't retry on status 500
cbaker6 Jan 13, 2025
d4da764
pass reject in closure
cbaker6 Jan 13, 2025
55adff1
force proxy to close
cbaker6 Jan 13, 2025
7727d1e
nit
cbaker6 Jan 13, 2025
85bedf6
improve 500 errors
cbaker6 Jan 13, 2025
1efd838
more tests
cbaker6 Jan 14, 2025
20fc514
reduce use of callbacks
cbaker6 Jan 14, 2025
c04aa4e
add back proxy test
cbaker6 Jan 14, 2025
3c05075
fix manage channel proxy test
cbaker6 Jan 14, 2025
14e50f1
comment nits
cbaker6 Jan 14, 2025
354242d
increase coverage
cbaker6 Jan 14, 2025
065e88c
add destroySession test
cbaker6 Jan 14, 2025
4445430
nit
cbaker6 Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/provider.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ When the returned `Promise` resolves, its value will be an Object containing two

An array of device tokens to which the notification was successfully sent and accepted by Apple.

Being `sent` does **not** guaranteed the notification will be _delivered_, other unpredictable factors - including whether the device is reachable - can ultimately prevent delivery.
Being `sent` does **not** guarantee the notification will be _delivered_, other unpredictable factors - including whether the device is reachable - can ultimately prevent delivery.

#### failed

Expand Down
100 changes: 79 additions & 21 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,34 @@ interface Aps {
"mutable-content"?: undefined | 1
"url-args"?: string[]
category?: string
"thread-id"?: string
"interruption-level"?: string
"relevance-score"?: number
"filter-criteria"?: string
"stale-date"?: number
"content-state"?: Object
timestamp?: number
event?: string
"dismissal-date"?: number
"attributes-type"?: string
attributes?: Object
}

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;
Expand All @@ -114,28 +137,56 @@ export interface ResponseFailure {
};
}

export interface Responses {
sent: ResponseSent[];
failed: ResponseFailure[];
export interface BroadcastResponseFailure extends Omit<ResponseFailure, "device"> {
bundleId: string;
}

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

export interface Responses<R,F> {
Copy link
Author

Choose a reason for hiding this comment

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

I used generics here to:

  1. Keep the original responses and not break code
  2. Write less code for each type

I attempted to write this in a way to still work out-of-the-box with no breaking, but I'm not a TS expert, so feel free to chime in if you see an issue.

sent: R[];
failed: F[];
}

export class Provider extends EventEmitter {
constructor(options: ProviderOptions);
/**
* This is main interface for sending notifications. Create a Notification object and pass it in, along with a single recipient or an array of them and node-apn will take care of the rest, delivering a copy of the notification to each recipient.
* This is main interface for sending notifications.
*
* @remarks
* Create a Notification object and pass it in, along with a single recipient or an array of them and node-apn will take care of the rest, delivering a copy of the notification to each recipient.
*
* @param notification - The notification to send.
* @param recipients - A String or an Array of Strings containing the hex-encoded device token.
*/
send(notification: Notification, recipients: string|string[]): Promise<Responses<ResponseSent,ResponseFailure>>;

/**
* 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).
*
* A "recipient" is a String containing the hex-encoded device token.
* @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.
*/
send(notification: Notification, recipients: string|string[]): Promise<Responses>;
broadcast(notifications: Notification|Notification[], bundleId: string): Promise<Responses<BroadcastResponse,BroadcastResponseFailure>>;

/**
* Set an info logger, and optionally an errorLogger to separately log errors.
*
* @remarks
* In order to log, these functions must have a property '.enabled' that is true.
* (The default logger uses the npm 'debug' module which sets '.enabled'
* based on the DEBUG environment variable)
*/
setLogger(logger: (msg: string) => void, errorLogger?: (msg: string) => void): Promise<Responses>;
setLogger(logger: (msg: string) => void, errorLogger?: (msg: string) => void): Promise<Responses<LoggerResponse,LoggerResponseFailure>>;

/**
* Indicate to node-apn that it should close all open connections when the queue of pending notifications is fully drained. This will allow your application to terminate.
Expand All @@ -146,28 +197,35 @@ export class Provider extends EventEmitter {
export class MultiProvider extends EventEmitter {
constructor(options: MultiProviderOptions);
/**
* This is main interface for sending notifications. Create a Notification object and pass it in, along with a single recipient or an array of them and node-apn will take care of the rest, delivering a copy of the notification to each recipient.
* This is main interface for sending notifications.
*
* @remarks
* Create a Notification object and pass it in, along with a single recipient or an array of them and node-apn will take care of the rest, delivering a copy of the notification to each recipient.
*
* A "recipient" is a String containing the hex-encoded device token.
* @param notification - The notification to send.
* @param recipients - A String or an Array of Strings containing the hex-encoded device token.
*/
send(notification: Notification, recipients: string|string[]): Promise<Responses>;
send(notification: Notification, recipients: string|string[]): Promise<Responses<ResponseSent,ResponseFailure>>;

/**
* Set an info logger, and optionally an errorLogger to separately log errors.
*
* @remarks
* In order to log, these functions must have a property '.enabled' that is true.
* (The default logger uses the npm 'debug' module which sets '.enabled'
* (The default logger uses the npm 'debug' module which sets '.enabled'
* based on the DEBUG environment variable)
*/
setLogger(logger: (msg: string) => void, errorLogger?: (msg: string) => void): Promise<Responses>;
setLogger(logger: (msg: string) => void, errorLogger?: (msg: string) => void): Promise<Responses<LoggerResponse,LoggerResponseFailure>>;

/**
* Indicate to node-apn that it should close all open connections when the queue of pending notifications is fully drained. This will allow your application to terminate.
*/
shutdown(callback?: () => void): void;
}

export type NotificationPushType = 'background' | 'alert' | 'voip' | 'pushtotalk' | 'liveactivity';
export type NotificationPushType = 'background' | 'alert' | 'voip' | 'pushtotalk' | 'liveactivity' | 'location' | 'complication' | 'fileprovider' | 'mdm';
Copy link
Author

Choose a reason for hiding this comment

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

Adding location closes #111 and the others enable the rest of the type of notifications.


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

export interface NotificationAlertOptions {
title?: string;
Expand Down Expand Up @@ -198,21 +256,22 @@ export class Notification {
*/
public id: string;
/**
* The UNIX timestamp representing when the notification should expire. This does not contribute to the 2048 byte payload size limit. An expiry of 0 indicates that the notification expires immediately.
* A UUID to identify this request.
*/
public expiry: number;
public requestId: string;
/**
* Multiple notifications with same collapse identifier are displayed to the user as a single notification. The value should not exceed 64 bytes.
* A base64-encoded string that identifies the channel to publish the payload.
The channel ID is generated by sending channel creation request to APNs.
*/
public collapseId: string;
public channelId: string;
/**
* Multiple notifications with same collapse identifier are displayed to the user as a single notification. The value should not exceed 64 bytes.
*/
public requestId: string;
public collapseId: string;
/**
* An optional custom request identifier that’s returned back in the response. The request identifier must be encoded as a UUID string.
* The UNIX timestamp representing when the notification should expire. This does not contribute to the 2048 byte payload size limit. An expiry of 0 indicates that the notification expires immediately.
*/
public channelId: string;
public expiry: number;
/**
* Provide one of the following values:
*
Expand All @@ -225,7 +284,6 @@ export class Notification {
* The type of the notification.
*/
public pushType: NotificationPushType;

/**
* An app-specific identifier for grouping related notifications.
*/
Expand Down
Loading
Loading