Unable to push GCM notification using node-gcm - android

I need to push GCM from node, I am using node-gcm, but I am getting ENOTFOUND error
var gcm = require('node-gcm');
var gcmMessage = new gcm.Message({
collapseKey: 'notify',
delayWhileIdle: false,
timeToLive: 3600,
data: {
title: "Hai",
message: "Message",
}
});
var gcmIds = ['<myid>'];
var sender = new gcm.Sender('<mysender id>',{'proxy':'<proxyip>:8080'});
sender.send(gcmMessage, gcmIds, 4, function(err, ret){
console.log('>>> sendGcm: ', err, ret);
});
I am getting error
sendGcm: { [Error: tunneling socket could not be established, cause=getaddr
info ENOTFOUND] code: 'ECONNRESET' } null

Related

Firebase notifications working but not opening app on android with Ionic

I'm using Firebase notifications on my app and for some reason, the notifications will not open the app on android. I really don't have any clue what is causing this because some apps work fine and others don't.
Does anyone have any idea what would be causing this?
I use this code to send notifications to a specific device. Sometimes they open, other times they dont.
var admin = require("firebase-admin");
var Geopoint = require("geopoint");
var serviceAccount = require("./serviceaccount.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://myapp.firebaseio.com"
});
var db = admin.database();
const title = "My Test";
const body = "My Test Body";
const deepLink = "http://google.com/";
const token = "firebaseDeviceToken";
var payload = {
notification: {
title,
body,
sound: "default",
deepLink
},
data: {
title,
body,
deepLink
}
};
var options = {
priority: "high",
timeToLive: 60 * 60 * 24,
contentAvailable: true
};
console.log(payload);
return admin
.messaging()
.sendToDevice(token, payload, options)
.then(r => {
console.log(JSON.stringify({ r }));
process.exit(0);
})
.catch(err => {
console.log(JSON.stringify({ r: err }));
process.exit(1);
});

Repeating push notification in back4app

I am sending push notification from iOS and Android both by calling a cloud function, and in each device I am getting 5 times a single push notification. I am using parse database hosted on back4app.
Cloud code is given below:
Parse.Cloud.define("push", function (request, response) {
var query = new Parse.Query(Parse.Installation);
var userID = request.params.user;
var message = request.params.message;
var notificationType = request.params.notificationType;
var user = new Parse.User();
user.id = userID;
query.equalTo('user', user);
query.equalTo("allowPush", true);
Parse.Push.send({
where: query,
data: {
alert: message,
sound: 'default',
"type": notificationType
}
}, { useMasterKey: true });
});
Try to call reponse.success and response.error functions in the end of your cloud code function. Since your client code is not receiving the feedback if the call worked or not it is probably attempting to send again.
Parse.Cloud.define("push", function (request, response) {
var query = new Parse.Query(Parse.Installation);
var userID = request.params.user;
var message = request.params.message;
var notificationType = request.params.notificationType;
var user = new Parse.User();
user.id = userID;
query.equalTo('user', user);
query.equalTo("allowPush", true);
Parse.Push.send({
where: query,
data: {
alert: message,
sound: 'default',
"type": notificationType
}
},
{
success: function () { response.success(); },
error: function(err) { response.error(err); },
useMasterKey: true
});
});

npm-gcm does not send notification to the device

guys, I want to send a notification to the android device using npm-gcm
and here is my code:
var sender = new gcm.Sender('myAPIkey');
var message = new gcm.Message();
message.addNotification('title', 'Backend!!!');
message.addNotification('body', 'Bitch');
var regTokens = ['deviceToken1'];
console.log(message)
regTokens.push('deviceToken2')
sender.send(message, { registrationTokens: regTokens }, function (err, response) {
if (err) console.error(err);
else console.log(response);
});
and in the console I get:
{ multicast_id: 7664202372197545000,
success: 2,
failure: 0,
canonical_ids: 0,
results:
[ { message_id: '0:1481660978053751%cc7dad02f9fd7ecd' },
{ message_id: '0:1481660978045814%cc7dad02f9fd7ecd' } ]
}
but the devices did not receive any notification. What is the problem?
Why is this happening? Can you give me an advice?!
Based on this SO post,there might be an issue with "notification" object on node-gcm code. Try to use message.addData and check if it works. Make sure that you followed this tutorial and this thread that gives sample code on how to send simple message to your device.
var sender = new gcm.Sender(googleApiKey);
var message = new gcm.Message();
message.addData('key1','testdarinodegcm');
message.delay_while_idle = 1;
var registrationIds = [];
registrationIds.push('APA91bHCzBP6wEfExCZlSXgf0wuOC6QEWJ-7MVFl3hgaW3Jv8FslsofGJ- qgdliyS8zjwy_W7zPaKsEZx6kbeuWdoAAexawKl1Qd6GEGV_v844n1LMRaTsWeiwI9iaLGKKq_R3scY_wuRG8uG2SZ5X9q0J67Ko3gcw');
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);
});
Hope this helps!

Message not delivering in GCM

I am using this Java code for sending message
Sender sender = new Sender("AIza..................Q");
// use this to send message with payload data
Message message = new Message.Builder()
.collapseKey("message")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message", "Welcome to Push Notifications") //you can get this message on client side app
.build();
//Use this code to send notification message to a single device
Result result = sender.send(message,
"eBXXXXX:XXXXXXXXXXX",
1);
I am getting response
[ messageId=0:1465322271594526%5036bc7038eb0007 ]
On my mobile I have used
var push = PushNotification.init({
android: {
senderID: "SNDER_ID"
},
ios: {
alert: "true",
badge: "true",
sound: "true"
},
windows: {}
});
push.on('registration', function(data) {
console.log("registration: "+JSON.stringify(data));
});
push.on('notification', function(data) {
console.log("notification: "+JSON.stringify(data));
// data.message,
// data.title,
// data.count,
// data.sound,
// data.image,
// data.additionalData
});
push.on('error', function(e) {
console.log("error: "+JSON.stringify(e));
});
Here I am using this plugin but I am not getting any message inside notification

send downstream message to google ccs with node js

I am using node-xmpp to connect to google gcm ccs servers. I followed the from gcm google groups to connect. Now i need to send a downstream message whenever i receive a message from my redis subscriber(i subscribed to a redis channel redis node package). My code is as follows
var gearmanode = require('gearmanode');
var redis = require("redis");
var xmpp = require('node-xmpp');
var gearJob;
var redisSubChan = 'test_channel';
var gearmanJobName = 'reverse';
var jobPayload;
var redisClient;
var xmppClient;
var gearClient;
gearClient = gearmanode.client();
var options = {
type: 'client',
jid: 'myid#gcm.googleapis.com',
password: 'myserverkey',
port: 5235,
host: 'gcm.googleapis.com',
legacySSL: true,
preferredSaslMechanism: 'PLAIN'
};
console.log('creating xmpp app');
xmppClient = new xmpp.Client(options);
xmppClient.connection.socket.setTimeout(0)
xmppClient.connection.socket.setKeepAlive(true, 10000)
redisClient = redis.createClient();
redisClient.subscribe(redisSubChan);
redisClient.on("message", function(channel, message) {
console.log('received message');
console.log(message);
message = JSON.parse(message);
//send the messages to google ccs server via xmpp
var payload = {
"to": message.to,
"message_id": message.message_id,
"data": message.data,
"time_to_live": message.time_to_live,
"delay_while_idle": message.delay_while_idle
};
var jsonPayload = JSON.stringify(payload);
console.log(jsonPayload);
var ackToDevice = new xmpp.Element('message', {'id': ''}).c('gcm', {xmlns: 'google:mobile:data'}).t(jsonPayload);
console.log('prepared message');
console.log(ackToDevice.root().toString());
xmppClient.send(ackToDevice);
console.log('sent!!!');
});
xmppClient.on('online', function() {
console.log("online");
});
xmppClient.on('connection', function() {
console.log('online');
});
xmppClient.on('stanza',
function(stanza) {
if (stanza.is('message') && stanza.attrs.type !== 'error') {
// Best to ignore an error
console.log("Message received");
//Message format as per here: https://developer.android.com/google/gcm/ccs.html#upstream
var messageData = JSON.parse(stanza.getChildText("gcm"));
if (messageData && messageData.message_type != "ack" && messageData.message_type != "nack") {
var ackMsg = new xmpp.Element('message', {'id': ''}).c('gcm', {xmlns: 'google:mobile:data'}).t(JSON.stringify({
"to": messageData.from,
"message_id": messageData.message_id,
"message_type": "ack"
}));
//send back the ack.
xmppClient.send(ackMsg);
console.log("Sent ack");
//receive messages from ccs and give it to PHP workers
gearClient.submitJob(gearmanJobName, JSON.stringify(messageData), {background: true});
} else {
//Need to do something more here for a nack.
console.log("message was an ack or nack...discarding");
}
} else {
console.log("error");
console.log(stanza);
}
});
xmppClient.on('authenticate', function(opts, cb) {
console.log('AUTH' + opts.jid + ' -> ' + opts.password);
cb(null);
});
xmppClient.on('error', function(e) {
console.log("Error occured:");
console.error(e);
console.error(e.children);
});
I am able to receive the messages from ccs server but can not send the downstream message from redis on message callback.
I get the following error
error
{ name: 'message',
parent: null,
attrs:
{ id: '',
type: 'error',
to: '1026645507924#gcm.googleapis.com/8DF23ED7',
'xmlns:stream': 'http://etherx.jabber.org/streams' },
children:
[ { name: 'gcm',
parent: [Circular],
attrs: [Object],
children: [Object] },
{ name: 'error',
parent: [Circular],
attrs: [Object],
children: [Object] } ] }
I tried to print(followed node xmpp) the xmpp stanza before sending and its same
//log of my message
<message id=""><gcm xmlns="google:mobile:data">{"to":"APA91bHIGZcbePZ-f1jSyWQkBAJMHorHJiwgtN1GWITzcHf6uyVOZ3k7AasUiB-vBix32ucSypin3xDTYmoxqSc_ZmTDTuKjuDQ8BPQLpC41SqYRimm-hn34ZjSAF4uQO0OP1LSbqUxjh2WF0K5n4KyD3-Vn8ghASQ","message_id":84,"data":{"test":"sample data to send"},"time_to_live":0,"delay_while_idle":false}</gcm></message>
as they mentioned in documentation(Request format). What is wrong with my code ?
I had the same issue. It nearly drove me crazy but in the end it was just an Invalid JSON format error.
I suppose messageData.from or messageData.message_id wasn't converted to correct JSON format. In my case I passed a scalar and JSON.stringify() didn't converted it to a string. Hence the result was --> "message_id": 1234 and not "message_id": "1234"
The quick answer is you can not use CCS (XMPP) without having your project whitelisted. If you try using smack library instead, you will get an error saying that your project is not whitelisted.

Categories

Resources