Skip to content

Commit

Permalink
Notification service is optional, on click deploy setup added
Browse files Browse the repository at this point in the history
  • Loading branch information
agraebe committed Aug 30, 2019
1 parent c352402 commit 232da8b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
16 changes: 16 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
"HT_SECRET_KEY": {
"description": "Your HyperTrack SecretKey from https://dashboard.hypertrack.com/setup",
"value": ""
},
"FCM_KEY": {
"description": "Push Notifications (optional): Your Firebase Cloud Messaging Key, see more here: https://github.com/hypertrack/quickstart-android#enable-server-to-device-communication",
"value": ""
},
"APN_KEY_ID": {
"description": "Push Notifications (optional): Your Apple Push Notification (APN) Key ID, see more here: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token-based_connection_to_apns",
"value": ""
},
"APN_CERT": {
"description": "Push Notifications (optional): Your Apple Push Notification (APN) authentication token signing key. Paste *.p8 file contents in the field. See more here: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token-based_connection_to_apns",
"value": ""
},
"APN_TEAM_ID": {
"description": "Push Notifications (optional): Your Apple Developer account Team ID, see more here: https://www.mobiloud.com/help/knowledge-base/ios-app-transfer/",
"value": ""
}
},
"repository": "https://github.com/hypertrack/sample-backend-nodejs",
Expand Down
81 changes: 49 additions & 32 deletions controllers/push-notification.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ const push = new PushService({
});

exports.sendNotification = (device_id, payload, res) => {
// continue only if all keys are set
if (
process.env.FCM_KEY !== "" &&
process.env.APN_CERT !== "" &&
process.env.APN_KEY_ID !== "" &&
process.env.APN_TEAM_ID !== ""
) {
if (res) {
res.status(500).send({
message: `Push notifications are not configured on the backend`
});
}

return;
}

// lookup device push first
pushInfo.findOne({ device_id }).then(pushInfo => {
if (pushInfo) {
Expand All @@ -43,45 +59,46 @@ exports.sendNotification = (device_id, payload, res) => {
title = "Trip geofence enter";
}

// send push notification
push
.send([pushInfo.push_token], {
title, // REQUIRED for Android
topic: pushInfo.app_name, // REQUIRED for iOS
body,
contentAvailable: true,
custom: payload
})
.then(results => {
if (results) {
const responseText = `Push notification for id '${
pushInfo.push_token
}' and app '${pushInfo.app_name}' stored. Notification push ${
_.get(results, "[0].success", 0) === 1 ? "succeded" : "failed"
}`;
// store notification record
PushNotification.create({ ids: device_id, payload })
.then(() => {
// send push notification
push
.send([pushInfo.push_token], {
title, // REQUIRED for Android
topic: pushInfo.app_name, // REQUIRED for iOS
body,
contentAvailable: true,
custom: payload
})
.then(results => {
if (results) {
const responseText = `Push notification for id '${
pushInfo.push_token
}' and app '${pushInfo.app_name}' stored. Notification push ${
_.get(results, "[0].success", 0) === 1 ? "succeded" : "failed"
}`;

if (res) {
res.status(201).send({ message: responseText });
}
if (res) {
res.status(201).send({ message: responseText });
}

console.log(responseText);
console.log(responseText);
}
});
})
.catch(err => {
if (res) {
return res.status(500).send({
message: `Error creating push notification: ${err}`
});
}
console.log(`Error creating push notification: ${err}`);
});
}
});
};

exports.addOne = (req, res) => {
const { ids, payload } = req.body;
// store notification record
PushNotification.create({ ids, payload })
.then(() => {
// send push notification
this.sendNotification(ids, payload, res);
})
.catch(err => {
return res.status(500).send({
message: `Error creating push notification: ${err}`
});
});
this.sendNotification(req.body.ids, req.body.payload, res);
};

0 comments on commit 232da8b

Please sign in to comment.