push notification success in console but not received in android device - android

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?

Related

Google Cloud Messaging Push Notification: PushNotification

I am developing an ionic application with Google Cloud Messaging Push Notification with Visual Studio. First I add phonegap-plugin-push from https://github.com/phonegap/phonegap-plugin-push and insert my senderID which is Project number from Google Cloud Messaging API. Next, I add following code.
var push = PushNotification.init({
"android" : {
"senderID" : "9XX5XXX346XX"// This is your project number which you've created on Google Developers Console
},
"ios" : {},
"windows" : {}
});
push.on('registration', function(data) {
//data.registrationId
//send this token on server
});
push.on('notification', function(data) {
if (data.sound) {// play specific sound on notification received(When app running)
var snd = new Media('file:///android_asset/www/sound/' + data.sound);
snd.play();
}
if (!data.additionalData.foreground) {
// app is NOT RUNNING and new notification is RECEIVED
} else {
// app is RUNNING and new notification is RECEIVED
}
});
push.on('error', function(e) {
//alert(JSON.stringify(data))
// e.message
});
When I run my application in emulator and android device. It comes with eror PushNotification.init. May I know why?

Fetching additional data from AWS SNS Push Notification

I have a problem regarding my SNS Push Notifications. I have the following lambda code:
db.scan(params, function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
data.Items.forEach(function(record) {
var receiverID = record.userDeviceToken.S;
var message = "You have a new invitation to the event";
var topic = "Friend's invitation";
var eventText = JSON.stringify(event);
console.log("Received event:", eventText);
var sns = new AWS.SNS();
var params = {
Message: message,
Subject: "Friend's invitation",
TargetArn: receiverID,
};
sns.publish(params, function(err, data) {
if (err) {
console.log('Failed to publish SNS message');
context.fail(err);
}
else {
console.log('SNS message published successfully');
context.succeed(data);
}
});
});
//context.succeed(data.Items); // data.Items
}
});
};
Now my goal is to get the "Subject" or "topic" sometimes, if it is possible. I cannot find it in documentation, and I need it to customize my notification title depending on the push message sent (I have few functions).
When I used sample amazon app I found this cound in Push Listener Service:
public static String getMessage(Bundle data) {
// If a push notification is sent as plain text, then the message appears in "default".
// Otherwise it's in the "message" for JSON format.
return data.containsKey("default") ? data.getString("default") : data.getString(
"message", "");
}
This works, but the "data" itself has the following data:
Bundle[{google.sent_time=1480364966070, google.message_id=0:1480364966079787%22269524f9fd7ecd, default=You have a new invitation to the event, collapse_key=do_not_collapse}]
Therefore, it is just providing some internal data and the "message" itself. I cannot access the topic.
My question is: how to get other variables in the Android code so I can use them further on? Can I add custom variables by myself through data bundle?
I have notifications in JSON format. I just wonder, whether the only way is to put the data I want in JSON format inside the message, and then read the message accordingly, or maybe I can attach the required data from lambda function to the push notification already?

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);
});

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)
});

Android GCM repeating notifications

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);
});

Categories

Resources