Skip to content

Commit c181de5

Browse files
committed
send android push notifications
1 parent 980c4f0 commit c181de5

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/config/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export default {
66
GRAPHIQL_PATH: '/graphiql',
77
GRAPHQL_PATH: '/graphql',
88
BUNDESTAGIO_SERVER_URL: process.env.BUNDESTAGIO_SERVER_URL || 'http://localhost:3100/graphql',
9+
NOTIFICATION_ANDROID_SERVER_KEY: process.env.NOTIFICATION_ANDROID_SERVER_KEY || false,
910
};

src/services/notifications/gcm.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import gcm from 'node-gcm';
2+
import CONSTANTS from '../../config/constants';
3+
4+
let gcmProvider; // eslint-disable-line
5+
6+
if (!gcmProvider) {
7+
gcmProvider = new gcm.Sender(CONSTANTS.NOTIFICATION_ANDROID_SERVER_KEY);
8+
console.log('NOTIFICATION_ANDROID_SERVER_KEY', CONSTANTS.NOTIFICATION_ANDROID_SERVER_KEY);
9+
}
10+
11+
export default gcmProvider;

src/services/notifications/index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import _ from 'lodash';
44
import apn from 'apn';
5+
import gcm from 'node-gcm';
56

67
import apnProvider from './apn';
8+
import gcmProvider from './gcm';
79
import UserModel from '../../models/User';
810

911
export default async ({ message, user }) => {
@@ -13,6 +15,7 @@ export default async ({ message, user }) => {
1315
}
1416
const userObj = await UserModel.findById(userId);
1517
if (userObj) {
18+
const androidNotificationTokens = [];
1619
userObj.pushTokens.forEach(({ token, os }) => {
1720
switch (os) {
1821
case 'ios':
@@ -29,15 +32,30 @@ export default async ({ message, user }) => {
2932
}
3033
break;
3134

32-
// Hier android und checken ob der case identifier korrekt ist.
3335
case 'android':
34-
// gcm
35-
36+
// Prepare android notifications
37+
androidNotificationTokens.push(token);
3638
break;
3739

3840
default:
3941
break;
4042
}
4143
});
44+
// send bulk send android notifications
45+
if (androidNotificationTokens.length > 0) {
46+
const gcmMessage = new gcm.Message({
47+
notification: {
48+
body: message,
49+
},
50+
});
51+
gcmProvider.send(
52+
gcmMessage,
53+
{ registrationTokens: androidNotificationTokens },
54+
(err, response) => {
55+
if (err) console.error('gcmProvider', err);
56+
else console.log('gcmProvider', response);
57+
},
58+
);
59+
}
4260
}
4361
};

0 commit comments

Comments
 (0)