Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
66 changes: 57 additions & 9 deletions app/expo.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import Expo from 'expo-server-sdk';
const { Expo } = require('expo-server-sdk')

// Create a new Expo SDK client

let sendNotification = (tokens) => {
for (let pushToken of tokens) {

// Create the messages that you want to send to clents
let messages = [];

module.exports.sendNotification = (tokens, title, body, data) => {
let expo = new Expo();
for (let pushToken of tokens) {
// Each push token looks like ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]
let token = pushToken.pushToken;
// Check that all your push tokens appear to be valid Expo push tokens
if (!Expo.isExpoPushToken(pushToken)) {
console.error(`Push token ${pushToken} is not a valid Expo push token`);
if (!Expo.isExpoPushToken(token)) {
console.error(`Push token ${token} is not a valid Expo push token`);
continue;
}

// Construct a message (see https://docs.expo.io/versions/latest/guides/push-notifications.html)
messages.push({
to: pushToken,
to: token,
sound: 'default',
body: 'This is a test notification',
data: { withSome: 'data' },
title: title,
body: body,
data: data,
})
}

Expand Down Expand Up @@ -43,4 +52,43 @@ let sendNotification = (tokens) => {
}
}
})();
}

let receiptIds = [];
for (let ticket of tickets) {
// NOTE: Not all tickets have IDs; for example, tickets for notifications
// that could not be enqueued will have error information and no receipt ID.
if (ticket.id) {
receiptIds.push(ticket.id);
}
}

let receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
(async () => {
// Like sending notifications, there are different strategies you could use
// to retrieve batches of receipts from the Expo service.
for (let chunk of receiptIdChunks) {
try {
let receipts = await expo.getPushNotificationReceiptsAsync(chunk);
console.log(receipts);

// The receipts specify whether Apple or Google successfully received the
// notification and information about an error, if one occurred.
for (let receipt of receipts) {
if (receipt.status === 'ok') {
continue;
} else if (receipt.status === 'error') {
console.error(`There was an error sending a notification: ${receipt.message}`);
if (receipt.details && receipt.details.error) {
// The error codes are listed in the Expo documentation:
// https://docs.expo.io/versions/latest/guides/push-notifications#response-format
// You must handle the errors appropriately.
console.error(`The error code is ${receipt.details.error}`);
}
}
}
} catch (error) {
console.error(error);
}
}
})();
};
8 changes: 8 additions & 0 deletions app/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ module.exports.findById = function (userId, callback) {

module.exports.getByColor = function (color, callback) {
User.find({color: color}, callback);
};

module.exports.getAllTokens = function (callback) {
User.find({}, 'pushToken', callback);
};

module.exports.getAllTokens = function (color, callback) {
User.find({color: color}, 'pushToken', callback);
};
12 changes: 12 additions & 0 deletions app/routes/userRoute.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var User = require('../models/userModel');
var colorTool = require('../colors/groupsingleton');
var lobbyTool = require('../lobbies/lobbyController');
var expo = require('../expo');

module.exports = function(router) {

Expand Down Expand Up @@ -41,6 +42,17 @@ module.exports = function(router) {
});
});

router.post('/notification', function (req, res) {
User.getAllTokens(function(err, tokens) {
if (err){
console.log(err);
}else {
expo.sendNotification(tokens, "The game is on!", "Open the app to get your first assignment", {gameStart: true})
}
});

});

router.put('/isPresent/:userId', function (req, res) {
User.findById(req.params.userId, function (err, user) {
console.log("TTTTTTTTere");
Expand Down
26 changes: 0 additions & 26 deletions fcm.js

This file was deleted.

3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const bodyParser = require('body-parser');
const app = express();



mongoose.connect('mongodb://meating:maatwerk1@ds135844.mlab.com:35844/ic');
const db = mongoose.connection;

Expand All @@ -14,6 +15,8 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }))
require('./app/routes')(app);



app.listen(port, () => {
console.log('We are live on ' + port);
});