Skip to content

Instantly share code, notes, and snippets.

@adadgio
Created April 19, 2022 12:59
Show Gist options
  • Save adadgio/d7e2d91bb85a1c6d9187788f6f54afe7 to your computer and use it in GitHub Desktop.
Save adadgio/d7e2d91bb85a1c6d9187788f6f54afe7 to your computer and use it in GitHub Desktop.
Airship service TS
import ROOT_DIR from '../../rootdir'
require('dotenv').config({ path: `${ROOT_DIR}/.env` })
import axios, { AxiosResponse } from 'axios'
import { Notification, ios, android, openInPayload } from './notifications'
type NamedUser = {
named_user_id: string
attributes: any
channels: any[]
}
export default class Airship {
getBasicAuthorization(): string {
return `Basic ${Buffer.from(`${process.env.AIRSHIP_APP_KEY}:${process.env.AIRSHIP_MASTER_KEY}`).toString('base64')}`;
}
getHeaders() {
return {
'Authorization': this.getBasicAuthorization(),
'Accept': process.env.AIRSHIP_VERSION,
'Content-Type': 'application/json'
}
}
async getNamedUser(namedUserIdentifier: string, headers: any = {}): Promise<NamedUser> {
return new Promise((resolve, reject) => {
axios.get(`${process.env.AIRSHIP_BASEURL}/api/named_users/?id=${namedUserIdentifier}`, { headers: { ...this.getHeaders(), ...headers } })
.then((response: AxiosResponse) => {
if (response.data.ok !== true) {
reject('named user not found')
} else {
resolve(response.data.named_user)
}
}).catch(e => {
reject(e)
})
})
}
async sendNotification(namedUserIdentifier: string, notification: Notification): Promise<any> {
return new Promise((resolve, reject) => {
this.getNamedUser(namedUserIdentifier)
.then(user => {
const device_types = user.channels.map(({device_type}) => device_type).filter(device_type => device_type !== 'email')
this.pushNotification(
{
named_user: user.named_user_id,
},
notification,
[...new Set(device_types)]
).then(result => {
resolve(result)
}).catch(e => {
console.log(e.response.data)
reject(e)
})
}).catch(e => {
reject(e)
})
})
}
async pushNotification(audience: any, payload: Notification, device_types: string[], headers: any = {}): Promise<any> {
const airshipPayload = {
audience,
notification: {
android: android(payload),
ios: ios(payload),
actions: {
open: openInPayload(payload.navigation),
},
},
device_types,
}
return new Promise((resolve, reject) => {
axios.post(`${process.env.AIRSHIP_BASEURL}/api/push`, airshipPayload,
{
headers: {...this.getHeaders(), ...headers},
}).then((response: AxiosResponse) => {
resolve(response.data)
}).catch(e => {
reject(e)
})
})
}
};
export type Notification = {
title: string
body: string
navigation?: string
}
export function openInPayload(url: string) {
const deepLinkSchema = {
type: 'deep_link',
content: url,
}
const urlSchema = {
type: 'url',
content: url,
}
return url.includes('medics://') ? deepLinkSchema : urlSchema
}
export function ios(payload: Notification) {
return {
alert: {
title: payload.title,
body: payload.body,
},
}
}
export function android(payload: Notification) {
return {
title: payload.title,
alert: payload.body,
summary: payload.body,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment