Skip to content

Commit 66d713d

Browse files
committed
handle add push notification
1 parent 560c432 commit 66d713d

File tree

8 files changed

+93
-28
lines changed

8 files changed

+93
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"merge-graphql-schemas": "^1.3.0",
3333
"mongoose": "^5.0.3",
3434
"node-fetch": "^2.0.0",
35+
"node-gcm": "^0.14.10",
3536
"passport": "^0.4.0",
3637
"passport-jwt": "^4.0.0",
3738
"prettier-eslint": "^8.2.0",

src/graphql/resolvers/Notification.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ export default {
55

66
Mutation: {
77
addToken: async (parent, { token, os }, { user }) => {
8-
console.log('x');
9-
console.log(` ### addToken ### , Token: "${token}, os: "${os}"`);
10-
console.log('y');
8+
if (!user.pushTokens.some(t => t.token === token)) {
9+
user.pushTokens.push({ token, os });
10+
user.save();
11+
}
12+
return {
13+
succeeded: true,
14+
};
1115
},
1216
},
1317
};

src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import webhook from './scripts/webhook';
1616
// import importAll from './scripts/importAll';
1717

1818
import auth from './express/auth';
19-
import './services/notifications/apn';
19+
import pushNotify from './services/notifications';
2020

2121
// Models
2222
import ProcedureModel from './models/Procedure';
@@ -87,6 +87,19 @@ app.post('/webhooks/bundestagio/update', async (req, res) => {
8787
}
8888
});
8989

90+
app.get('/push-test', async (req, res) => {
91+
const { message } = req.query;
92+
const users = await UserModel.find();
93+
users.forEach((user) => {
94+
pushNotify({
95+
title: 'test',
96+
message: message || 'Test push notification to all users',
97+
user,
98+
});
99+
});
100+
res.send("push's send");
101+
});
102+
90103
// Darf in Production nicht ausführbar sein!
91104
// app.get('/webhooks/bundestagio/import-all', importAll);
92105

src/models/User.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import jwt from 'jsonwebtoken';
55
const UserSchema = new Schema(
66
{
77
deviceHash: { type: String, required: true, unique: true },
8+
pushTokens: [
9+
{
10+
token: String,
11+
os: String,
12+
},
13+
],
814
},
915
{ timestamps: true },
1016
);

src/services/notifications/apn.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
import apn from 'apn';
22

3-
const options = {
4-
token: {
5-
key: process.env.APPLE_APN_KEY,
6-
keyId: process.env.APPLE_APN_KEY_ID,
7-
teamId: process.env.APPLE_TEAMID,
8-
},
9-
production: false,
10-
};
3+
let apnProvider; // eslint-disable-line
114

12-
const apnProvider = new apn.Provider(options);
5+
if (!apnProvider) {
6+
const options = {
7+
token: {
8+
key: process.env.APPLE_APN_KEY,
9+
keyId: process.env.APPLE_APN_KEY_ID,
10+
teamId: process.env.APPLE_TEAMID,
11+
},
12+
production: false,
13+
};
1314

14-
const deviceToken = 'c6a966484866d2d274d4660e9e593ccbc35636894eb08e2b1fb4070396bcaad5';
15-
16-
const note = new apn.Notification();
17-
18-
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
19-
note.badge = 3;
20-
note.sound = 'ping.aiff';
21-
note.alert = '\uD83D\uDCE7 \u2709 You have a new message';
22-
note.payload = { messageFrom: 'John Appleseed' };
23-
note.topic = 'de.democracy-deutschland.clientapp';
15+
apnProvider = new apn.Provider(options);
16+
}
2417

2518
export default apnProvider;
26-
27-
apnProvider.send(note, deviceToken).then((result) => {
28-
console.log('apnProvider.send', result);
29-
});

src/services/notifications/gcm.js

Whitespace-only changes.

src/services/notifications/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* eslint no-underscore-dangle: ["error", { "allow": ["_id"] }] */
2+
3+
import _ from 'lodash';
4+
import apn from 'apn';
5+
6+
import apnProvider from './apn';
7+
import gcmProvicer from './gcm';
8+
import UserModel from '../../models/User';
9+
10+
export default async ({ message, user }) => {
11+
let userId;
12+
if (_.isObject(user)) {
13+
userId = user._id;
14+
}
15+
const userObj = await UserModel.findById(userId);
16+
if (userObj) {
17+
userObj.pushTokens.forEach(({ token, os }) => {
18+
switch (os) {
19+
case 'ios':
20+
{
21+
const note = new apn.Notification();
22+
23+
note.alert = message;
24+
// note.payload = { messageFrom: 'John Appleseed' };
25+
note.topic = 'de.democracy-deutschland.clientapp';
26+
27+
apnProvider.send(note, token).then((result) => {
28+
console.log('apnProvider.send', result);
29+
});
30+
}
31+
break;
32+
33+
// Hier android und checken ob der case identifier korrekt ist.
34+
case 'android':
35+
// gcm
36+
37+
break;
38+
39+
default:
40+
break;
41+
}
42+
});
43+
}
44+
};

yarn.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2848,7 +2848,7 @@ [email protected]:
28482848
version "4.0.1"
28492849
resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.1.tgz#bf2249886ce514cda112fae9218cdc065211fc9c"
28502850

2851-
2851+
[email protected], lodash@^3.10.1:
28522852
version "3.10.1"
28532853
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
28542854

@@ -3106,6 +3106,14 @@ node-forge@^0.7.1:
31063106
version "0.7.4"
31073107
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.4.tgz#8e6e9f563a1e32213aa7508cded22aa791dbf986"
31083108

3109+
node-gcm@^0.14.10:
3110+
version "0.14.10"
3111+
resolved "https://registry.yarnpkg.com/node-gcm/-/node-gcm-0.14.10.tgz#fd1e3357dab1c62115e6e2d342ac249fe05e7360"
3112+
dependencies:
3113+
debug "^3.1.0"
3114+
lodash "^3.10.1"
3115+
request "2.81.0"
3116+
31093117
node-pre-gyp@^0.6.39:
31103118
version "0.6.39"
31113119
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.39.tgz#c00e96860b23c0e1420ac7befc5044e1d78d8649"

0 commit comments

Comments
 (0)