Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Android Device As Server to send message to GCM directly

is there any possible way to make my android device act as a server to send gcm message for another device. i know the way using webservice and all. but i want to use only android device to send gcm msz directly.

thanks.

like image 826
Roodie Avatar asked Nov 12 '22 05:11

Roodie


1 Answers

To write the server-side application on android device :

Copy the gcm-server.jar file from the SDK's gcm-server/dist directory to your server classpath.

Create a servlet (or other server-side mechanism) that can be used by the Android application to send the registration ID received by GCM . The application might also need to send other information—such as the user's email address or username—so that the server can associate the registration ID with the user owning the device. Similarly, create a servlet used to unregister registration IDs. When the server needs to send a message to the registration ID, it can use the com.google.android.gcm.server.Sender helper class from the GCM library.

For example:
import com.google.android.gcm.server.*;

Sender sender = new Sender(myApiKey);
Message message = new Message.Builder().build();
MulticastResult result = sender.send(message, devices, 5);

The snippet above does the following:

Creates a Sender object using your project's API key. Creates a message using a given registration ID (the message builder also has methods to set all message parameters such as the collapse key and payload data). Sends the message with a maximum of 5 retry attempts (in case the GCM servers are unavailable), and stores the response on result. It's now necessary to parse the result and take the proper action in the following cases:

If the message was created but the result returned a canonical registration ID, it's necessary to replace the current registration ID with the canonical one. If the returned error is NotRegistered, it's necessary to remove that registration ID, because the application was uninstalled from the device. Here's a code snippet that handles these 2 conditions:

if (result.getMessageId() != null) {
 String canonicalRegId = result.getCanonicalRegistrationId();
 if (canonicalRegId != null) {
   // same device has more than on registration ID: update database
 }
} else {
 String error = result.getErrorCodeName();
 if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
   // application has been removed from device - unregister database
 }
}

The Required permissions are:

    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" /> 
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
<permission android:name="my_app_package.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="my_app_package.permission.C2D_MESSAGE" /> 

Also you would require : com.google.android.c2dm.permission.SEND permission

For more info please explore more..Please do not ask util and unless you explored the things well. Referal Link

like image 64
Arpit Garg Avatar answered Nov 14 '22 23:11

Arpit Garg