Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as JavaScript by Paulao ( 5 years ago )
var request = require('request');
var Config = require('../config');

module.exports = function(kwargs, cb){
	
	var Notify = {};
	var API_ACCESS_KEY = Config.CMKEY;

	Notify.prepare = function(data, fn){
		this.args = data;
		this.send(function(res){
			fn(res);			
		});
	}

	Notify.send = function(fn){

		var args = this.args;
		var to = args.to;
		var text = args.message;
		var title = args.title;
		var target = [to];

		if( to instanceof Array ){
			target = to;
		}


		var msg = {
			"actions": args.actions,
			'message'	: text,
			'title'		: title,
			'vibrate'	: 1,
			"style": args.style,
			"picture": "http://138.197.124.10:1234/static/logo.png",
			'sound'		: 1,
			"icon": "ic_stat_logo",
		    "image": args.image ? args.image: '',
	        "image-type": "circle",
	        actions: [
	    		{ 
	    			"icon": "ic_stat_logo",
	    			"title": "Atualizar cadastro", 
	    			"callback": "window.openPlay", 
	    			"foreground": true
	    		},
	    	
    		]

		};
		var merged = require('util')._extend(msg, args.data);

		var fields =	{
			'registration_ids' : target,
			'data' : merged,
			
		};



		var options = {
		  method: 'post',
		  body: fields,
		  json: true,
		  url: 'https://android.googleapis.com/gcm/send',
		  headers: {	
			'Authorization': 'key=' + API_ACCESS_KEY,
			'Content-Type': 'application/json'
		  }
		}


		// console.log(options)

		request(options, function (err, res, body) {
		  if (err) {
		    console.log('Error :', err)
		    return
		  }
		  if( args.debug )
		  	console.log(' Body :', body)

		  fn(body);

		});	
	}


	Notify.prepare({
		to: kwargs.to,
		title: kwargs.title,
		message: kwargs.text,
	}, function(res){
		cb(res);
	});
};

 

Revise this Paste

Children: 114687
Your Name: Code Language: