Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author: danyal
- * @date: 10/2/17.
- * @licence: The MIT License (MIT) all copies or substantial portions of the Software
- * @copyright: Tractive Sdn. Bhd.
- *
- * Copyright (c) 2016 Tractive Sdn. Bhd.
- * All rights reserved.
- *
- */
- const utilities = require('../helpers/utilities');
- const admin = require('firebase-admin');
- const serviceAccount = require('../mrt-app-9af67-firebase-adminsdk-v4jdx-d437786efe.json');
- const Subscription = require("../models/Subscription");
- admin.initializeApp({
- credential: admin.credential.cert(serviceAccount),
- databaseURL: 'https://mrt-app-9af67.firebaseio.com'
- });
- exports.sendSMS = function (request, response) {
- console.log("request.body", request.body)
- var responseJSON = {success: true, messages: [], result: []};
- var message = request.body.message;
- var phone = request.body.phone;
- if (message === undefined || message === '') {
- responseJSON.messages.push('message is not exist or not string');
- }
- if (phone === undefined || phone === '') {
- responseJSON.messages.push('phone is not exist or not string');
- }
- phone = parseInt(request.body.phone);
- if (!utilities.isInt(phone)) {
- responseJSON.messages.push('phone_no is not exist or not integer');
- }
- if (responseJSON.messages.length > 0) {
- response.status(422).json(responseJSON);
- } else {
- var requestApi = require("request");
- var options = {
- method: 'GET',
- url: 'http://api.gosms.com.my/eapi/sms.aspx',
- qs:
- {
- company: 'xmt',
- user: 'xmt',
- password: 'PUYCRT',
- gateway: 'L',
- mode: 'BUK',
- type: 'TX',
- hp: phone.toString(),
- mesg: message.toString(),
- charge: '0',
- maskid: '1234abcd',
- convert: '0'
- },
- headers:
- {'cache-control': 'no-cache', 'content-type': 'application/x-www-form-urlencoded'}
- };
- requestApi(options, function (error, res, body) {
- if (error) {
- console.log(error);
- } else {
- console.log("body", body);
- responseJSON.success = true;
- responseJSON.result = body;
- response.status(200).json(responseJSON);
- }
- });
- }
- };
- exports.fcmSubscribe = function (req, res) {
- var responseJSON = {success: false, messages: [], result: []};
- var data = req.body.data;
- if (data.key === undefined || data.key === '') {
- responseJSON.messages.push("'key' is missing");
- }
- if (data.mobile === undefined || data.mobile === '') {
- responseJSON.messages.push("'mobile' is missing");
- }
- if (responseJSON.messages.length > 0) {
- res.status(422).json(responseJSON);
- } else {
- var registrationToken = data.key;
- var mobileNumber = data.mobile;
- addToken(mobileNumber, registrationToken).then(function (result) {
- admin.messaging().subscribeToTopic([registrationToken], 'test').then(function (response) {
- console.log('Successfully subscribed to topic:', response);
- }).catch(function (error) {
- console.log('Error subscribing to topic:', error);
- });
- console.log('Successfully subscribed:', result);
- responseJSON.success = true;
- res.json(responseJSON);
- }).catch(function (error) {
- console.log('Error subscribing:', error);
- responseJSON.messages.push(error.messages);
- res.json(responseJSON);
- });
- }
- };
- exports.fcmPushToTopic = function (req, res) {
- var responseJSON = {success: false, messages: [], result: []};
- var data = req.body.data;
- if (data.topic === undefined || data.topic === '') {
- responseJSON.messages.push("'topic' is missing");
- }
- if (responseJSON.messages.length > 0) {
- res.status(422).json(responseJSON);
- } else {
- var topic = data.topic;
- var text = data.message;
- var message = {
- data: {
- message: text,
- time: (new Date()).toISOString()
- },
- android: {
- ttl: 3600 * 1000,
- priority: 'normal',
- notification: {
- title: 'Test Notifications',
- body: topic + ': ' + text,
- color: '#f45342'
- }
- },
- "webpush": {
- "notification": {
- "title": 'Friend Invitation',
- "body": topic + ': ' + text,
- "icon": 'https://freeiconshop.com/wp-content/uploads/edd/notification-flat.png'
- }
- },
- topic: topic
- };
- admin.messaging().send(message).then(function (response) {
- console.log('Successfully sent message:', response);
- responseJSON.success = true;
- responseJSON.result = [response];
- res.json(responseJSON);
- }).catch(function (error) {
- console.log('Error sending message:', error);
- responseJSON.messages.push(error.messages);
- res.json(responseJSON);
- });
- }
- };
- exports.fcmPushToDevice = function (req, res) {
- var responseJSON = {success: false, messages: [], result: []};
- var data = req.body.data;
- if (data.mobile === undefined || data.mobile === '') {
- responseJSON.messages.push("'mobile' is missing");
- }
- if (responseJSON.messages.length > 0) {
- res.status(422).json(responseJSON);
- } else {
- var mobile = data.mobile;
- var text = data.message;
- findToken(mobile).then(function (subscription) {
- var content = {
- "data": {
- "message": "hello",
- "date": (new Date()).toISOString()
- },
- android: {
- ttl: 3600 * 1000,
- priority: 'normal',
- notification: {
- title: 'Invitation',
- body: 'This is a sample notification for you',
- color: '#f45342'
- }
- },
- "webpush": {
- "notification": {
- "title": 'Invitation',
- "body": 'This is a sample notification for you',
- "icon": 'https://freeiconshop.com/wp-content/uploads/edd/notification-flat.png'
- }
- },
- "token": subscription.token
- };
- admin.messaging().send(content).then(function (response) {
- console.log('Successfully sent message:', response);
- responseJSON.success = true;
- responseJSON.result = [response];
- res.json(responseJSON);
- }).catch(function (error) {
- console.log('Error sending message:', error);
- responseJSON.messages.push(error.messages);
- res.json(responseJSON);
- });
- }).catch(function (error) {
- console.log('Error sending message:', error);
- responseJSON.messages.push(error);
- res.json(responseJSON);
- })
- }
- };
- function addToken(mobile, token) {
- return new Promise(function (resolve, reject) {
- Subscription.findOne({mobile: mobile}, function (err, subscription) {
- if (err) {
- reject(err.message)
- } else {
- if (subscription) {
- subscription.token = token;
- subscription.save();
- resolve(subscription);
- } else {
- Subscription.create({mobile: mobile, token: token}, function (err, subscription) {
- if (err) {
- reject(err.message)
- } else {
- resolve(subscription);
- }
- })
- }
- }
- })
- });
- }
- function findToken(mobile) {
- return new Promise(function (resolve, reject) {
- Subscription.findOne({mobile: mobile}, function (err, subscription) {
- if (err) {
- reject(err.message)
- } else {
- resolve(subscription);
- }
- })
- });
- }
- exports.internalPush = function (req, res) {
- var responseJSON = {success: false, messages: [], result: []};
- var type = req.body.type; // feedback / / / questionnaire / MRT_station / transferred_promo / suggested_promo
- var push_type = ''; // device | topic
- var msg_recipient = ''; // phone number or topic name
- var msg_data = {
- type: '',
- feedback: '',
- friend_request: '',
- friend_respond: '',
- questionnaire: '',
- MRT_station: '',
- transferred_promo: '',
- suggested_promo: ''
- };
- var notification = {
- title: '',
- body: '',
- icon: 'https://lh3.googleusercontent.com/W_LH18eHqeKtN5t0_QAI9u-CxkRmMX_cgN4YpCQErxPpP-a7W2MTr1pF_wQDs9ZAZ68=s180-rw'
- };
- switch (type) {
- case 'feedback':
- push_type = 'device';
- msg_recipient = req.body.recipient;
- msg_data.type = 'feedback';
- msg_data.feedback = JSON.stringify(req.body.data); // {image: "", title: "", description: "", feedback_rate: ""}
- notification.title = "Promotion Redeem";
- notification.body = req.body.data.title + ' has been redeemed.';
- break;
- case 'friend_request':
- push_type = 'device';
- msg_recipient = req.body.recipient;
- msg_data.type = 'friend_request';
- msg_data.friend_request = JSON.stringify(req.body.data); // {user_id: "", name: ""}
- notification.title = "Friend Request";
- notification.body = req.body.data.name + ' wants to connect with you on MyMRT.';
- break;
- case 'friend_respond':
- push_type = 'device';
- msg_recipient = req.body.recipient;
- msg_data.type = 'friend_respond';
- msg_data.friend_respond = JSON.stringify(req.body.data); // {message: ""}
- notification.title = "Friend Request";
- notification.body = req.body.data.message;
- break;
- case 'questionnaire':
- break;
- case 'MRT_station':
- push_type = 'topic';
- msg_recipient = 'mrt_station';
- msg_data.type = 'MRT_station';
- msg_data.MRT_station = JSON.stringify(req.body.data); // {title: "", description: ""}
- notification.title = "MRT Station";
- notification.body = req.body.data.description;
- break;
- case 'transferred_promo':
- push_type = 'device';
- msg_recipient = req.body.recipient;
- msg_data.type = 'transferred_promo';
- msg_data.transferred_promo = JSON.stringify(req.body.data); // {promotion: "", message: ""}
- notification.title = "Promotion Transfer";
- notification.body = req.body.data.message;
- break;
- case 'suggested_promo':
- break;
- }
- if (push_type === 'device') {
- var mobile = msg_recipient;
- findToken(mobile).then(function (subscription) {
- var content = {
- "data": msg_data,
- "android": {
- "ttl": 3600 * 1000,
- "priority": 'normal',
- "notification": notification
- },
- "webpush": {
- "notification": notification
- },
- "token": subscription.token
- };
- admin.messaging().send(content).then(function (response) {
- console.log('Successfully sent message:', response);
- responseJSON.success = true;
- responseJSON.result = [response];
- res.json(responseJSON);
- }).catch(function (error) {
- console.log('Error sending message:', error);
- responseJSON.messages.push(error.messages);
- res.json(responseJSON);
- });
- }).catch(function (error) {
- console.log('Error sending message:', error);
- responseJSON.messages.push(error);
- res.json(responseJSON);
- })
- } else if (push_type === 'topic') {
- var topic = msg_recipient;
- var content = {
- data: msg_data,
- android: {
- ttl: 3600 * 1000,
- priority: 'normal',
- notification: notification
- },
- "webpush": {
- "notification": notification
- },
- topic: topic
- };
- admin.messaging().send(content).then(function (response) {
- console.log('Successfully sent message:', response);
- responseJSON.success = true;
- responseJSON.result = [response];
- res.json(responseJSON);
- }).catch(function (error) {
- console.log('Error sending message:', error);
- responseJSON.messages.push(error.messages);
- res.json(responseJSON);
- });
- } else {
- }
- };
- //"icon": 'https://freeiconshop.com/wp-content/uploads/edd/notification-flat.png'
- /*
- *
- * var topic = data.topic;
- admin.messaging().subscribeToTopic([registrationToken], topic)
- .then(function (response) {
- console.log('Successfully subscribed to topic:', response);
- responseJSON.success = true;
- responseJSON.result = [response];
- res.json(responseJSON);
- })
- .catch(function (error) {
- console.log('Error subscribing to topic:', error);
- responseJSON.messages.push(error.messages);
- res.json(responseJSON);
- });*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement