I've recently been learning Android Development and I am trying to make a sample application which uses Google Cloud Messaging. My goal is to make a simple application which can receive Push notifications from a server. I've gotten the client side of the application to work by registering my device. Now I am trying to create the server side. However, I have absolutely no experience in setting up a server or programming on the server side. So I was hoping someone could point me in the right direction so that I could have a server sending the Push notifications. I have been following the tutorial on this link but I am stuck at the server implementation. I would greatly appreciate it if someone could point me in the right direction. Thanks!
Actually is more easier using Tomcat or AppEngine. See this tutorial in how to setup your GCM Server.
You need the device registration id to which you want to send the message on the server side you will need your API key, this is a JSP example :
http://yourdomain.com:8080/sendMessage.jsp?registrationID=kSADAS3242&messageToSend=Hello
String value = request.request.getParameter("messageToSend");
String registrationId = request.getParameter("registrationID");
Sender sender = new Sender("YOUR API KEY");
Message message = new Message.Builder().addData("FLAG","SERVE").addData("MSG", value).build();
Result result = sender.send(message, registrationId, 5);
On your client device should expect :
#Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Got a message from Google Cloud Messaging !!");
String tag = intent.getExtras().getString("FLAG");
String message = intent.getExtras().getString("MSG");
Log.i(TAG, tag + " : " + message);
}
This should print "SERVE : Hello"
If you have used PHP you should be familiar with xampp or similar software.
If not, all you have to do is download and install it, start the services and on your browser go to:
http://localhost/xampp
to test if it's properly installed.
If you can see the Xampp page you can start running scripts php from xampp/htdocs and run them like this:
http://localhost/yourscript.php
Try a simple hello world:
<?php
echo 'hello world';
?>
After that you should be ready to start following this tutorial or any tutorial in google just typing gcm php tutorial
I found that php is the most simple way for me to configure the server side for GCM, hope you find it useful...
Related
I am trying to create an Android chat client using ejabberd XMPP server (19.02), Smack library (4.2.4) and Android SDK 25 using Android Studio.
I followed the example app found here: https://www.blikoontech.com/tutorials/android-smack-xmpp-introductionbuilding-a-simple-client
All is working well and I can send messages between two different Android devices running that sample app.
In ejabberd, there are options to send messages to the clients directly from the server using a CLI tool called ejabberdctl or ejabberd REST API. When I sent messages that way, the Android client doesn’t receive those messages. I tried with other clients like Conversations and Gajim and they could all receive it. I am pretty sure messages sent using those methods arrived because they were received as offline messages (on ejabberd web admin) when sent to offline clients.
Here is the part of the Android (java) code (roosterconnection.java from that sample app) that is to receive incoming messages. Please suggest me if I am missing anything. Thanks a lot.
ChatManager.getInstanceFor(mConnection).addIncomingListener(new IncomingChatMessageListener() {
#Override
public void newIncomingMessage(EntityBareJid messageFrom, Message message, Chat chat) {
///ADDED
Log.d(TAG,"message.getBody() :"+message.getBody());
Log.d(TAG,"message.getFrom() :"+message.getFrom());
String from = message.getFrom().toString();
String contactJid="";
if ( from.contains("/"))
{
contactJid = from.split("/")[0];
Log.d(TAG,"The real jid is :" +contactJid);
Log.d(TAG,"The message is from :" +from);
}else
{
contactJid=from;
}
//Bundle up the intent and send the broadcast.
Intent intent = new Intent(RoosterConnectionService.NEW_MESSAGE);
intent.setPackage(mApplicationContext.getPackageName());
intent.putExtra(RoosterConnectionService.BUNDLE_FROM_JID,contactJid);
intent.putExtra(RoosterConnectionService.BUNDLE_MESSAGE_BODY,message.getBody());
mApplicationContext.sendBroadcast(intent);
Log.d(TAG,"Received message from :"+contactJid+" broadcast sent.");
///ADDED
}
});
Here is a possible explanation, based in my experiments with a desktop client, Tkabber:
I login to ejabberd using Tkabber client, account user1#localhost, resource tka1, priority -3. The negative priority in this experiment is important.
Then I execute the command to send to full JID, including the correct resource:
ejabberdctl send_stanza aaa#localhost user1#localhost/tka1
"<message>..."
The client receives the stanza correctly.
Now I send to bare JID (without providing resource), and another setting another resource:
ejabberdctl send_stanza aaa#localhost user1#localhost
"<message>..."
ejabberdctl send_stanza aaa#localhost user1#localhost/sdsd
"<message>..."
In those cases, none of them are received by the client, because the resource doesn't match, and because its priority is negative. I can see those messages stored offline in the database.
In your client, maybe you have to add another call to set the presence online, with a positive priority.
I want to make a html website. And everytime someone downloads something of my site, I want to get a notification on my phone. I have very little knowledge in Android app making but I do have some in html. Is there a way to do this? And where can I learn to do this? Wich programming language?
Thank you for the answers! I will start working on it.
Website part
Here you will need a server-side script language like ASP.NET or PHP to build your website where it will have some code to handle the part of capturing the click on the linked item you are showing on the site, getting its information like its name and then pushing a message to Google cloud messaging (GCM). You need to register with GCM to obtain Sender ID to use to it publish your message from your website's server-side code.
Well, this is quite a large topic, I will list down some useful resources to get you started with the development in website
ASP.NET Tutorial
PHP Tutorial
Google Cloud Messaging
Android part
Here you want to create a simple app, that has a service that subscribes to GCM (using the same GCM sender id) to obtain registration token and then it will be able to read the push notification messages sent from GCM via the onMessageReceived Method
#Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
// Handle received message here.
}
Some useful resources for android and GCM:
Android tutorial
Android GCM
GCM Push notifications
Your restricted materials, like .pdf files can be handled by a request.
Az easy implementation it would be with PHP. Because you aren't exposing the .pdf directly it will be called a downloads.php. In the .php file you will make a counter and store it in a file or database and set up a Google messaging system and send a notification to Android. That's all.
I am following instructions at :
https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-android-push-notification-google-gcm-get-started/#sending-push-notifications
Instructions say:
// If you want to use tags...
// Refer to : https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-routing-tag-expressions/
// regID = hub.register(token, "tag1,tag2").getRegistrationId();
Based on that I did the following:
added regID assignment to my code
My code looks like RegistrationIntentService example via Microsoft
except the line 48
regID = hub.register(token).getRegistrationId();
is something like this
regID = hub.register(token, "tag1").getRegistrationId();
didn't really implement anything in the reference link. Reference link assumes that I have an actual server. Right now I am just testing from Azure trouble shooting test send center. (will set up server latter).
Right now from Azure trouble shooting test send center I can't send a message to 'tag1'. I know my payload is set up right but my tag is not getting registered in RegistrationIntentService.java as suggested in instructions.
Any suggestions?
Your tags are being added correctly. I have a working version with notifications that send upstream messages to tags, and the tags are created in the exact manner you describe in your question.
The problem is Azure. I have yet to find the correct way to send a payload with a tag on the test send server in Azure. The typical GCM format of
"to": "/topics/foo-bar",
"data": {
"message": "This is a GCM Topic Message!",
}
does not work. I believe this is because Azure test send server is doing something in between when it sends the request on to GCM.
When you setup your server, your tags should be working as expected.
I want to send push notification to individual using android GCM. have created the app in SNS. I am using aws-sdk v1.4 for Ruby.
When I send through Amazon web interface I receive the messages, but When I publish it through using the code below, I get blank messages. what is the right message format to send?
sns = AWS::SNS::Client.new
endpoint = sns.create_platform_endpoint(platform_application_arn:my_token)
sns.publish(target_arn:endpoint:endpoint_arn, message: "GCM:{data:{message:"GCM:{data:{message:'hello'}")
Please help.
TIA
{"GCM": "{ \"data\": { \"message\": \"hello\" } }"}
Do not call to_json method also set the subject parameter in publish.
The message should be a valid JSON string if you want to publish a message to GCM. You can use to_json to serialize a hash object to JSON. Here is a article about using AWS SDK for Ruby http://blog.tryneighborly.com/amazon-sns-for-apns-on-rails/.
For more information about Amazon SNS:
Send Custom Platform-Specific Payloads in Messages to Mobile Devices
Getting Started with Google Cloud Messaging for Android
Ruby doc for Aws::SNS::Client
I am trying GCM based android app to push messages from server to android client. I am able to push fix string with the following coe. I am wondering about the ways to push XML file from server and parse at the android application. I have done some research but I couldn't find push XML rather I found send XML file. Thank you
if (androidArray.size() == 1) {
String registrationId = androidArray.get(0);
Message message = new Message.Builder()
.collapseKey(collapseKey)
.timeToLive(30)
.delayWhileIdle(true)
.addData("message", Message)
.build();
Result result = sender.send(message, registrationId, 5);
You don't push xml (or JSON preferably) to the android app. You send a simple message to the app.
when the app receives the message it then needs to go and pull the xml/json from the website with an http get request to the relevant url that will supply the xml.
The android app can then parse the response and do whatever you want it to.
Here is an EXCELLENT tutorial on C2DM (The forerunner to GCM) http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html
You should be able to work out the differences needed.
UPDATE
Google Android has a complete section on GCM which can be found here
http://developer.android.com/google/gcm/index.html
Within that link there are getting started guides and a GCM Demo app
There are limits to the amount of data you can send and you should not rely on your data not ever exceeding the limits or Google arbitrarily changing the amount of data you are allowed to send.
Should either of those occur you would need to update your app so just do it right in the first place.
The message you send should act as a "key" to determine what action to take when the message is received.
UPDATE
If you are feeling REALLY adventurous you could use a custom sync adapter to help you consume your web services. It's pretty advanced stuff but if you are feeling curious about this then watch the Google I/O seminar on consuming RESTfull web services http://www.youtube.com/watch?v=xHXn3Kg2IQE