Sitelet https://github.com/mankan1/java/commit/9375a2f8eed1baf98e4a25ec314d741150c9e5c6
Skip to content

Commit 9375a2f

Browse files
author
Devendra
committed
adding comments, refactoring Gcm APIs
1 parent b8c8d07 commit 9375a2f

49 files changed

Lines changed: 1076 additions & 224 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎android/Pubnub-Android-3.6.1.jar‎

359 Bytes
Binary file not shown.
359 Bytes
Binary file not shown.

‎android/examples/PubnubExample/src/com/pubnub/examples/pubnubExample10/MainActivity.java‎

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.pubnub.api.Pubnub;
3737
import com.pubnub.api.PubnubError;
3838
import com.pubnub.api.PubnubException;
39-
import com.pubnub.api.PubnubGcmRegistrationIdMissingException;
4039
import com.pubnub.examples.pubnubExample10.R;
4140

4241
import com.google.android.gms.gcm.GoogleCloudMessaging;
@@ -236,7 +235,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
236235
return true;
237236
case R.id.option27:
238237
gcmRegister();
239-
pubnub.setGcmRegistrationId(REG_ID);
240238
return true;
241239
case R.id.option28:
242240
gcmUnregister();
@@ -438,7 +436,6 @@ private void init() {
438436
CIPHER_KEY,
439437
SSL
440438
);
441-
pubnub.setGcmRegistrationId(REG_ID);
442439
pubnub.setCacheBusting(false);
443440
pubnub.setOrigin(ORIGIN);
444441
pubnub.setAuthKey(AUTH_KEY);
@@ -474,7 +471,13 @@ private Map<String,String> getCredentials() {
474471
}
475472

476473
private void gcmRemoveAllChannels() {
477-
pubnub.unregisterAllChannelsForDevice(new Callback() {
474+
if (TextUtils.isEmpty(REG_ID)) {
475+
Toast.makeText(getApplicationContext(),
476+
"GCM Registration id not set. Register to GCM and try again.",
477+
Toast.LENGTH_LONG).show();
478+
return ;
479+
}
480+
pubnub.removeAllPushNotificationsForDeviceRegistrationId(REG_ID, new Callback() {
478481
@Override
479482
public void successCallback(String channel,
480483
Object message) {
@@ -501,26 +504,25 @@ private void gcmRemoveChannel() {
501504

502505
@Override
503506
public void onClick(DialogInterface dialog, int which) {
507+
if (TextUtils.isEmpty(REG_ID)) {
508+
Toast.makeText(getApplicationContext(),
509+
"GCM Registration id not set. Register to GCM and try again.",
510+
Toast.LENGTH_LONG).show();
511+
return ;
512+
}
504513
String channel = edChannelName.getText().toString();
505-
try {
506-
pubnub.unregisterDeviceFromChannel(channel, new Callback() {
507-
@Override
508-
public void successCallback(String channel,
509-
Object message) {
510-
notifyUser("GCM REMOVE : " + message);
511-
}
512-
@Override
513-
public void errorCallback(String channel,
514-
PubnubError error) {
515-
notifyUser("GCM REMOVE : " + error);
516-
}
517-
});
518-
} catch (PubnubGcmRegistrationIdMissingException e) {
519-
gcmRegister();
520-
Toast.makeText(getApplicationContext(),
521-
"GCM registration ID was not set. Auto registering. Try removing channel again.",
522-
Toast.LENGTH_LONG).show();
523-
}
514+
pubnub.disablePushNotificationsOnChannel(channel, REG_ID, new Callback() {
515+
@Override
516+
public void successCallback(String channel,
517+
Object message) {
518+
notifyUser("GCM REMOVE : " + message);
519+
}
520+
@Override
521+
public void errorCallback(String channel,
522+
PubnubError error) {
523+
notifyUser("GCM REMOVE : " + error);
524+
}
525+
});
524526
}
525527
});
526528
AlertDialog alert = builder.create();
@@ -540,26 +542,26 @@ private void gcmAddChannel() {
540542

541543
@Override
542544
public void onClick(DialogInterface dialog, int which) {
545+
546+
if (TextUtils.isEmpty(REG_ID)) {
547+
Toast.makeText(getApplicationContext(),
548+
"GCM Registration id not set. Register to GCM and try again.",
549+
Toast.LENGTH_LONG).show();
550+
return ;
551+
}
543552
String channel = edChannelName.getText().toString();
544-
try {
545-
pubnub.registerDeviceOnChannel(channel, new Callback() {
546-
@Override
547-
public void successCallback(String channel,
548-
Object message) {
549-
notifyUser("GCM ADD : " + message);
550-
}
551-
@Override
552-
public void errorCallback(String channel,
553-
PubnubError error) {
554-
notifyUser("GCM ADD : " + error);
555-
}
556-
});
557-
} catch (PubnubGcmRegistrationIdMissingException e) {
558-
gcmRegister();
559-
Toast.makeText(getApplicationContext(),
560-
"GCM registration ID was not set. Auto registering. Try adding channel again.",
561-
Toast.LENGTH_LONG).show();
562-
}
553+
pubnub.disablePushNotificationsOnChannel(channel, REG_ID, new Callback() {
554+
@Override
555+
public void successCallback(String channel,
556+
Object message) {
557+
notifyUser("GCM ADD : " + message);
558+
}
559+
@Override
560+
public void errorCallback(String channel,
561+
PubnubError error) {
562+
notifyUser("GCM ADD : " + error);
563+
}
564+
});
563565
}
564566
});
565567
AlertDialog alert = builder.create();
@@ -568,7 +570,7 @@ public void errorCallback(String channel,
568570
}
569571

570572
private void gcmUnregister() {
571-
pubnub.setGcmRegistrationId("");
573+
//TODO add unregister code
572574
}
573575

574576

@@ -587,7 +589,7 @@ private String gcmRegister() {
587589
}
588590

589591
String REG_ID = getRegistrationId(context);
590-
pubnub.setGcmRegistrationId(REG_ID);
592+
591593
if (TextUtils.isEmpty(REG_ID)) {
592594

593595
registerInBackground();
359 Bytes
Binary file not shown.

‎android/src/com/pubnub/api/Pubnub.java‎

Lines changed: 104 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
public class Pubnub extends PubnubCoreShared {
2020

21-
String gcmRegistrationId = null;
2221
/**
2322
* Pubnub Constructor
2423
*
@@ -126,25 +125,53 @@ protected String getUserAgent() {
126125
" Build) PubNub-Java/Android/" + VERSION;
127126
}
128127

129-
public void setGcmRegistrationId(String registrationId) {
130-
this.gcmRegistrationId = registrationId;
128+
129+
/**
130+
* Enable Push Notifications (Google Cloud Messaging)
131+
* @param channel
132+
* @param gcmRegistrationId
133+
*/
134+
public void enablePushNotificationsOnChannel(String channel, String gcmRegistrationId) {
135+
enablePushNotificationsOnChannels(new String[]{channel}, gcmRegistrationId, null);
136+
}
137+
138+
/**
139+
* Enable Push Notifications (Google Cloud Messaging)
140+
* @param channels
141+
* @param gcmRegistrationId
142+
*/
143+
public void enablePushNotificationsOnChannels(String[] channels, String gcmRegistrationId) {
144+
enablePushNotificationsOnChannels(channels, gcmRegistrationId, null);
145+
}
146+
147+
/**
148+
* Enable Push Notifications (Google Cloud Messaging)
149+
* @param channel
150+
* @param gcmRegistrationId
151+
* @param callback
152+
*/
153+
public void enablePushNotificationsOnChannel(String channel, String gcmRegistrationId, Callback callback) {
154+
enablePushNotificationsOnChannels(new String[]{channel}, gcmRegistrationId, callback);
131155
}
132156

133-
public void registerDeviceOnChannel(final String channel, final Callback callback) throws PubnubGcmRegistrationIdMissingException {
134-
final Callback cb = getWrappedCallback(callback);
135157

136-
if (this.gcmRegistrationId == null || this.gcmRegistrationId.length() <= 0)
137-
throw new PubnubGcmRegistrationIdMissingException("Registration Id Missing");
158+
/**
159+
* Enable Push Notifications (Google Cloud Messaging)
160+
* @param channels
161+
* @param gcmRegistrationId
162+
* @param callback
163+
*/
164+
public void enablePushNotificationsOnChannels(final String[] channels, String gcmRegistrationId, final Callback callback) {
165+
final Callback cb = getWrappedCallback(callback);
138166

139167
Hashtable parameters = PubnubUtil.hashtableClone(params);
140168
String[] urlargs = null;
141169
urlargs = new String[]{ getPubnubUrl(), "v1", "push", "sub-key",
142-
this.SUBSCRIBE_KEY, "devices", this.gcmRegistrationId
170+
this.SUBSCRIBE_KEY, "devices", gcmRegistrationId
143171
};
144172

145-
146173
parameters.put("type", "gcm");
147-
parameters.put("add", channel);
174+
parameters.put("add", PubnubUtil.joinString(channels, ","));
148175

149176
HttpRequest hreq = new HttpRequest(urlargs, parameters,
150177
new ResponseHandler() {
@@ -168,21 +195,58 @@ public void handleError(HttpRequest hreq, PubnubError error) {
168195

169196
_request(hreq, nonSubscribeManager);
170197
}
171-
public void unregisterDeviceFromChannel(final String channel, final Callback callback) throws PubnubGcmRegistrationIdMissingException {
198+
199+
/**
200+
* Disable Push Notifications (Google Cloud Messaging)
201+
* @param channel
202+
* @param gcmRegistrationId
203+
*/
204+
public void disablePushNotificationsOnChannel(String channel, String gcmRegistrationId) {
205+
disablePushNotificationsOnChannels(new String[]{channel}, gcmRegistrationId, null);
206+
}
207+
208+
/**
209+
* Disable Push Notifications (Google Cloud Messaging)
210+
* @param channels
211+
* @param gcmRegistrationId
212+
*/
213+
public void disablePushNotificationsOnChannels(String[] channels, String gcmRegistrationId) {
214+
disablePushNotificationsOnChannels(channels, gcmRegistrationId, null);
215+
}
216+
217+
/**
218+
* Disable Push Notifications (Google Cloud Messaging)
219+
* @param channel
220+
* @param gcmRegistrationId
221+
* @param callback
222+
*/
223+
public void disablePushNotificationsOnChannel(String channel, String gcmRegistrationId, Callback callback) {
224+
disablePushNotificationsOnChannels(new String[]{channel}, gcmRegistrationId, callback);
225+
}
226+
227+
/**
228+
* Disable Push Notifications (Google Cloud Messaging)
229+
* @param channel
230+
* @param callback
231+
*/
232+
/**
233+
* @param channels
234+
* @param gcmRegistrationId
235+
* @param callback
236+
*/
237+
public void disablePushNotificationsOnChannels(final String[] channels, String gcmRegistrationId, final Callback callback) {
172238
final Callback cb = getWrappedCallback(callback);
173239

174-
if (this.gcmRegistrationId == null || this.gcmRegistrationId.length() <= 0)
175-
throw new PubnubGcmRegistrationIdMissingException("Registration Id Missing");
176240

177241
Hashtable parameters = PubnubUtil.hashtableClone(params);
178242
String[] urlargs = null;
179243
urlargs = new String[]{ getPubnubUrl(), "v1", "push", "sub-key",
180-
this.SUBSCRIBE_KEY, "devices", this.gcmRegistrationId
244+
this.SUBSCRIBE_KEY, "devices", gcmRegistrationId
181245
};
182246

183247

184248
parameters.put("type", "gcm");
185-
parameters.put("remove", channel);
249+
parameters.put("remove", PubnubUtil.joinString(channels, ","));
186250

187251
HttpRequest hreq = new HttpRequest(urlargs, parameters,
188252
new ResponseHandler() {
@@ -206,12 +270,19 @@ public void handleError(HttpRequest hreq, PubnubError error) {
206270

207271
_request(hreq, nonSubscribeManager);
208272
}
209-
public void getChannelListforDevice(final Callback callback) {
273+
274+
275+
/**
276+
* Get channels for which push notification is enabled (Google Cloud Messaging)
277+
* @param gcmRegistrationId
278+
* @param callback
279+
*/
280+
public void requestPushNotificationEnabledChannelsForDeviceRegistrationId(String gcmRegistrationId, final Callback callback) {
210281
final Callback cb = getWrappedCallback(callback);
211282
Hashtable parameters = PubnubUtil.hashtableClone(params);
212283
String[] urlargs = null;
213284
urlargs = new String[]{ getPubnubUrl(), "v1", "push", "sub-key",
214-
this.SUBSCRIBE_KEY, "devices", this.gcmRegistrationId
285+
this.SUBSCRIBE_KEY, "devices", gcmRegistrationId
215286
};
216287

217288

@@ -237,12 +308,26 @@ public void handleError(HttpRequest hreq, PubnubError error) {
237308
}
238309
});
239310
}
240-
public void unregisterAllChannelsForDevice(final Callback callback) {
311+
312+
/**
313+
* Disable push notifications for all channels (Google Cloud Messaging)
314+
* @param gcmRegistrationId
315+
*/
316+
public void removeAllPushNotificationsForDeviceRegistrationId(String gcmRegistrationId) {
317+
removeAllPushNotificationsForDeviceRegistrationId(gcmRegistrationId, null);
318+
}
319+
320+
/**
321+
* Disable push notifications for all channels (Google Cloud Messaging)
322+
* @param gcmRegistrationId
323+
* @param callback
324+
*/
325+
public void removeAllPushNotificationsForDeviceRegistrationId(String gcmRegistrationId, final Callback callback) {
241326
final Callback cb = getWrappedCallback(callback);
242327
Hashtable parameters = PubnubUtil.hashtableClone(params);
243328
String[] urlargs = null;
244329
urlargs = new String[]{ getPubnubUrl(), "v1", "push", "sub-key",
245-
this.SUBSCRIBE_KEY, "devices", this.gcmRegistrationId, "remove"
330+
this.SUBSCRIBE_KEY, "devices", gcmRegistrationId, "remove"
246331
};
247332

248333

662 Bytes
Binary file not shown.
987 Bytes
Binary file not shown.

‎j2me/Pubnub-MicroEdition-3.6.1.jar‎

625 Bytes
Binary file not shown.
1.14 KB
Binary file not shown.

‎java/doc/allclasses-frame.html‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<!--NewPage-->
33
<HTML>
44
<HEAD>
5-
<!-- Generated by javadoc (build 1.6.0_37) on Fri May 30 12:12:32 IST 2014 -->
5+
<!-- Generated by javadoc (build 1.6.0_37) on Sat May 31 11:33:11 IST 2014 -->
66
<TITLE>
77
All Classes
88
</TITLE>
99

10-
<META NAME="date" CONTENT="2014-05-30">
10+
<META NAME="date" CONTENT="2014-05-31">
1111

1212
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
1313

@@ -41,6 +41,8 @@
4141
<BR>
4242
<A HREF="com/pubnub/api/PubnubSender.html" title="class in com.pubnub.api" target="classFrame">PubnubSender</A>
4343
<BR>
44+
<A HREF="com/pubnub/api/PubnubSenderMissingException.html" title="class in com.pubnub.api" target="classFrame">PubnubSenderMissingException</A>
45+
<BR>
4446
<A HREF="com/pubnub/api/PubnubUtil.html" title="class in com.pubnub.api" target="classFrame">PubnubUtil</A>
4547
<BR>
4648
<A HREF="com/pubnub/api/Sender.html" title="interface in com.pubnub.api" target="classFrame"><I>Sender</I></A>

0 commit comments

Comments
 (0)