Getting wrong data throug GCM push notification - android

I am building application using phonegap build and pushnotification plugin.
I can't understand why is field "event" (inside "data")always "message" even if i dont send it at all or if i send something else like "event":"registration". Also i dont get any other fields i include in data, like "message_id" and sort...
Here is example of rest request i am using for testing and it works fine (i can change message and it always sends correct message)
{
"data" : {
"event":"message",
"message":"some message"
},
"registration_ids" : ["APA9..."]
}
but when I try to send this:
{
"data" : {
"event":"newMessage",
"message_id":134,
"group":1
},
"registration_ids" : ["APA91b..."]
}
I still get event "message" and dont even see other fields in code.
Here is my code:
onNotificationGCM: function(e) {
switch( e.event )
{
case 'message':
alert(e.message);
break;
case 'newMessage':
alert ('entered newMessage');
var id = e.message_id;
var grp = e.group;
//something to do with theese two but it never enters here
break;
default:
alert('An unknown GCM event has occurred');
break;
}
It never gets to alert ('entered newMessage') and if I put alerts of fields group and e.message_id in case 'message', they are undefined.
Hope someone can help, thanks in advance (sorry for bad english).

Ok, issue was with push plugin from phonegap build, it forwarded only those two fields... Now it is updated and plugin now forwards all arguments sent under "payload" field. Hope it helps some1 else with similar issue

Related

How to deliver messages atomically when using Firebase without a backend logic

I´m creating this chat app using Firebase. I wanted to create a system
where every device having the chat app installed is responsible for delivering chat messages.
Even if the device did not send the message it plays a part in the pool of workers, that deliver chat messages.
Now for a normal chat app the device that creates the chat message can easily send the message using E.g. OneSignal.
In my case user can chat with abstract things and there can be many people listening for new chat messages. One is sending and 1000+ is listening.
Here is where the problem comes. If one device is responsible for notifying 1000+ other it can get really heavy on the load. I was thinking all other devices can help out with the delivery,
so I´m trying this Firebase database notification entry:
{
"NOTIFICATIONS" : {
"-Kg4_cnR9T8Efx77rL2n" : {
"targetId" : "-KfpVVenyQccKqAxxrvE",
"text1" : "There´s a message for you",
"text2" : "Pluto",
"text3" : "",
"time" : 1490447404504,
"type" : "chatMessage"
},
"-Kg4_eWQFZndhztPqSTp" : {
"targetId" : "-KfpWz7ZWBAa_8pLM7tI",
"text1" : "There´s a message for you",
"text2" : "Saturnus",
"text3" : "",
"time" : 1490447411536,
"type" : "chatMessage"
}
}
}
and when one device is creating a message it post this new NOTIFICATIONS that will be picked up by all devices that are listening.
Now since all devices is listening on the NOTIFICATIONS and will grab one NOTIFICATIONS and send the chat message heads up, to all other devices that are registered on the targetId the problem obviously arise.
Here are the TARGET_VISITORS. This are the users that are registers for new chat messages.
{
"TARGET_VISITORS" : {
"-KfpVVenyQccKqAxxrvE" : {
"HnhLyXRxUINmlltK3jdsfxx2QBYiQ53" : {
"notify" : true,
"time" : 1490300590623,
"userFirebaseId" : "HnhLyXRxUINmlltK3jdsfxx2QBYiQ53"
}
},
"-KfpWz7ZWBAa_8pLM7tI" : {
"HnhLrryXUINmlltK3jdsfxx2QBYi3455" : {
"notify" : true,
"time" : 1490300581677,
"userFirebaseId" : "HnhLrryXUINmlltK3jdsfxx2QBYi3455"
}
}
Can I use the Firebase Transaction for this?
Like one device pick up a NOTIFICATIONS and then gets maybe 10 TARGET_VISITORS (there can be 1000+), and in a Firebase transaction locking down TARGET_VISITORS to perform the chat message heads up delivery on his userFirebaseId or OneSignal PlayerId.?
After that the TARGET_VISITORS can save the NOTIFICATIONS id and by doing that prevent getting duplicate messages.
Maybe creating a visitor lock rule like:
"TARGET_VISITORS_LOCK": {
".read": "auth != null",
"$FirebaseId": {
// This need to be a final entity.
// we can write as long as old data or new data does not exist.
// In other words, we can write if we're deleting or creating data, but not updating data.
".write": "!data.exists() || !newData.exists() && auth != null"
}
},
And then run an Data Fan Out updateChildren on the TARGET_VISITORS_LOCK, if it fails it means some other device is running the updateChildren before me and sending the notification to the single device. What will happen her if device having an offline period?
Many devices in the pool of workers can have the same NOTIFICATIONS to try to send and they will fight over TARGET_VISITORS, so to speak
Then comes the problem to know when to remove a NOTIFICATIONS entry, remove it when all TARGET_VISITORS have been notified :) Yea this is quite fun :)
It´s all theoretical at this point and i´m reading a loot so hoping for some input about this?

TypeError: sockets.forEach is not a function

i am trying to create a simple personal chat with node js and android.the following code works fine
socket.on('message' , function (msg) {
console.log(msg);
io.emit('message' , {message : msg});
})
when a message is receive from a client, the server send it to all other client connected to the server. but the problem is that the sender receive his own message as well. to overcome this issue i am using the following code
socket.on('message' , function (msg) {
console.log(msg);
//io.emit('message' , {message : msg});
var sockets = io.sockets.sockets;
sockets.forEach(function (sock) {
if(sock.id != socket.id){
sock.emit('message' , {message:msg})
}
})
})
but the code above produces an error
TypeError: sockets.forEach is not a function
can anyone help me with this and save my day? thanks in advance.
As for Socket.io v1.4.5, it should be:
Object.keys(io.sockets.sockets);
This returns an array containing id's of sockets connected and you can apply forEach on it.
Object.keys(io.sockets.sockets).forEach(function(id) {
console.log("ID:",id) // socketId
});
To send a message to a particular socket, you can use:
io.to(socketId).emit()
This worked for me:
I input npm install socket.io#1.3.5 --save in commandline
It's because of the version. Go to package.json and change like this:
..
.
..
..
"dependencies": {
"express": "^4.17.1",
"socket.io": "^1.3.5"
},

Unable to send the GCM registration ID to application server

I am implementing android push notification. I could create the cordova/phonegap app using Intel XDK and made all the setup on GCM. I can see my application showing the registration id when I install the app and story is smooth till this.
Now I want to send the registration id to my application server that I hosted. I have already written the rest api which can receive a get and save the registration id. Now the issue is when ever I add the Restful call in onNotificationGCM of index.js, the apk installation shows error "There was problem parsing the package". Please find my onNotificationGCM call code
onNotificationGCM: function(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
var req = new XMLHttpRequest();
var url = "http://XXXX.com/mycloudapp/register?regid="+e.regid;
console.log("Regid " +url);
req.open("GET", url, true);
console.log("After req.open");
req.send();
console.log("after send");
alert('registration id = '+e.regid);
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = '+e.message+' msgcnt = '+e.msgcnt);
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
if I remove the below code from above, again it works fine
req.open("GET", url, true);
console.log("After req.open");
req.send();
Your help is highly appreciated!!!!
At last I could solve the issue. The issue is nothing to do with the coding, but Intel XDK project configuration for white listing the external domain. Please follow steps below
1) Select you project
2) Go to the Build Settings and expand
3)Goto Access List box
4) Keep * and select External
5) Add Another access list entry
6) Provide the external domain URL select internal
This worked for me very well.

How to get Registration ID with Intel XDK?

For a while now, I have been trying to figure out how to send push notifications. The app I have made is for Android right now, but I want to extend it to other devices once I figure this out. I've looked into various services, such as Amazon SNS, but they all neglect to include how to get the device token. They all assume you know how to do that.
So what I am asking is: how do I get a device token/registration ID for a device?
I tried using this code:
var tokenID = "";
document.addEventListener("deviceready", function(){
//Unregister the previous token because it might have become invalid. Unregister everytime app is started.
window.plugins.pushNotification.unregister(successHandler, errorHandler);
if(intel.xdk.device.platform == "Android")
{
//register the user and get token
window.plugins.pushNotification.register(
successHandler,
errorHandler,
{
//senderID is the project ID
"senderID":"",
//callback function that is executed when phone recieves a notification for this app
"ecb":"onNotification"
});
}
else if(intel.xdk.device.platform == "iOS")
{
//register the user and get token
window.plugins.pushNotification.register(
tokenHandler,
errorHandler,
{
//allow application to change badge number
"badge":"true",
//allow application to play notification sound
"sound":"true",
//register callback
"alert":"true",
//callback function name
"ecb":"onNotificationAPN"
});
}
}, false);
//app given permission to receive and display push messages in Android.
function successHandler (result) {
alert('result = ' + result);
}
//app denied permission to receive and display push messages in Android.
function errorHandler (error) {
alert('error = ' + error);
}
//App given permission to receive and display push messages in iOS
function tokenHandler (result) {
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send the token to your server along with user credentials.
alert('device token = ' + result);
tokenID = result;
}
//fired when token is generated, message is received or an error occured.
function onNotification(e)
{
switch( e.event )
{
//app is registered to receive notification
case 'registered':
if(e.regid.length > 0)
{
// Your Android push server needs to know the token before it can push to this device
// here is where you might want to send the token to your server along with user credentials.
alert('registration id = '+e.regid);
tokenID = e.regid;
}
break;
case 'message':
//Do something with the push message. This function is fired when push message is received or if user clicks on the tile.
alert('message = '+e.message+' msgcnt = '+e.msgcnt);
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
//callback fired when notification received in iOS
function onNotificationAPN (event)
{
if ( event.alert )
{
//do something with the push message. This function is fired when push message is received or if user clicks on the tile.
alert(event.alert);
}
if ( event.sound )
{
//play notification sound. Ignore when app is in foreground.
var snd = new Media(event.sound);
snd.play();
}
if ( event.badge )
{
//change app icon badge number. If app is in foreground ignore it.
window.plugins.pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}
}
All I get is an alert that says "result = ok". The alerts later on in the code don't happen. I've tried making sense of the code but I'm not getting anywhere. Any suggestions? Is there a tutorial for this I'm not finding?
Those legacy intel.xdk functions are being retired (the will continue to live in an 01.org, see the notice on this page: https://software.intel.com/en-us/node/492826).
I recommend you investigate one of the many push notification Cordova plugins that are available. Use your favorite web search tool to search for something like "cordova phonegap push notification plugin" to find some. The good ones will have examples of how to use.
Note:-
Unregister - Its not strictly necessary to call it.....
Ensure that you have a sender ID for Android (no idea about iOS).
Result OK means that the plugin is installed correctly and has run properly.
Problems could be due to:
Incorrect sender ID
Testing in emulator without adequate setup
Important - Push notifications are intended for real devices. They are not tested for WP8 Emulator. The registration process will fail on the iOS simulator. Notifications can be made to work on the Android Emulator, however doing so requires installation of some helper libraries, as outlined here, under the section titled "Installing helper libraries and setting up the Emulator".
onNotification must be available as a global object. So try attaching it to the window. Refer to this question
Examples of properly initializing PushPlugin in:
Ionic (my answer)

Trying to implement Pushwoosh on PhoneGap build (Android)

I am trying to get my notifications to work however I am not receiving them. I followed the tutorial for implementing pushwoosh on phonegap build. I have my App ID and API Key from Google however I am not getting my device which the app is on to register when I reinstall the app.
I am guessing that my issue is in the following code:
// handle GCM notifications for Android
function onNotificationGCM(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
PushWoosh.appCode = "YOUR_PUSHWOOSH_APP_ID"; // my pushwoosh id is here
PushWoosh.register(e.regid, function(data) {
alert("PushWoosh register success: " + JSON.stringify(data));
}, function(errorregistration) {
alert("Couldn't register with PushWoosh" + errorregistration);
});
}
break;
I inserted this code in <script> tags within my index file however the device doesn't register what could be causing this issue?
I should also mention that I have the pushwoosh.js file within my javascript folder and I am pointing to it in my reference tags for .js files

Categories

Resources