forked from commonsguy/cw-omnibus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
command line "server" for sending GCM messages
- Loading branch information
1 parent
e7b3302
commit 174d194
Showing
9 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="lib" path="libs/gcm-server.jar"/> | ||
<classpathentry kind="lib" path="libs/commons-cli-1.2.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>GCMCommand</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
java -cp libs/commons-cli-1.2.jar:libs/gcm-server.jar:libs/json_simple-1.1.jar:dist/gcm-cmd.jar com.commonsware.android.gcm.cmd.GCM "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<jardesc> | ||
<jar path="GCMCommand/dist/gcm-cmd.jar"/> | ||
<options buildIfNeeded="true" compress="true" descriptionLocation="/GCMCommand/gcm.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="false" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/> | ||
<storedRefactorings deprecationInfo="true" structuralOnly="false"/> | ||
<selectedProjects/> | ||
<manifest generateManifest="true" mainClassHandleIdentifier="=GCMCommand/src<com.commonsware.android.gcm.cmd{GCM.java[GCM" manifestLocation="" manifestVersion="1.0" reuseManifest="false" saveManifest="false" usesManifest="true"> | ||
<sealing sealJar="false"> | ||
<packagesToSeal/> | ||
<packagesToUnSeal/> | ||
</sealing> | ||
</manifest> | ||
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false"> | ||
<javaElement handleIdentifier="=GCMCommand/src"/> | ||
<file path="/GCMCommand/gcm.jardesc"/> | ||
<file path="/GCMCommand/.classpath"/> | ||
<file path="/GCMCommand/.project"/> | ||
</selectedElements> | ||
</jardesc> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
126 changes: 126 additions & 0 deletions
126
Push/GCMCommand/src/com/commonsware/android/gcm/cmd/GCM.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/*** | ||
Copyright (c) 2012 CommonsWare, LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
use this file except in compliance with the License. You may obtain a copy | ||
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required | ||
by applicable law or agreed to in writing, software distributed under the | ||
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
OF ANY KIND, either express or implied. See the License for the specific | ||
language governing permissions and limitations under the License. | ||
From _The Busy Coder's Guide to Android Development_ | ||
http://commonsware.com/Android | ||
*/ | ||
|
||
package com.commonsware.android.gcm.cmd; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import com.google.android.gcm.server.Constants; | ||
import com.google.android.gcm.server.Message; | ||
import com.google.android.gcm.server.MulticastResult; | ||
import com.google.android.gcm.server.Result; | ||
import com.google.android.gcm.server.Sender; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.CommandLineParser; | ||
import org.apache.commons.cli.HelpFormatter; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.OptionBuilder; | ||
import org.apache.commons.cli.Options; | ||
import org.apache.commons.cli.PosixParser; | ||
|
||
public class GCM { | ||
@SuppressWarnings("static-access") | ||
public static void main(String[] args) { | ||
Option helpOpt=new Option("h", "help", false, "print this message"); | ||
Option apiKeyOpt= | ||
OptionBuilder.withArgName("key").hasArg().isRequired() | ||
.withDescription("GCM API key") | ||
.withLongOpt("apiKey").create('a'); | ||
Option deviceOpt= | ||
OptionBuilder.withArgName("regId").hasArg().isRequired() | ||
.withDescription("device to send to") | ||
.withLongOpt("device").create('d'); | ||
Option dataOpt= | ||
OptionBuilder.withArgName("key=value").hasArgs(2) | ||
.withDescription("datum to send") | ||
.withValueSeparator().withLongOpt("data") | ||
.create('D'); | ||
|
||
Options options=new Options(); | ||
|
||
options.addOption(apiKeyOpt); | ||
options.addOption(deviceOpt); | ||
options.addOption(dataOpt); | ||
options.addOption(helpOpt); | ||
|
||
CommandLineParser parser=new PosixParser(); | ||
|
||
try { | ||
CommandLine line=parser.parse(options, args); | ||
|
||
if (line.hasOption('h') || !line.hasOption('a') | ||
|| !line.hasOption('d')) { | ||
HelpFormatter formatter=new HelpFormatter(); | ||
formatter.printHelp("gcm", options, true); | ||
} | ||
else { | ||
sendMessage(line.getOptionValue('a'), | ||
Arrays.asList(line.getOptionValues('d')), | ||
line.getOptionProperties("data")); | ||
} | ||
} | ||
catch (org.apache.commons.cli.MissingOptionException moe) { | ||
System.err.println("Invalid command syntax"); | ||
HelpFormatter formatter=new HelpFormatter(); | ||
formatter.printHelp("gcm", options, true); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static void sendMessage(String apiKey, List<String> devices, | ||
Properties data) throws Exception { | ||
Sender sender=new Sender(apiKey); | ||
Message.Builder builder=new Message.Builder(); | ||
|
||
for (Object o : data.keySet()) { | ||
String key=o.toString(); | ||
|
||
builder.addData(key, data.getProperty(key)); | ||
} | ||
|
||
MulticastResult mcResult=sender.send(builder.build(), devices, 5); | ||
|
||
for (int i=0; i < mcResult.getTotal(); i++) { | ||
Result result=mcResult.getResults().get(i); | ||
|
||
if (result.getMessageId() != null) { | ||
String canonicalRegId=result.getCanonicalRegistrationId(); | ||
|
||
if (canonicalRegId != null) { | ||
System.err.println(String.format("%s canonical ID = %s", | ||
devices.get(i), | ||
canonicalRegId)); | ||
} | ||
else { | ||
System.out.println(String.format("%s success", devices.get(i))); | ||
} | ||
} | ||
else { | ||
String error=result.getErrorCodeName(); | ||
|
||
if (Constants.ERROR_NOT_REGISTERED.equals(error)) { | ||
System.err.println(String.format("%s is unregistered", | ||
devices.get(i))); | ||
} | ||
else if (error != null) { | ||
System.err.println(String.format("%s error = %s", | ||
devices.get(i), error)); | ||
} | ||
} | ||
} | ||
} | ||
} |