11package com .pubnub .examples .pubnubExample ;
22
3- import java .util . Hashtable ;
3+ import java .io . IOException ;
44
5- import android .view .View ;
6- import android .widget .LinearLayout ;
75import org .json .JSONArray ;
86import org .json .JSONObject ;
97
1412import android .content .DialogInterface ;
1513import android .content .Intent ;
1614import android .content .IntentFilter ;
15+ import android .content .SharedPreferences ;
16+ import android .content .pm .PackageInfo ;
17+ import android .content .pm .PackageManager .NameNotFoundException ;
1718import android .net .ConnectivityManager ;
19+ import android .os .AsyncTask ;
1820import android .os .Bundle ;
1921import android .text .InputType ;
22+ import android .text .TextUtils ;
23+ import android .util .Config ;
2024import android .util .Log ;
2125import android .view .Menu ;
2226import android .view .MenuItem ;
27+ import android .view .View ;
2328import android .widget .EditText ;
29+ import android .widget .LinearLayout ;
30+ import android .widget .TextView ;
2431import android .widget .Toast ;
25- import android .view .View .OnClickListener ;
2632
2733import com .pubnub .api .Callback ;
2834import com .pubnub .api .Pubnub ;
2935import com .pubnub .api .PubnubError ;
36+ import com .pubnub .api .PubnubException ;
37+ import com .pubnub .api .PubnubGcmRegistrationIdMissingException ;
38+
39+ import com .google .android .gms .gcm .GoogleCloudMessaging ;
3040
3141public class MainActivity extends Activity {
3242 /*
@@ -37,7 +47,15 @@ public class MainActivity extends Activity {
3747 */
3848
3949 Pubnub pubnub = new Pubnub ("demo" , "demo" , "" , false );
40-
50+ GoogleCloudMessaging gcm ;
51+ Context context ;
52+ String regId ;
53+ public static final String SENDER_ID = "750222044802" ;
54+ public static final String REG_ID = "regId" ;
55+ private static final String APP_VERSION = "appVersion" ;
56+
57+ static final String TAG = "Register Activity" ;
58+
4159 private void notifyUser (Object message ) {
4260 try {
4361 if (message instanceof JSONObject ) {
@@ -198,12 +216,216 @@ public boolean onOptionsItemSelected(MenuItem item) {
198216 case R .id .option23 :
199217 setHeartbeatInterval ();
200218 return true ;
219+ case R .id .option24 :
220+ gcmRegister ();
221+ return true ;
222+ case R .id .option25 :
223+ gcmUnregister ();
224+ return true ;
225+ case R .id .option26 :
226+ gcmAddChannel ();
227+ return true ;
228+ case R .id .option27 :
229+ gcmRemoveChannel ();
230+ return true ;
231+ case R .id .option28 :
232+ gcmRemoveAllChannels ();
233+ return true ;
201234 default :
202235 return super .onOptionsItemSelected (item );
203236 }
204237 }
205238
206- private void setHeartbeatInterval () {
239+ private void gcmRemoveAllChannels () {
240+ pubnub .unregisterAllChannelsForDevice (new Callback () {
241+ @ Override
242+ public void successCallback (String channel ,
243+ Object message ) {
244+ notifyUser ("GCM REMOVE ALL : " + message );
245+ }
246+ @ Override
247+ public void errorCallback (String channel ,
248+ PubnubError error ) {
249+ notifyUser ("GCM REMOVE ALL : " + error );
250+ }
251+ });
252+
253+ }
254+
255+ private void gcmRemoveChannel () {
256+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
257+ builder .setTitle ("Remove Channel from GCM" );
258+ builder .setMessage ("Enter Channel Name" );
259+ final EditText edChannelName = new EditText (this );
260+ edChannelName .setInputType (InputType .TYPE_CLASS_TEXT );
261+ builder .setView (edChannelName );
262+ builder .setPositiveButton ("Remove" ,
263+ new DialogInterface .OnClickListener () {
264+
265+ @ Override
266+ public void onClick (DialogInterface dialog , int which ) {
267+ String channel = edChannelName .getText ().toString ();
268+ try {
269+ pubnub .unregisterDeviceFromChannel (channel , new Callback () {
270+ @ Override
271+ public void successCallback (String channel ,
272+ Object message ) {
273+ notifyUser ("GCM REMOVE : " + message );
274+ }
275+ @ Override
276+ public void errorCallback (String channel ,
277+ PubnubError error ) {
278+ notifyUser ("GCM REMOVE : " + error );
279+ }
280+ });
281+ } catch (PubnubGcmRegistrationIdMissingException e ) {
282+ gcmRegister ();
283+ }
284+ }
285+ });
286+ AlertDialog alert = builder .create ();
287+ alert .show ();
288+
289+ }
290+
291+ private void gcmAddChannel () {
292+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
293+ builder .setTitle ("Add Channel to GCM" );
294+ builder .setMessage ("Enter Channel Name" );
295+ final EditText edChannelName = new EditText (this );
296+ edChannelName .setInputType (InputType .TYPE_CLASS_TEXT );
297+ builder .setView (edChannelName );
298+ builder .setPositiveButton ("Add" ,
299+ new DialogInterface .OnClickListener () {
300+
301+ @ Override
302+ public void onClick (DialogInterface dialog , int which ) {
303+ String channel = edChannelName .getText ().toString ();
304+ try {
305+ pubnub .registerDeviceOnChannel (channel , new Callback () {
306+ @ Override
307+ public void successCallback (String channel ,
308+ Object message ) {
309+ notifyUser ("GCM ADD : " + message );
310+ }
311+ @ Override
312+ public void errorCallback (String channel ,
313+ PubnubError error ) {
314+ notifyUser ("GCM ADD : " + error );
315+ }
316+ });
317+ } catch (PubnubGcmRegistrationIdMissingException e ) {
318+ gcmRegister ();
319+ }
320+ }
321+ });
322+ AlertDialog alert = builder .create ();
323+ alert .show ();
324+
325+ }
326+
327+ private void gcmUnregister () {
328+ pubnub .setGcmRegistrationId ("" );
329+ }
330+
331+
332+
333+
334+ private String gcmRegister () {
335+ pubnub .setCacheBusting (false );
336+ pubnub .setOrigin ("dara11.devbuild" );
337+ context = getApplicationContext ();
338+ gcm = GoogleCloudMessaging .getInstance (this );
339+ String regId = getRegistrationId (context );
340+
341+ if (TextUtils .isEmpty (regId )) {
342+
343+ registerInBackground ();
344+
345+ Log .d ("RegisterActivity" ,
346+ "registerGCM - successfully registered with GCM server - regId: "
347+ + regId );
348+ } else {
349+ Toast .makeText (getApplicationContext (),
350+ "RegId already available. RegId: " + regId ,
351+ Toast .LENGTH_LONG ).show ();
352+ }
353+ return regId ;
354+ }
355+
356+ private String getRegistrationId (Context context ) {
357+ final SharedPreferences prefs = getSharedPreferences (
358+ MainActivity .class .getSimpleName (), Context .MODE_PRIVATE );
359+ String registrationId = prefs .getString (REG_ID , "" );
360+ if (registrationId .length () <= 0 ) {
361+ Log .i (TAG , "Registration not found." );
362+ return "" ;
363+ }
364+ int registeredVersion = prefs .getInt (APP_VERSION , Integer .MIN_VALUE );
365+ int currentVersion = getAppVersion (context );
366+ if (registeredVersion != currentVersion ) {
367+ Log .i (TAG , "App version changed." );
368+ return "" ;
369+ }
370+ return registrationId ;
371+ }
372+
373+ private static int getAppVersion (Context context ) {
374+ try {
375+ PackageInfo packageInfo = context .getPackageManager ()
376+ .getPackageInfo (context .getPackageName (), 0 );
377+ return packageInfo .versionCode ;
378+ } catch (NameNotFoundException e ) {
379+ Log .d ("RegisterActivity" ,
380+ "I never expected this! Going down, going down!" + e );
381+ throw new RuntimeException (e );
382+ }
383+ }
384+
385+ private void registerInBackground () {
386+ new AsyncTask <Void , Void , String >() {
387+ @ Override
388+ protected String doInBackground (Void ... params ) {
389+ String msg = "" ;
390+ try {
391+ if (gcm == null ) {
392+ gcm = GoogleCloudMessaging .getInstance (context );
393+ }
394+ regId = gcm .register (SENDER_ID );
395+ Log .d ("RegisterActivity" , "registerInBackground - regId: "
396+ + regId );
397+ msg = "Device registered, registration ID=" + regId ;
398+
399+ storeRegistrationId (context , regId );
400+ } catch (IOException ex ) {
401+ msg = "Error :" + ex .getMessage ();
402+ Log .d ("RegisterActivity" , "Error: " + msg );
403+ }
404+ Log .d ("RegisterActivity" , "AsyncTask completed: " + msg );
405+ return msg ;
406+ }
407+
408+ @ Override
409+ protected void onPostExecute (String msg ) {
410+ Toast .makeText (getApplicationContext (),
411+ "Registered with GCM Server." + msg , Toast .LENGTH_LONG )
412+ .show ();
413+ }
414+ }.execute (null , null , null );
415+ }
416+
417+ private void storeRegistrationId (Context context , String regId ) {
418+ final SharedPreferences prefs = getSharedPreferences (
419+ MainActivity .class .getSimpleName (), Context .MODE_PRIVATE );
420+ int appVersion = getAppVersion (context );
421+ Log .i (TAG , "Saving regId on app version " + appVersion );
422+ SharedPreferences .Editor editor = prefs .edit ();
423+ editor .putString (REG_ID , regId );
424+ editor .putInt (APP_VERSION , appVersion );
425+ editor .commit ();
426+ }
427+
428+ private void setHeartbeatInterval () {
207429 AlertDialog .Builder builder = new AlertDialog .Builder (this );
208430 builder .setTitle ("Set Presence Heartbeat Interval" );
209431 builder .setMessage ("Enter heartbeat value in seconds" );
0 commit comments