Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  2. <uses-permission android:name="android.permission.WAKE_LOCK" />
  3. <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  4. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  5. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  6.  
  7. package com.example.elarabygroup;
  8.  
  9. import com.google.android.gcm.GCMBaseIntentService;
  10. import com.google.android.gcm.GCMRegistrar;
  11.  
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.os.PowerManager;
  15. import android.util.Log;
  16.  
  17. public class GCMIntenetService extends GCMBaseIntentService {
  18. private static final String GCM_SENDER_ID = "1111111111";
  19.  
  20. public GCMIntenetService() {
  21. super();
  22. }
  23.  
  24. @Override
  25. protected void onRegistered(Context context, String registrationId) {
  26. Log.i(TAG, "Device registered: regId = " + registrationId);
  27. GCMRegistrar.setRegisteredOnServer(context, true);
  28. }
  29.  
  30. @Override
  31. protected void onUnregistered(Context context, String registrationId) {
  32. Log.i(TAG, "Device unregistered");
  33. if (GCMRegistrar.isRegisteredOnServer(context)) {
  34. String regId = "";
  35. Log.i(TAG, "unregistering device (regId = " + regId + ")");
  36. GCMRegistrar.setRegisteredOnServer(context, false);
  37. } else {
  38. // This callback results from the call to unregister made on
  39. // ServerUtilities when the registration to the server failed.
  40. Log.i(TAG, "Ignoring unregister callback");
  41. }
  42. }
  43.  
  44. @Override
  45. protected void onError(Context context, String errorId) {
  46. // push error processing
  47. }
  48.  
  49. @Override
  50. protected void onMessage(Context arg0, Intent arg1) {
  51. Log.i(TAG, "Received message");
  52. Log.i(TAG, "EXTRAS" + arg1.getExtras());
  53. //String message = getString(R.string.gcm_message);
  54. generateNotification(arg0, arg1.getStringExtra("Please download our new updates"));
  55. // notifies user about message
  56.  
  57. }
  58.  
  59. private void generateNotification(Context arg0, String stringExtra) {
  60. // TODO Auto-generated method stub
  61.  
  62. }
  63.  
  64. public static void registerInGCMService(Context context) {
  65. if (!checkIsGCMServiceAvailable(context)) {
  66. return;
  67. }
  68. final String regId = GCMRegistrar.getRegistrationId(context);
  69. if (regId.equals("")) {
  70. try {
  71. GCMRegistrar.register(context, GCM_SENDER_ID);
  72. } catch (Exception ex) {
  73. }
  74. } else {
  75. // Already registered
  76. }
  77. }
  78.  
  79. public static boolean checkIsGCMServiceAvailable(Context context) {
  80. try {
  81. GCMRegistrar.checkDevice(context);
  82. GCMRegistrar.checkManifest(context);
  83. return true;
  84. } catch (Throwable th) {
  85. return false;
  86. }
  87. }
  88.  
  89. }
  90.  
  91. import android.provider.Settings.Secure;
  92.  
  93. private String android_id = Secure.getString(getContext().getContentResolver(),
  94. Secure.ANDROID_ID);
  95.  
  96. public class GCMIntenetService extends GCMBaseIntentService {
  97. private static String GCM_SENDER_ID ;
  98.  
  99. public GCMIntenetService() {
  100. super();
  101. }
  102.  
  103.  
  104.  
  105. public static void registerInGCMService(Context context) {
  106.  
  107.  
  108. GCM_SENDER_ID = Secure.getString(getContext().getContentResolver(),
  109. Secure.ANDROID_ID);
  110.  
  111. if (!checkIsGCMServiceAvailable(context)) {
  112. return;
  113. }
  114. final String regId = GCMRegistrar.getRegistrationId(context);
  115. if (regId.equals("")) {
  116. try {
  117. GCMRegistrar.register(context, GCM_SENDER_ID);
  118. } catch (Exception ex) {
  119. }
  120. } else {
  121. // Already registered
  122. }
  123. }
  124.  
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement