Since early May (2013) our Webgrrls Events app is available in playstore. Originally, it was designed so that the user himself get the events after pressing refresh button. By this action we wanted to prevent using the webservices when there is no internet.
- 1. Services (essential because it provides the availability of the REST server )
- 2. Push Notifications (to save the RegistrationsIds of Android devices in the database)
- 3. GCM [Google Cloud Messaging] (to control the sending of messages using the Rules)
- 1.1 enable services
- 1.2 Enable REST-server
- 1.3 Setting up endpoints
- 2. Setting up the Push Notifications (The developer has written a tutorial here: http://www.danielhanold.com/story/drupal-7-push-notifications-how-to-reg...)
- 2.1 In addition, the user rights for Push Notifications must be set up so that guests can also send Notifications - so that the registration id if the device can be stored.
- 3. setting up the GCM
- 3.1 Setting up the Rules
- 3.1.1 Rule for the sending of messages
- 3.1.2 Rule for a notification when registrations ids are no longer active, so they can be deleted from the database.
The developer also had the good idea: easy to use PHP code in the text field of the token.
So it was easier to use php code into the tokens-field.
<?php $result = db_query("SELECT GROUP_CONCAT(token SEPARATOR ',') FROM {push_notifications_tokens}")->fetchField(); echo $result; ?>
<?php
$tokens = "";
$result = db_query("SELECT token FROM {push_notifications_tokens}");
while($row = $result->fetchAssoc()) {
$tokens .= $row['token'] . ", ";
}
$tokens = substr($tokens, 0, strlen($tokens) - 1);
echo $tokens;
?>
public static final String YOUR_URL =http://www.yourwebsite.com/;
public static final String REGISTER_URI=mobile_data/push_notifications;
public static final String UNREGISTER_URI=mobile_data_unregister/push_notifications;
@Override
protected void onRegistered(Context arg0, String regId) {
Log.d("onRegistered", regId);
int statusCode = -1;
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(Constants.YOUR_URL + Constants.REGISTER_URI);
List
nameValuePairs.add(new BasicNameValuePair("token", regId));
nameValuePairs.add(new BasicNameValuePair("type", "android"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
statusCode = statusLine.getStatusCode();
Log.d("Webgrrls Events user register, statusCode", statusCode + "");
} catch (Exception e) {
Log.d("Webgrrls Events user register, exception", e.toString());
GCMRegistrar.unregister(arg0);
}
}
Comments
Android GCM
but a have an error
package com.domaine.app;
com.peopleinput.pospatisen/com.peopleinput.pospatisen.MainActivity}:
ZygoteInit.java:795)
Have you adjusted the Android Manifest?
Hi tgzbeum,
have you adjusted the Android Manifest? Please have a look at this page: http://developer.android.com/google/gcm/gs.html
Hi android_oma, thanks to
Hi android_oma, thanks to help.
finally I see the problem : a mistake in SENDER_ID.
don´t get any message from drupal
Hello android_oma, congratulations for your tutorial! i followed it step by step but i didn´t get it..
I also followed his tutorial https://developers.google.com/eclipse/docs/endpoints-androidconnected-gae and i modified the onRegistered method with your code but nothing happened... in log cat i can see Log.d("onRegistered", regId) with its code and also i can see Log.d("Webgrrls Events user register, statusCode", statusCode + ""); with statuscode = 503 and after that, tag = GCMBaseIntentService , text = Releasing wakelock, but nothing more...
From my drupal site i try to send a message but i get "No Android recipients found, potentially for this language." all the time. I don´t know if i´m doing something wrong and what i must do because I can´t see any device registered from drupal.
one more thing, a stupid question:
I have public static final String REGISTER_URI = "mobile_data/push_notifications"; i suppose that i don´t have to reemplace "mobile_data/push_notifications" with any code, don´t I?
Thanks in advance for your help, if you need any code fragment or what else, tell me please.
Could it be a problem with user permissions?
Hello Juan,
thanks for your question. First of all, if your endpoint was defined as "mobile_data/push_notifications" and you want to use it as constant, than you should not change the code. We have defined our constants into a different package. Might be, you want to go a different way.
Your other question: is it possible, that the following 2 permissions are not enabled in your durpal - site?
Register Device Token --> Guest (should be enabled)
Remove Device Token --> Guest (should be enabled)
Solved!
Hi android_oma! thanks for your fast answer, finally i could solve it!
I explain what i did:
First of all i had my drupal site in maintenance mode and that was the reason of statuscode = 503. After changing this parameter i got status code = 401.
I solved this giving permissions to everyone in people/permissions section (scroll down to "Push Notifications", and mark down whichever permissions you want) so in this way i got status code = 200 (everything ok) and I could send a push notification.
Now I´d like to develop my own "code.php" in order to send a push notification to a specific user when someone access to a public page in my site. Could you give me any clue for doing this? I´m a beginner on this and I don´t know if I must use rules, or looking at the code of push notifications module and rewrite it in my code.php, or there is another easier way..
Thanks again for your help, I doubt a lot that I had achieved it without your tutorial.
How do you want to identify a special user for sending notif.
Hi Juan,
that sounds great, that my answer could help you in one part.
The methode, I used to send push notification was by rules. But I had not the requirement to identify one special user for it. Because, the regId is every time anonymous. If you know the regId of this specific user (might be your own regId - will be given via debugging on your own phone or during tests), you could work with rules for it.
Hi android_oma, thanks for
Hi android_oma, thanks for your advise.
Just one more question referring to the unregister.
I can´t access to mobile_data_unregister/push_notifications service from the app, when i try it, i get status code = 400.
if I try:
mobile_data -- > status code = 200
mobile_data_register/push_notifications -- > status code = 200
mobile_data_unregister -- > status code = 200
but with mobile_data_unregister/push_notifications -- > status code = 400.
Users have also permissions to remove Device Token and i do exactly the same than in "onRegister" method but obviously with UNREGISTER_URI constant so i don´t know what can be wrong with the unregister service... Any clue?
Thanks in advance,
Juan.
unregistration
Hello Juan,
I haven't implemented the unregistration. Until now it was not necessary, because there are not so many regIds to unregister. So I send me an email (through a rule) with the deactivated regIds and delete them manual with a sql-statement into the database. In some weeks I will find the way to do it also automatic via android and Drupal. May be some other readers of this article tried it and could leave a comment here. If not, I will post the solution after finding it.
Sorry for my english,
Sorry for my english,
have you solved the automatic deletion of inactive token ?
Error 404 Not Found. Could not found the Controller
When I tried to access to the url http://serveur/mobile_data/push_notifications?type=android, I have this error Error 404 Not Found. Could not found the Controller.
Any Ideas? Thanks to help
Hi android_oma
Hi android_oma
Finally I resolve the problem with 'The controller can not be found'.
Thanks.
Now I try to send notifications with rules but the push notifications (point 3.1.1 in the tutorial) I receive in my phone is empty (no text). I use title=[node:title] in the KEY-VALUE block. Any Ideas?
The controller can not be found
Hello how did you resolve the problem with "The controller can not be found"?
Hi, I've followed all the
Hi, I've followed all the step you stated and I manage to register my device to Drupal as token using sample code Demo activity from android developer training, but when I try to send push notification from drupal, it says
"Successfully sent 0 Android push messages (attempted to send 1 messages)."
why is this happening? I ran out of solution and I've been trying to solve this for days. Please help me.
I am in the same boat:
I am in the same boat: "Successfully sent 0 Android push messages (attempted to send 1 messages)."
Still unsure why this happens. Will report if I find a solution.
The error: "Successfully sent
The error: "Successfully sent 0 Android push messages (attempted to send 1 messages).", in my case, turned out to be a 401 unauthorized issue as return by the google api. Even though I had whitelisted my ipv4 address, I had not done so for my ipv6 address. Adding this solved the problem at once.
Thanks for this great write up
That's great
Hello ArenDroid,
thanks for sending the solution of the problem. It will help some other developer.
Same problem
I am also getting the error "Successfully sent 0 Android push messages (attempted to send 1 messages). I didn't quite understand ArenDroid's solution; do you mean the ip address for the server? Please explain in detail.
Same problem can you help
I am getting same problem tried all solutions but no luck. Can you please explain exactly where did you whitelist your IPs. I am not able to find any provision for the same inside google cloud project.
Thank you in advance. Please reply.
Same problem
I have registered my mobile device following the instructions given in this page. I am implementing GCM in an android application and am not able to send push notifications from the Drupal site.
"Successfully sent 0 Android push messages (attempted to send 3 messages)."
Don't know what is wrong and how to resolve it. ArenDroid's solution is not working for me. Can anybody please help?
Hi,My dear friend
Wow,Fantastic article,it's so helpful to me,and your blog is very good, I've learned a lot from your blog here,Keep on going,my friend,I will keep an eye on it,One more thing,thanks for your post!
Drupal push
Hi there,
I followed the whole procedure. Installing modules Services, Push Notifications, GCM. So far, so good. Ending on www.website.com/mobile_data/push_notification this results in a empty white page... no source.
Any ideas? Thanks in advance. Best regards.
empty white page
Hello Hans,
are you sure, that you have set the endpoint in the right way?
enabling php code writing
Hi,
nice tutorial,
can you explain me in detail that how did you enable php code feature to write php code in the text field of gcm module to retrieve all tokens from database table.
Might be, an upload of the complete Android-Code will help
Hello together,
because of the fact, that so many people have problems with this, I think, the best solution is to upload the complete android-code to Git-Hub. Please be patient, I have actually no account and I haven't worked with Git-Hub. Thatswhy I need help from a colleague to do this. If it is done, I will publish the URL here.
Help
Hi ,
I follow all the steps and same code as your.
I also got a GCM red Id..
But when I m post the url with token and type.
It give 406 Status code .
Please anyone help me to solve this issue.
Push notification not receive
Hi,
I follow all the steps as tutorial.
My device is successfully register on server by using GCM Id.
My problem is that , from server when sending push notification it shows 'Successfully send notification' , but this notification is not receive on phone.
Can you please suggest me what should I do....
How to overcome Error 406 at Android Side Endpoint connection.
Hello,
I am getting 406 status code while connecting to drupal endpoint. How to solve this?
Its urgent, please help me in this.
406 Error
Hello Anuj, I am not sure, if I can help, because I don't know your code and other properties. On the last weekend I tried to update Piwik on my Server. Piwik needs php-version 5.3.3 or higher. The installation of the webgrrls-rheinmain-website was under php 5.3.2. After the update, the webgrrls-events-app could not get a connection to the website. After reading some stack-overflow-articles I found out, that the php-version is the reason for the error. The Push Notification Module will not work with it.
If this information will not help you, so might be, it can help the others.
Add new comment