2
0

RegisterIntentService.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Copyright 2015 Google Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.dev.deben.implementation;
  17. import android.app.IntentService;
  18. import android.content.Intent;
  19. import android.content.SharedPreferences;
  20. import android.preference.PreferenceManager;
  21. import android.support.v4.content.LocalBroadcastManager;
  22. import android.util.Log;
  23. import com.google.android.gms.gcm.GcmPubSub;
  24. import com.google.android.gms.gcm.GoogleCloudMessaging;
  25. import com.google.android.gms.iid.InstanceID;
  26. import org.json.JSONException;
  27. import java.io.IOException;
  28. import java.util.HashMap;
  29. public class RegisterIntentService extends IntentService {
  30. private static final String TAG = "RegIntentService";
  31. public RegisterIntentService() {
  32. super(TAG);
  33. }
  34. function fn = new function(this);
  35. @Override
  36. protected void onHandleIntent(Intent intent) {
  37. try {
  38. // [START register_for_gcm]
  39. // Initially this call goes out to the network to retrieve the token, subsequent calls
  40. // are local.
  41. // [START get_token]
  42. InstanceID instanceID = InstanceID.getInstance(this);
  43. // R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json.
  44. // See https://developers.google.com/cloud-messaging/android/start for details on this file.
  45. String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
  46. GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
  47. // [END get_token]
  48. // Log.i(TAG, "GCM Registration Token: " + token);
  49. System.out.println("Service creating token");
  50. System.out.println("Token: "+token);
  51. sendRegistrationToServer(token);
  52. // [END register_for_gcm]
  53. } catch (Exception e) {
  54. System.out.println("Failed to complete token refresh");
  55. }
  56. }
  57. /**
  58. * Persist registration to third-party servers.
  59. *
  60. * Modify this method to associate the user's GCM registration token with any server-side account
  61. * maintained by your application.
  62. *
  63. * @param token The new token.
  64. */
  65. private void sendRegistrationToServer(String token) throws IOException, JSONException {
  66. System.out.println("REGISTERING TOKEN");
  67. HashMap<String, String> set = new HashMap<>();
  68. set.put("notificationId", token);
  69. fn.writeSetting(set);
  70. }
  71. }