Android GCM repeating notifications - android

I am using the cordova plugin for GCM at GCM-Cordova. On the server side, I am using ToothlessGear's node module.
I think I am not going to use a 'collapsible' key as I require a payload to be sent to the client. The problem I am facing is that the message keeps on arriving after an interval of about 30 seconds. Why is the google server not realizing that the message has been received? Do I have to explicitly do something to stop repeating notifications?
The node-gcm code for triggering push :
gcm = require('node-gcm');
message = new gcm.Message();
sender = new gcm.Sender(app.settings.gcm.server_key);
registrationId = [];
registrationId.push(regid);
message.addData('title', 'Payment Request');
message.addData('message', JSON.stringify(someObj));
message.delayWhileIdle = true;
message.timeToLive = 3;
sender.send(message, registrationId, 1, function(result) {
return console.log(result);
});

Related

How to send push notifications to multiple device with FCM tokens using cloud functions

First I generated a FCM token and stored in firestore. After that I wrote a cloud functions to send notifications based on FCM token. and I deployed cloud functions it says successfully sent notifications with status ok. But it doesn't displays in mobile device. My Index.js is
'use strict';
const functions = require('firebase-functions');
const Firestore = require('#google-cloud/firestore');
const admin = require('firebase-admin');
const firestore = new Firestore();
const db = admin.firestore();
admin.initializeApp(functions.config().firebase);
exports.hellouser = functions.firestore
.document('users/{token}')
.onWrite(event =>{
var document = event.data.data();
console.log("tokens",document);
var token = ['cdNN0AbYKU0:APA91bEyL0zo3zwHZD8H43Vp7bxAfYgehlVI8LrKktPO2eGuByVDdioysIGxHe5wocwq8ynxRToJPpOve_M59YY_MIRbWLnF9AIgoTwJORXZbw6VBw7']// this is my FCM token.
if(
const payload = {
notification: {
title: "Message",
body: "hi hello",
sound: "default"
}
};
return admin.messaging().sendToDevice(token, payload).then((response)=> {
console.info("Successfully sent notification")
}).catch(function(error) {
console.warn("Error sending notification " , error)
});
});
How to send notifications based on the FCMtoken.
If it's the exact code you use then check syntax near if(. This may help you.Next write some code to go through your response object. Firebase may take your tokens and payload, process them and return 200 OK response but in the response you will have errors. Response has general structure like this: { results:
[ { //stuff related to one token },{ //stuff related to one token } ],
canonicalRegistrationTokenCount: 0,
failureCount: 1,
successCount: 0,
multicastId: SOME_LONG_NUMBER }Take in mind that response.results array has status of each message sent to token in the same order as tokens in your token array. You can see all posible errors in Firebase Documentation. If response.failureCount > 0 then no messages were sent and you should get corresponding error in response.results.Also learn about options variable. options.priority must be 'high' to guarantee fast message delivery. Maybe this will help.

How to send Push Notification Firebase

I am new to Firebase and the main reason I adapted to it from my old MySql DB is the ability to send push notification and dynamic links. I have been trying for the past two days to send notification to a group of people who have subscribed to a topic from my node.js script. The script always returns InternalServerError. I am able to send notification from the Firebase console but that is not good enough for my app as I need to implement dynamic notification (i.e. triggered by one users action).
So far I did not understand what was in the official docs and tried following a tutorial I found and I am currently here
app.get('/push',function(req,res){
/*var title = req.params.title;
var body = req.params.body;*/
// var confName = req.params.name;
var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
to: '/topics/ilisten',
// collapse_key: 'your_collapse_key',
notification: {
title: 'This is Title',
body: 'This is body'
},
data: { //you can send only notification or only data(or include both)
my_key: 'Conf Name here'
}
};
fcm.send(message, function(err, response){
if (err) {
console.log("Something has gone wrong!"+err);
} else {
console.log("Successfully sent with response: ", response);
}
});
})
My first question is what should I do in the to field so that all the users in my app reciece the notification.
I would also like to take a look at correct and complete implementation of this concept with android code. If anyone has such code please share it here as it would help the future Firebase users who cannot understand the official docs like me.
Following is one approach using node-gcm (https://github.com/ToothlessGear/node-gcm)
var gcm = require('node-gcm');
var sender = new gcm.Sender(<sender_key>);
var message = new gcm.Message();
message.addNotification('title', title);
message.addNotification('body', body);
sender.send(message, { topic: "/topics/" + topic }, function (err, response) {
if (err) console.error(err);
else console.log(response);
});

push notification success in console but not received in android device

i've to send push notification to android and apple devices, apple devices are getting the push notification but android not, its showing successfully sent notification but there's no notification in my android device.
following is my code, in this i'm sending one hardcoded device id and an API key:
var pushToAndroid=function(tokens,message)
{
var message = new gcm.Message();
var sender = new gcm.Sender('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
var registrationIds = [];
message.addData('id',message.title);
registrationIds.push('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
sender.send(message,['xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'],2,
function (err, result) {
if(err) console.log(err);
else console.log("Send Notification Android"+ JSON.stringify(result));
});
};
output:
{"multicast_id":6582780084434899000,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1426068793442479%b980d923f9fd
7ecd"}]}
kindly tell what am i doing wrong?

How to run a Notify.js file for receive a pushnotification which is designd in phonegap app?

I want to implement a Pushnotification in Phonegap 2.9.1 in android. I done till getting regId after this how can i receive a pushnotification,I also created a Notify.js file which is below and when i run this Notify.js file in cmd like this "DemoPush > node notify.js" its give me error can not fine this module,So how can i run this and how can i receive pushnotification in phonegap 2.9.1. Any one can tell me please? I am newly in Phonegap
Notify.js
var gcm = require('node-gcm');
var message = new gcm.Message();
var sender = new gcm.Sender('APIKey');
var registrationIds = [];
message.addData('message',"\u270C Peace, Love \u2764 and PhoneGap \u2706!");
message.addData('title','Push Notification Sample' );
message.addData('msgcnt','3');
message.addData('soundname','beep.wav');
message.timeToLive = 300;
registrationIds.push('regId');
sender.send(message, registrationIds, 4, function (result) {
console.log(result);
alert(result)
});

TCP bidirectional communication between c# and android

I try to connect a Pc with and Android device via TCPp using the eneter library
When I send data from the android device to the PC, data is transferred and answer back to android is received immediately but I get problems transfering data from PC to the android device
With the following code the PC is sending the data to itself because I receive the data on the OnMessageReceived procedure of the C# code not in the android
public NCProgram()
{
aReceiverFactory = new DuplexTypedMessagesFactory();
myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();
mySender = aReceiverFactory.CreateDuplexTypedMessageSender<MyResponse, MyRequest>();
// Subscribe to handle messages.
myReceiver.MessageReceived += OnMessageReceived;
mySender.ResponseReceived += OnResponseReceived;
// Create TCP messaging.
IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("tcp://192.168.173.1:6060/");
IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://192.168.173.1:6060/");
// Attach the input channel and start to listen to messages.
myReceiver.AttachDuplexInputChannel(anInputChannel);
// Attach output channel to the sender and be able to send
// request messages and receive responses.
mySender.AttachDuplexOutputChannel(anOutputChannel);
m_TexteRebut = "Comunicació establerta";
}
public void Enviar()
{
MyRequest aRequestMessage = new MyRequest();
aRequestMessage.Text = m_Texte_Enviar;
mySender.SendRequestMessage(aRequestMessage);
}
If I don't use the the outputchannel and I send the message as if it was an answer to a communication from the android device, then the data are transferred to the android device but sometimes it takes one second, sometimes a few ones and sometimes 20 or 30 seconds.
public NCProgram()
{
aReceiverFactory = new DuplexTypedMessagesFactory();
myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();
//mySender = aReceiverFactory.CreateDuplexTypedMessageSender<MyResponse, MyRequest>();
// Subscribe to handle messages.
myReceiver.MessageReceived += OnMessageReceived;
//mySender.ResponseReceived += OnResponseReceived;
// Create TCP messaging.
IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("tcp://192.168.173.1:6060/");
//IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://192.168.173.1:6060/");
// Attach the input channel and start to listen to messages.
myReceiver.AttachDuplexInputChannel(anInputChannel);
// Attach output channel to the sender and be able to send
// request messages and receive responses.
//mySender.AttachDuplexOutputChannel(anOutputChannel);
m_TexteRebut = "Comunicació establerta";
}
public void Enviar()
{
MyRequest aRequestMessage = new MyRequest();
aRequestMessage.Text = m_Texte_Enviar;
myReceiver.SendResponseMessage(IPAddress,aRequestMessage);
}
Any advice on how to solve the communication from PC to android device will be appreciated

Categories

Resources