Logo Gaby Becker

Locus Maps App - Offline Maps + Geocaching
Google Currents Feedreader für Android
WordPress App zum Blogartikel schreiben von unterwegs
Play Music - App - der Musicplayer für unterwegs
IMDb Kino App für die Kinovorschau

Add new comment

android_oma's picture

Sending Push notifications of Drupal 7 to registered Android smartphones

Add a Service part 1Since 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. 

The user had to update the events time to time, and it was not a nice thing to do. To save the user from doing the above task, we came up with the idea of the GCM [Google Cloud Mwssaging]. The GCM service ensbles you to send messages to each registered device via push messages. In our case such a message would be setting a new event or change an existing one.
 
To connect Drupal with Android is quite tricky. After severela trial we proceeded as the following.
 
there were 3 modules which were installed
  • 1. Services (essential because it provides the availability of the REST server )

               install services module

              install module push-notifications

  • 3. GCM [Google Cloud Messaging] (to control the sending of messages using the Rules)

               install gcm module

 
Setting up the modules
 
  • 1.1 enable services 
  • 1.2 Enable REST-server
  • 1.3 Setting up endpoints

               add a service part 1

              add a service part 2

              add a service part 3

              add a service part 4

              add a service part 5

              add a service part 6

              add a service part 7

              configure push notifications module part 1

              configure push notifications module part 2

              configure push notifications module part 3

  • 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

              configure gcm module

  • 3.1 Setting up the Rules
  • 3.1.1 Rule for the sending of messages

              create rule for registration part 1

              create rule for registration part 2

              create rule for registration part 3

              create rule for registration part 4

  • 3.1.2 Rule for a notification when registrations ids are no longer active, so they can be deleted from the database.

              create rule for unregistration

              create rule for unregistration part 2

 
At this point  everything is done at the Drupal site already. Now we need to establish the GCM on the Android App page. We will discuss more on this later.
I found a little bug in GCM-Module, Which was notified to the developer via Drupal.org and Twitter. He was extrelemely fast fixing the bug and created a new version for installation. 
Then I had the stupid idea to change the code from the GCM module itself, because I wanted to get the RegistrationIds [token field here] from the database table for the push notifications module. In the native mode of the GCM module the IDs are - comma separated.

The developer also had the good idea: easy to use PHP code in the text field of the token.

I was not aware until this email in text format that I can use this in each and any text field, if "PHP code" is enabled [only for admins]. In the rules I had an action "Execute custom PHP code". But I could not use because the GCM module did not provide this.

So it was easier to use php code into the tokens-field.

Here is the PHP code in the action token field.
 
This code, I tried first, will not run. The function GROUP_CONCAT can only be used for a defined field-length [I have read length from 521 until 1024]. For our tokens-field it is too short. 
 

<?php $result = db_query("SELECT GROUP_CONCAT(token SEPARATOR  ',') FROM {push_notifications_tokens}")->fetchField();  echo $result; ?>  

For this reason at least I used this code into the tokens-field of the rule:

<?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; 
?>

So I read all the tokens in the form of a comma-separated list. Exactly what is expected in the field. Deleting the  inactive ids I have not yet implemented.
At present this is still easily doable manually by deleting the tokens in the database table.
 
Now the todos for the Android side. 
Here the previously generated endpoint, the URL and the project number must be passed so that the notifications can be processed. 
An extract from the Code can be found here:
The URLs and endpoints are first defined in another class as constants:
 

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 = new ArrayList(2);
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);
}
}

In our case, "type" fix = android. The push notifications module is able to save even iPhone tokens. So here's the differences.
 
All other methods are customized to develop according to the requirements.
I am currently in email contact with the developer of the GCM module. We are expected to work together to expand the GCM module so that it does not require the push notifications table. Thus, it would be possible only with two modules to control the sending of messages to smartphones. But it may take a while. Nevertheless, we would be delighted if other Drupal developers would participate.
  
Bitte geben Sie Ihre E-Mail-Adresse ein. Sie wird nicht angezeigt.
@

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.