Unable to send the GCM registration ID to application server - android

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.

Related

Parse Push is not working with Parse Sever

I am a newbie on parse server and have been using it with my android app. But I am having trouble with implementing push notifications. I do not know how to get logs so any guidance will be much appreciated! The installation class does not contain any "GCMSenderId" or "deviceToken".
Here is how I set up parse server to implement push notifications.
Parse Sever : 2.2.7
1) Developer Console
a) I create a new project using the new console and clicked on the overflow menu --> Project Info and got my project number.
b) Then I went to the "Credentials" page and clicked on Create Credentials --> Api Key --> Server Key and left everything default and clicked create.
c) I got the key for my project. Let it be "MY_API_KEY".
2) Now I setup my parse server app to use this api key and project number as you can see from the image.
enter image description here
3) Now I setup my android app manifest accordingly as you can see from the picture below.
enter image description here
I also added ParseInstallation.getCurrentInstallation.saveInBackground();in my app's Application class.
I am trying to send notifications using the parse dashboard
Please provide any help if possible, thanks!
to send push notification from parse server you have to implement cloud code
in Javascript or another programming language.
below is some code in JS to send push notification to a user
function(objectids,msg) {
var pushQuery = new Parse.Query(Parse.Installation);
msg.default="default";
pushQuery.exists("deviceToken");
pushQuery.containedIn('userObjectId',objectids);
try
{
Parse.Push.send({
where: pushQuery,
data: {
alert:msg.msgInfo,
str:msg,
sound:msg.default
}
}).then(function() {
console.log("push successfully sent");
}, function(error) {
console.log("error while send pn:"+error.code+":"+error.message);
throw "Push Error " + error.code + " : " + error.message;
});
}catch(error)
{
console.log("error while send pn:"+error.code+":"+error.message);
}
}

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)

Push Notification Android with ionic framework

I'm trying to send push notifications to my Android device from my server. Using plug PushNotification Cordova, my code is something.
var androidConfig = {
"senderID": "inspired-berm-101218",
};
document.addEventListener("deviceready", function(){
$cordovaPush.register(androidConfig).then(function(result) {
// Success
}, function(err) {
// Error
})
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
alert('registration ID = ' + notification.regid);
sessionService.set("token_device",notification.regid);
My information according to me, I followed advice from this forum and tutorials are good and are these:
Api KEY : AIzaSyDtZndyGvmWXF0TpYe83KVDgxRZ4MR3zK8
ID del proyecto: inspired-berm-101218
NĂºmero del proyecto: 805573676421
All this I use both my application and server are the same codes, but nevertheless, I receive the error:
{"multicast_id":7843752850335107662,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
Try uninstalling the application on my phone and reinstall but nothing, does not this work for me.
Use this website to create and run the application again immediately, it gives the exact data.
Google Developers

Getting wrong data throug GCM push notification

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

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