Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending messages(notification) to a group - Android

I am writing an android application in which i need to send a notification to a group of users.

I have two group of users in the database. If a user press a button "Notify Group 1" in the android app , i need to send the notification to all the group 1 users. How i implement this logic? I think the same logic is using in the group chat .

Can you provide me the sample code for android and server side?

Thanks.. Zacharia

like image 672
zacharia Avatar asked Mar 17 '14 05:03

zacharia


1 Answers

Using Android Cloud to Device Messaging is the best approach, as you can easily integrate it with MySQL and PhP, having the necessary tools to send message over internet.

You can group users according to your need:

 CREATE TABLE IF NOT EXISTS `gcm_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gcm_regid` text,
  `name` varchar(50) NOT NULL,
  `email` varchar(255) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Source - AndroidHive

Here you have a typical example with a Java class that send message over internet with Google cloud:

public class MessageUtil {
      private final static String AUTH = "authentication";

      private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";

      public static final String PARAM_REGISTRATION_ID = "registration_id";

      public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";

      public static final String PARAM_COLLAPSE_KEY = "collapse_key";

      private static final String UTF8 = "UTF-8";

      public static int sendMessage(String auth_token, String registrationId,
          String message) throws IOException {

        StringBuilder postDataBuilder = new StringBuilder();
        postDataBuilder.append(PARAM_REGISTRATION_ID).append("=")
            .append(registrationId);
        postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=")
            .append("0");
        postDataBuilder.append("&").append("data.payload").append("=")
            .append(URLEncoder.encode(message, UTF8));

        byte[] postData = postDataBuilder.toString().getBytes(UTF8);

        // Hit the dm URL.

        URL url = new URL("https://android.clients.google.com/c2dm/send");
        HttpsURLConnection
            .setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded;charset=UTF-8");
        conn.setRequestProperty("Content-Length",
            Integer.toString(postData.length));
        conn.setRequestProperty("Authorization", "GoogleLogin auth="
            + auth_token);

        OutputStream out = conn.getOutputStream();
        out.write(postData);
        out.close();

        int responseCode = conn.getResponseCode();
        return responseCode;
      }

      private static class CustomizedHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
          return true;
        }
      }
 } 

To wrap it up, then you can integrate you query using MySQL with PhP to achieve what you want. Here you have the server side with the PhP example:

send_message.php
<?php
if (isset($_GET["regId"]) && isset($_GET["message"])) {
    $regId = $_GET["regId"];
    $message = $_GET["message"];

    include_once './GCM.php';

    $gcm = new GCM();

    $registatoin_ids = array($regId);
    $message = array("price" => $message);

    $result = $gcm->send_notification($registatoin_ids, $message);

    echo $result;
}
?>

Here you find some additional examples that might come in hand, including logic of the system:

  • Google cloud messaging sample
  • Android Push Notifications using Google Cloud Messaging GCM - Android Example
  • Google Cloud Messaging For Android (GCM) Simple Tutorial
  • Google Cloud Messaging GCM for Android and Push Notifications

Taking from here you can get your App with the features you have in mind.

like image 76
Avanz Avatar answered Oct 14 '22 22:10

Avanz