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 user groups in the database. If the user clicks the "Notify group 1" button in the Android application, I need to send a notification to all users of group 1. How do I implement this logic? I think the same logic is used in group chat.

Can you provide me sample code for android and server?

Thanks .. Zachariah

+8
java android php mysql push-notification
source share
1 answer

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 a message over the Internet.

You can group users according to your needs:

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 sends a message over the Internet with a 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, you can integrate your query using MySQL with PhP to achieve what you want. Here you have the server side with an example PhP :

 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 will find some additional examples that may arise in the hand, including system logic:

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

+5
source share

All Articles