|
1 | 1 | package com.pubnub.api; |
2 | 2 |
|
| 3 | +import java.util.Hashtable; |
3 | 4 | import java.util.UUID; |
4 | 5 |
|
| 6 | +import android.content.Context; |
| 7 | + |
5 | 8 | /** |
6 | 9 | * Pubnub object facilitates querying channels for messages and listening on |
7 | 10 | * channels for presence/message events |
|
11 | 14 | */ |
12 | 15 |
|
13 | 16 | public class Pubnub extends PubnubCoreShared { |
| 17 | + |
| 18 | + String gcmRegistrationId = null; |
14 | 19 | /** |
15 | 20 | * Pubnub Constructor |
16 | 21 | * |
@@ -107,4 +112,111 @@ protected String getUserAgent() { |
107 | 112 | "; " + android.os.Build.MODEL + |
108 | 113 | " Build) PubNub-Java/Android/" + VERSION; |
109 | 114 | } |
| 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 | + } |
110 | 222 | } |
0 commit comments