Skip to content

Instantly share code, notes, and snippets.

@thebigredgeek
Last active May 12, 2017 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebigredgeek/9c55f22c274a248bbb50ae95bf71af57 to your computer and use it in GitHub Desktop.
Save thebigredgeek/9c55f22c274a248bbb50ae95bf71af57 to your computer and use it in GitHub Desktop.
import { Permissions, Notifications } from 'expo';
import { Platform } from 'react-native';
import client from '../state/apollo';
import mutation from '../graphql/mutations/userAddPushToken';
import Observable from './observable';
export const register = async () => {
console.log('requesting push notification permissions');
const { status } = await Permissions.askAsync(Permissions.REMOTE_NOTIFICATIONS);
console.log(`push notification permissions: ${status}`);
if (status !== 'granted') {
return;
}
console.log('fetching push notifications token');
const token = await Notifications.getExponentPushTokenAsync();
console.log(`push notifications token: ${token}`);
const variables = {
input: {
token
}
};
console.log('flushing push notification token to server');
const { data: { user } } = await client.mutate({
mutation,
variables
});
console.log(`push notification token for user ${user.id} persisted on server`);
return user;
};
export const dismiss = id => Platform.OS === 'ios' ? Promise.resolve(id) : Notifications.dismissNotificationAsync(id);
export const observable = new Observable();
observable.subscribe(({ data }) => console.log(`received push notification with data ${JSON.stringify(data)}`));
Notifications.addListener(notification => {
console.log(notification);
observable.onNext(notification);
});
[exp] push notification permissions: granted
[exp] fetching push notifications token
[exp] push notifications token: ExponentPushToken[o4YLaOLeN9AjeKqEjlNob8]
[exp] flushing push notification token "ExponentPushToken[o4YLaOLeN9AjeKqEjlNob8]" to server
[exp] push notification token for user 15d04461-14ba-4770-86db-c8d0565cf010 persisted on server
import Promise from 'bluebird';
import { inject } from 'injectorator';
import _ from 'underscore';
import Expo from 'exponent-server-sdk';
import BaseConnector from './base';
const expo = new Expo();
@inject({
expo: () => expo,
Promise: () => Promise
})
export default class ExpoConnector extends BaseConnector {
constructor (
{ expo, Promise }
) {
super();
this._expo = expo;
this._Promise = Promise;
}
async sendPushNotifications(notifications = []) {
notifications = notifications
.filter(n => !!n)
.map(({
pushToken,
playSound,
title,
body,
data,
ttl,
expiration,
priority
}) => ({
to: pushToken,
sound: !!Boolean(playSound) ? 'default' : null,
title: title || null,
body: body || null,
data: data || {},
ttl: ttl || 0,
expiration: expiration || undefined,
priority: priority || 'normal'
}))
.filter(({ to, body }) => !!to && !!body)
const receipts = await this._expo.sendPushNotificationsAsync(notifications);
console.log(receipts);
}
sendPushNotification(notification) {
return this.sendPushNotifications([ notification ]);
}
}
async addPushToken (user, token, transactionContext) {
const transaction = transactionContext ? transactionContext.transaction : null;
user = this.isInstance(user) ? user : await this.findById(user.id || user, transactionContext);
log('adding push token "%s" for user "%s"', token, user.id);
await user.update({
pushTokens: [
...user.pushTokens,
token
]
}, {
transaction
});
await user.save({
transaction
});
log('added push token "%s" for user "%s": %O', token, user.id, user.pushTokens);
log('sending initial push notification to user "%s"', user.id);
setTimeout(() => {
this._expoConnector.sendPushNotification({
pushToken: token,
title: 'Welcome to Boost',
body: 'Thanks for joining Boost! We look forward to helping you with your career. :)',
data: {
userId: user.id
}
});
}, 5000);
return user;
}
[1] Fri, 12 May 2017 23:35:52 GMT graphql:models:user adding push token "ExponentPushToken[o4YLaOLeN9AjeKqEjlNob8]" for user "15d04461-14ba-4770-86db-c8d0565cf010"
[1] Fri, 12 May 2017 23:35:52 GMT graphql:models:user added push token "ExponentPushToken[o4YLaOLeN9AjeKqEjlNob8]" for user "15d04461-14ba-4770-86db-c8d0565cf010": [ 'ExponentPushToken[7g2bg7HtFL4LXsRI-HZgpe]',
[1] 'ExponentPushToken[NT6ktiOvwt63o8SHOX4sYg]',
[1] 'ExponentPushToken[s2RFpGFYOZ9Vh9O5-yrv7r]',
[1] 'ExponentPushToken[F4kc6_MODNiLfP7gEHkyZb]',
[1] 'ExponentPushToken[ooMroRDxMvvGAD-q6bBv1O]',
[1] 'ExponentPushToken[MvnrUTGVWkIcwAoxBlgApj]',
[1] 'ExponentPushToken[sNmGiBPXm5KJYvHZGYwLiy]',
[1] 'ExponentPushToken[gz-9hcAFu678NRrgjZHE85]',
[1] 'ExponentPushToken[_XDmxpOqBAOwr2Vmsmfz9A]',
[1] 'ExponentPushToken[xSwZ4sNq6v39KdE1_TD-DP]',
[1] 'ExponentPushToken[xSwZ4sNq6v39KdE1_TD-DP]',
[1] 'ExponentPushToken[o4YLaOLeN9AjeKqEjlNob8]' ]
[1] Fri, 12 May 2017 23:35:52 GMT graphql:models:user sending initial push notification to user "15d04461-14ba-4770-86db-c8d0565cf010"
[1] Fri, 12 May 2017 23:35:52 GMT graphql:middleware:context request finished for user 15d04461-14ba-4770-86db-c8d0565cf010 - 55 ms
[1] [
[1] {
[1] "to": "ExponentPushToken[o4YLaOLeN9AjeKqEjlNob8]",
[1] "sound": "default",
[1] "title": "Welcome to Boost",
[1] "body": "Thanks for joining Boost! We look forward to helping you with your career. :)",
[1] "data": {
[1] "userId": "15d04461-14ba-4770-86db-c8d0565cf010"
[1] },
[1] "ttl": 0,
[1] "priority": "normal"
[1] }
[1] ]
[1] [ { status: 'ok' } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment