Skip to content

Commit d6050dd

Browse files
author
Devendra
committed
changes for gcm
1 parent 189face commit d6050dd

6 files changed

Lines changed: 158 additions & 2 deletions

File tree

‎android/examples/PubnubExample/AndroidManifest.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
android:versionCode="1"
44
android:versionName="1.0">
55
<uses-sdk
6-
android:minSdkVersion="8"
6+
android:minSdkVersion="10"
77
android:targetSdkVersion="17"/>
88

99
<application android:allowBackup="true"
@@ -24,5 +24,6 @@
2424
<uses-permission android:name="android.permission.INTERNET"/>
2525
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
2626
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
27+
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
2728

2829
</manifest>

‎android/examples/PubnubExample/res/layout/usage.xml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,31 @@
162162
android:layout_width="wrap_content"
163163
android:layout_height="wrap_content"
164164
android:text="@string/option23" />
165+
<TextView
166+
android:id="@+id/option24"
167+
android:layout_width="wrap_content"
168+
android:layout_height="wrap_content"
169+
android:text="@string/option24" />
170+
<TextView
171+
android:id="@+id/option25"
172+
android:layout_width="wrap_content"
173+
android:layout_height="wrap_content"
174+
android:text="@string/option25" />
175+
<TextView
176+
android:id="@+id/option26"
177+
android:layout_width="wrap_content"
178+
android:layout_height="wrap_content"
179+
android:text="@string/option26" />
180+
<TextView
181+
android:id="@+id/option27"
182+
android:layout_width="wrap_content"
183+
android:layout_height="wrap_content"
184+
android:text="@string/option27" />
185+
<TextView
186+
android:id="@+id/option28"
187+
android:layout_width="wrap_content"
188+
android:layout_height="wrap_content"
189+
android:text="@string/option28" />
165190
</LinearLayout>
166191

167192
</LinearLayout>

‎android/examples/PubnubExample/res/menu/activity_main.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@
2121
<item android:id="@+id/option20" android:title="@string/option20" android:titleCondensed="@string/option20"></item>
2222
<item android:id="@+id/option21" android:title="@string/option21" android:titleCondensed="@string/option21"></item>
2323
<item android:id="@+id/option22" android:title="@string/option22" android:titleCondensed="@string/option22"></item>
24-
<item android:id="@+id/option23" android:title="@string/option23" android:titleCondensed="@string/option23"></item>
24+
<item android:id="@+id/option23" android:title="@string/option23" android:titleCondensed="@string/option23"></item>
25+
<item android:id="@+id/option24" android:title="@string/option24" android:titleCondensed="@string/option24"></item>
26+
<item android:id="@+id/option25" android:title="@string/option25" android:titleCondensed="@string/option25"></item>
27+
<item android:id="@+id/option26" android:title="@string/option26" android:titleCondensed="@string/option26"></item>
28+
<item android:id="@+id/option27" android:title="@string/option27" android:titleCondensed="@string/option27"></item>
29+
<item android:id="@+id/option28" android:title="@string/option28" android:titleCondensed="@string/option28"></item>
2530
</menu>

‎android/examples/PubnubExample/res/values/strings.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@
4040
<string name="option21">Set UUID</string>
4141
<string name="option22">Set State</string>
4242
<string name="option23">Set Presence Heartbeat Interval ( in seconds )</string>
43+
<string name="option24">Register on GCM</string>
44+
<string name="option25">Unregister on GCM</string>
45+
<string name="option26">Add channel to GCM</string>
46+
<string name="option27">Remove channel from GCM</string>
47+
<string name="option28">Remove all channels from GCM</string>
4348
</resources>

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.pubnub.api;
22

3+
import java.util.Hashtable;
34
import java.util.UUID;
45

6+
import android.content.Context;
7+
58
/**
69
* Pubnub object facilitates querying channels for messages and listening on
710
* channels for presence/message events
@@ -11,6 +14,8 @@
1114
*/
1215

1316
public class Pubnub extends PubnubCoreShared {
17+
18+
String gcmRegistrationId = null;
1419
/**
1520
* Pubnub Constructor
1621
*
@@ -107,4 +112,111 @@ protected String getUserAgent() {
107112
"; " + android.os.Build.MODEL +
108113
" Build) PubNub-Java/Android/" + VERSION;
109114
}
115+
116+
public void setGcmRegistrationId(String registrationId) {
117+
this.gcmRegistrationId = registrationId;
118+
}
119+
120+
public void registerDeviceOnChannel(final String channel, Callback callback) throws PubnubGcmRegistrationIdMissingException {
121+
final Callback cb = getWrappedCallback(callback);
122+
123+
if (this.gcmRegistrationId == null || this.gcmRegistrationId.length() <= 0)
124+
throw new PubnubGcmRegistrationIdMissingException("Registration Id Missing");
125+
126+
Hashtable parameters = PubnubUtil.hashtableClone(params);
127+
String[] urlargs = null;
128+
urlargs = new String[]{ getPubnubUrl(), "v1", "push", "sub-key",
129+
this.SUBSCRIBE_KEY, "devices", this.gcmRegistrationId
130+
};
131+
132+
133+
parameters.put("type", "gcm");
134+
parameters.put("add", channel);
135+
136+
HttpRequest hreq = new HttpRequest(urlargs, parameters,
137+
new ResponseHandler() {
138+
public void handleResponse(HttpRequest hreq, String response) {
139+
invokeCallback(channel, response, "payload",cb, 1);
140+
}
141+
142+
public void handleError(HttpRequest hreq, PubnubError error) {
143+
cb.errorCallback(channel, error);
144+
}
145+
});
146+
147+
_request(hreq, nonSubscribeManager);
148+
}
149+
public void unregisterDeviceFromChannel(final String channel, Callback callback) throws PubnubGcmRegistrationIdMissingException {
150+
final Callback cb = getWrappedCallback(callback);
151+
152+
if (this.gcmRegistrationId == null || this.gcmRegistrationId.length() <= 0)
153+
throw new PubnubGcmRegistrationIdMissingException("Registration Id Missing");
154+
155+
Hashtable parameters = PubnubUtil.hashtableClone(params);
156+
String[] urlargs = null;
157+
urlargs = new String[]{ getPubnubUrl(), "v1", "push", "sub-key",
158+
this.SUBSCRIBE_KEY, "devices", this.gcmRegistrationId
159+
};
160+
161+
162+
parameters.put("type", "gcm");
163+
parameters.put("remove", channel);
164+
165+
HttpRequest hreq = new HttpRequest(urlargs, parameters,
166+
new ResponseHandler() {
167+
public void handleResponse(HttpRequest hreq, String response) {
168+
invokeCallback(channel, response, "payload",cb, 1);
169+
}
170+
171+
public void handleError(HttpRequest hreq, PubnubError error) {
172+
cb.errorCallback(channel, error);
173+
}
174+
});
175+
176+
_request(hreq, nonSubscribeManager);
177+
}
178+
public void getChannelListforDevice(Callback callback) {
179+
final Callback cb = getWrappedCallback(callback);
180+
Hashtable parameters = PubnubUtil.hashtableClone(params);
181+
String[] urlargs = null;
182+
urlargs = new String[]{ getPubnubUrl(), "v1", "push", "sub-key",
183+
this.SUBSCRIBE_KEY, "devices", this.gcmRegistrationId
184+
};
185+
186+
187+
parameters.put("type", "gcm");
188+
189+
HttpRequest hreq = new HttpRequest(urlargs, parameters,
190+
new ResponseHandler() {
191+
public void handleResponse(HttpRequest hreq, String response) {
192+
invokeCallback("", response, "payload",cb, 1);
193+
}
194+
195+
public void handleError(HttpRequest hreq, PubnubError error) {
196+
cb.errorCallback("", error);
197+
}
198+
});
199+
}
200+
public void unregisterAllChannelsForDevice(Callback callback) {
201+
final Callback cb = getWrappedCallback(callback);
202+
Hashtable parameters = PubnubUtil.hashtableClone(params);
203+
String[] urlargs = null;
204+
urlargs = new String[]{ getPubnubUrl(), "v1", "push", "sub-key",
205+
this.SUBSCRIBE_KEY, "devices", this.gcmRegistrationId, "remove"
206+
};
207+
208+
209+
parameters.put("type", "gcm");
210+
211+
HttpRequest hreq = new HttpRequest(urlargs, parameters,
212+
new ResponseHandler() {
213+
public void handleResponse(HttpRequest hreq, String response) {
214+
invokeCallback("", response, "payload",cb, 1);
215+
}
216+
217+
public void handleError(HttpRequest hreq, PubnubError error) {
218+
cb.errorCallback("", error);
219+
}
220+
});
221+
}
110222
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.pubnub.api;
2+
3+
public class PubnubGcmRegistrationIdMissingException extends PubnubException {
4+
5+
public PubnubGcmRegistrationIdMissingException(String s) {
6+
super(s);
7+
}
8+
}

0 commit comments

Comments
 (0)