Advertisement
Guest User

Untitled

a guest
May 24th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. @Override
  2.     protected void onHandleIntent(Intent intent) {
  3.         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  4.         String resultString = null;
  5.         String regID = null;
  6.  
  7.         try {
  8.             InstanceID instanceID = InstanceID.getInstance(this);
  9.             String token = instanceID.getToken(NotificationSettings.SenderId,
  10.                     GoogleCloudMessaging.INSTANCE_ID_SCOPE);
  11.             Log.i(TAG, "Got GCM Registration Token: " + token);
  12.  
  13.             // Storing the registration id that indicates whether the generated token has been
  14.             // sent to your server. If it is not stored, send the token to your server,
  15.             // otherwise your server should have already received the token.
  16.  
  17.             /* Tu mi chyba hub = new....*/
  18.  
  19.             if ((regID=sharedPreferences.getString("registrationID", null)) == null) {
  20.                 NotificationHub hub = new NotificationHub(NotificationSettings.HubName,
  21.                         NotificationSettings.HubListenConnectionString, this);
  22.                 Log.i(TAG, "Attempting to register with NH using token : " + token);
  23.  
  24.                 regID = hub.register(token).getRegistrationId();
  25.  
  26.                 // If you want to use tags...
  27.                 // Refer to : https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-routing-tag-expressions/
  28.                 // regID = hub.register(token, "tag1,tag2").getRegistrationId();
  29.  
  30.                 resultString = "Registered Successfully - RegId : " + regID;
  31.                 Log.i(TAG, resultString);
  32.                 sharedPreferences.edit().putString("registrationID", regID ).apply();
  33.             } else {
  34.                
  35.                 /* tu mi chyba regID = hub.register()....*/
  36.  
  37.                 resultString = "Previously Registered Successfully - RegId : " + regID;
  38.             }
  39.         } catch (Exception e) {
  40.             Log.e(TAG, resultString="Failed to complete token refresh", e);
  41.             // If an exception happens while fetching the new token or updating our registration data
  42.             // on a third-party server, this ensures that we'll attempt the update at a later time.
  43.         }
  44.  
  45.         // Notify UI that registration has completed.
  46.         if (MainActivity.isVisible) {
  47.             MainActivity.mainActivity.ToastNotify(resultString);
  48.         }
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement