|
1 | 1 | package com.pubnub.examples; |
| 2 | +import org.apache.commons.cli.*; |
| 3 | + |
2 | 4 |
|
3 | 5 | import org.json.JSONArray; |
4 | 6 | import org.json.JSONException; |
|
15 | 17 | import com.pubnub.api.PubnubSenderMissingException; |
16 | 18 |
|
17 | 19 | public class PubnubPushSampleCode { |
18 | | - |
19 | | - String publish_key = "pub-c-c077418d-f83c-4860-b213-2f6c77bde29a"; |
20 | | - String subscribe_key = "sub-c-e8839098-f568-11e2-a11a-02ee2ddab7fe"; |
21 | | - String secret_key = "sec-c-OGU3Y2Q4ZWUtNDQwMC00NTI1LThjNWYtNWJmY2M4OGIwNjEy"; |
22 | | - |
23 | | - String channel = "abcd"; |
24 | | - |
25 | | - String auth_key = "abcd"; |
26 | 20 |
|
| 21 | + private static void usage(Options options){ |
| 22 | + |
| 23 | + HelpFormatter formatter = new HelpFormatter(); |
| 24 | + formatter.printHelp( "Publisher", options ); |
| 25 | + } |
27 | 26 |
|
28 | 27 | public void start() { |
29 | | - Pubnub pubnub = new Pubnub(publish_key, subscribe_key); |
30 | | - pubnub.setAuthKey("abcd"); |
| 28 | + |
| 29 | + |
| 30 | + } |
| 31 | + /** |
| 32 | + * @param args |
| 33 | + */ |
| 34 | + public static void main(String[] args) { |
| 35 | + Options options = new Options(); |
| 36 | + String publish_key = "demo"; |
| 37 | + String subscribe_key = "demo"; |
| 38 | + boolean nativ = false; |
| 39 | + boolean gcm = false; |
| 40 | + boolean apns = false; |
| 41 | + |
| 42 | + String channel = "my_channel"; |
| 43 | + |
| 44 | + String origin = "pubsub"; |
| 45 | + |
| 46 | + String auth_key = ""; |
| 47 | + |
| 48 | + CommandLine cmd = null; |
| 49 | + |
| 50 | + options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("publish_key"). |
| 51 | + withType(String.class).withDescription("Publish Key ( default: 'demo' )").create()); |
| 52 | + |
| 53 | + options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("subscribe_key"). |
| 54 | + withType(String.class).withDescription("Subscribe Key ( default: 'demo' )").create()); |
| 55 | + |
| 56 | + options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("origin"). |
| 57 | + withType(String.class).withDescription("Origin ( Ex. pubsub )").create()); |
| 58 | + |
| 59 | + options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("auth_key"). |
| 60 | + withType(String.class).withDescription("Auth Key").create()); |
| 61 | + |
| 62 | + options.addOption(OptionBuilder.hasArg().withArgName("String").withLongOpt("channel"). |
| 63 | + withType(String.class).withDescription("Secret Key ( default: 'my_channel' )").create()); |
| 64 | + |
| 65 | + options.addOption(OptionBuilder.withLongOpt("apns").withDescription("APNS message").create()); |
| 66 | + |
| 67 | + options.addOption(OptionBuilder.withLongOpt("gcm").withDescription("GCM message").create()); |
| 68 | + |
| 69 | + options.addOption(OptionBuilder.withLongOpt("native").withDescription("Native message").create()); |
| 70 | + |
| 71 | + CommandLineParser parser = new BasicParser(); |
| 72 | + try { |
| 73 | + cmd = parser.parse( options, args); |
| 74 | + } catch (ParseException e1) { |
| 75 | + usage(options);return; |
| 76 | + } |
| 77 | + |
| 78 | + if (cmd.hasOption("publish_key")) { |
| 79 | + try { |
| 80 | + publish_key = cmd.getOptionValue("publish_key"); |
| 81 | + } catch (Exception e) { |
| 82 | + e.printStackTrace(); |
| 83 | + usage(options);return; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (cmd.hasOption("subscribe_key")) { |
| 88 | + try { |
| 89 | + subscribe_key = cmd.getOptionValue("subscribe_key"); |
| 90 | + } catch (Exception e) { |
| 91 | + e.printStackTrace(); |
| 92 | + usage(options);return; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (cmd.hasOption("origin")) { |
| 97 | + try { |
| 98 | + origin = cmd.getOptionValue("origin"); |
| 99 | + } catch (Exception e) { |
| 100 | + e.printStackTrace(); |
| 101 | + usage(options);return; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (cmd.hasOption("channel")) { |
| 106 | + try { |
| 107 | + channel = cmd.getOptionValue("channel"); |
| 108 | + } catch (Exception e) { |
| 109 | + e.printStackTrace(); |
| 110 | + usage(options);return; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (cmd.hasOption("auth_key")) { |
| 115 | + try { |
| 116 | + auth_key = cmd.getOptionValue("auth_key"); |
| 117 | + } catch (Exception e) { |
| 118 | + e.printStackTrace(); |
| 119 | + usage(options);return; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (cmd.hasOption("apns")) { |
| 124 | + try { |
| 125 | + apns = true; |
| 126 | + } catch (Exception e) { |
| 127 | + e.printStackTrace(); |
| 128 | + usage(options);return; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (cmd.hasOption("gcm")) { |
| 133 | + try { |
| 134 | + gcm = true; |
| 135 | + } catch (Exception e) { |
| 136 | + e.printStackTrace(); |
| 137 | + usage(options);return; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + if (cmd.hasOption("native")) { |
| 143 | + try { |
| 144 | + nativ = true; |
| 145 | + } catch (Exception e) { |
| 146 | + e.printStackTrace(); |
| 147 | + usage(options);return; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + final Pubnub pubnub = new Pubnub(publish_key, subscribe_key); |
| 153 | + pubnub.setAuthKey(auth_key); |
31 | 154 | pubnub.setCacheBusting(false); |
32 | | - pubnub.setOrigin("dara20.devbuild"); |
| 155 | + pubnub.setOrigin(origin); |
33 | 156 |
|
34 | 157 | // Create APNS message |
35 | 158 |
|
@@ -63,67 +186,41 @@ public void start() { |
63 | 186 | @Override |
64 | 187 | public void successCallback(String channel, Object response) { |
65 | 188 | System.out.println(response); |
| 189 | + pubnub.shutdown(); |
66 | 190 | } |
67 | 191 | @Override |
68 | 192 | public void errorCallback(String channel, PubnubError error) { |
69 | 193 | System.out.println(error); |
| 194 | + pubnub.shutdown(); |
70 | 195 | } |
71 | 196 | }; |
72 | 197 |
|
73 | 198 | PubnubSender sender = new PubnubSender(channel, pubnub, callback); |
74 | 199 |
|
75 | | - // Create message1 with apns, gcm and normal data |
| 200 | + PnMessage message = null; |
76 | 201 |
|
77 | | - PnMessage message1 = new PnMessage(sender, apnsMessage, gcmMessage); |
78 | | - try { |
79 | | - message1.put("test","hi"); |
80 | | - } catch (JSONException e1) { |
81 | | - |
| 202 | + if (apns && gcm) { |
| 203 | + message = new PnMessage(sender, apnsMessage, gcmMessage); |
| 204 | + } else if (apns) { |
| 205 | + message = new PnMessage(sender, apnsMessage); |
| 206 | + } else if (gcm) { |
| 207 | + message = new PnMessage(sender, gcmMessage); |
82 | 208 | } |
83 | | - |
84 | | - |
85 | | - // Create message2 with only apns payload |
86 | | - |
87 | | - PnMessage message2 = new PnMessage(sender, apnsMessage); |
88 | | - |
89 | | - |
90 | | - |
91 | | - // Create message3 with only gcm payload |
92 | | - |
93 | | - PnMessage message3 = new PnMessage(sender, gcmMessage); |
94 | | - |
95 | | - |
96 | | - // Create message4 with only no gcm or apns data |
97 | | - |
98 | | - PnMessage message4 = new PnMessage(sender); |
99 | | - try { |
100 | | - message4.put("data","mydata"); |
101 | | - } catch (JSONException e1) { |
| 209 | + if (message == null) message = new PnMessage(sender); |
| 210 | + if (nativ) { |
| 211 | + try { |
| 212 | + message.put("test","hi"); |
| 213 | + } catch (JSONException e1) { |
102 | 214 |
|
| 215 | + } |
103 | 216 | } |
104 | 217 |
|
105 | | - |
106 | | - // Create message4 with only gcm and apns data |
107 | | - |
108 | | - PnMessage message5 = new PnMessage(sender, apnsMessage, gcmMessage); |
109 | | - |
110 | | - |
111 | 218 | try { |
112 | | - message1.publish(); |
113 | | - message2.publish(); |
114 | | - message3.publish(); |
115 | | - message4.publish(); |
116 | | - message5.publish(); |
| 219 | + message.publish(); |
117 | 220 | } catch (PubnubSenderMissingException e) { |
118 | 221 | System.out.println("Set Sender"); |
119 | 222 | } |
120 | 223 |
|
121 | | - } |
122 | | - /** |
123 | | - * @param args |
124 | | - */ |
125 | | - public static void main(String[] args) { |
126 | | - new PubnubPushSampleCode().start(); |
127 | 224 |
|
128 | 225 | } |
129 | 226 |
|
|
0 commit comments