Skip to content

feat: Add support for Expo push notifications #243

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

Merged
merged 9 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The official Push Notification adapter for Parse Server. See [Parse Server Push
- [Using a Custom Version on Parse Server](#using-a-custom-version-on-parse-server)
- [Install Push Adapter](#install-push-adapter)
- [Configure Parse Server](#configure-parse-server)
- [Expo Push Options](#expo-push-options)

# Silent Notifications

Expand Down Expand Up @@ -57,16 +58,32 @@ const parseServerOptions = {
push: {
adapter: new PushAdapter({
ios: {
/* Apple push notification options */
/* Apple push options */
},
android: {
/* Android push options */
}
},
web: {
/* Web push options */
}
})
},
expo: {
/* Expo push options */
},
}),
},
/* Other Parse Server options */
}
```

### Expo Push Options

Example options:

```js
expo: {
accessToken: '<EXPO_ACCESS_TOKEN>',
},
```

For more information see the [Expo docs](https://docs.expo.dev/push-notifications/overview/).

95 changes: 82 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@parse/node-apn": "6.0.1",
"@parse/node-gcm": "1.0.2",
"expo-server-sdk": "3.10.0",
"firebase-admin": "12.1.0",
"npmlog": "7.0.1",
"parse": "5.0.0",
Expand Down
91 changes: 91 additions & 0 deletions src/EXPO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"use strict";

import Parse from 'parse';
import log from 'npmlog';
import { Expo, ExpoPushMessage, ExpoPushTicket } from 'expo-server-sdk';

const LOG_PREFIX = 'parse-server-push-adapter EXPO';

export class EXPO {
expo = undefined;
/**
* Create a new EXPO push adapter. Based on Web Adapter.
*
* @param {Object} args https://github.com/expo/expo-server-sdk-node / https://docs.expo.dev/push-notifications/sending-notifications/
*/
constructor(args) {
if (typeof args !== 'object') {
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED, 'EXPO Push Configuration is invalid');
}

this.expo = new Expo(args)
this.options = args;
}

/**
* Send Expo push notification request.
*
* @param {Object} data The data we need to send, the format is the same with api request body
* @param {Array} devices An array of devices
* @returns {Object} A promise which is resolved immediately
*/
async send(data, devices) {
const coreData = data && data.data;

if (!coreData || !devices || !Array.isArray(devices)) {
log.warn(LOG_PREFIX, 'invalid push payload');
return;
}
const devicesMap = devices.reduce((memo, device) => {
memo[device.deviceToken] = device;
return memo;
}, {});
const deviceTokens = Object.keys(devicesMap);

const resolvers = [];
const promises = deviceTokens.map(() => new Promise(resolve => resolvers.push(resolve)));
let length = deviceTokens.length;

log.verbose(LOG_PREFIX, `sending to ${length} ${length > 1 ? 'devices' : 'device'}`);

const response = await this.sendNotifications(coreData, deviceTokens, this.options);

log.verbose(LOG_PREFIX, `EXPO Response: %d sent`, response.length);

deviceTokens.forEach((token, index) => {
const resolve = resolvers[index];
const result = response[index];
const device = devicesMap[token];
const resolution = {
transmitted: result.status === 'ok',
device: {
deviceToken: token
},
response: result,
};
resolve(resolution);
});
return Promise.all(promises);
}

/**
* Send multiple Expo push notification request.
*
* @param {Object} payload The data we need to send, the format is the same with api request body
* @param {Array} deviceTokens An array of devicesTokens
* @param {Object} options The options for the request
* @returns {Object} A promise which is resolved immediately
*/
async sendNotifications({alert, title, body, ...payload}, deviceTokens, options) {
const messages = deviceTokens.map((token) => ({
to: token,
title: title,
body: body || alert,
...payload
}));

return await this.expo.sendPushNotificationsAsync(messages);
}
}

export default EXPO;
6 changes: 5 additions & 1 deletion src/ParsePushAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import APNS from './APNS';
import GCM from './GCM';
import FCM from './FCM';
import WEB from './WEB';
import EXPO from './EXPO';
import { classifyInstallations } from './PushAdapterUtils';

const LOG_PREFIX = 'parse-server-push-adapter';
Expand All @@ -14,7 +15,7 @@ export default class ParsePushAdapter {
supportsPushTracking = true;

constructor(pushConfig = {}) {
this.validPushTypes = ['ios', 'osx', 'tvos', 'android', 'fcm', 'web'];
this.validPushTypes = ['ios', 'osx', 'tvos', 'android', 'fcm', 'web', 'expo'];
this.senderMap = {};
// used in PushController for Dashboard Features
this.feature = {
Expand All @@ -41,6 +42,9 @@ export default class ParsePushAdapter {
case 'web':
this.senderMap[pushType] = new WEB(pushConfig[pushType]);
break;
case 'expo':
this.senderMap[pushType] = new EXPO(pushConfig[pushType]);
break;
case 'android':
case 'fcm':
if (pushConfig[pushType].hasOwnProperty('firebaseServiceAccount')) {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import ParsePushAdapter from './ParsePushAdapter';
import GCM from './GCM';
import APNS from './APNS';
import WEB from './WEB';
import EXPO from './EXPO';
import * as utils from './PushAdapterUtils';

export default ParsePushAdapter;
export { ParsePushAdapter, APNS, GCM, WEB, utils };
export { ParsePushAdapter, APNS, GCM, WEB, EXPO, utils };
Loading