2015-07-10 4 views
4

Я пытаюсь реализовать GCM для андроида с использованием, https://developers.google.com/cloud-messaging/android/client ..не onHandleIntent вызывался

Created configuration file and placed under project/src..Added all the permissions in manifest file.. 
still onHandleIntent() method is not getting called, and token is not generated.. 

My intentservice file: 

public class RegistrationIntentService extends IntentService { 

    private static final String TAG = "RegIntentService"; 
    private static final String[] TOPICS = {"global"}; 

    public RegistrationIntentService() { 
     super(TAG); 
    } 

    @Override 
    public void onDestroy() { 
     Log.d(TAG, "onDestroy()"); 
     super.onDestroy(); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 

     try { 
      // In the (unlikely) event that multiple refresh operations occur simultaneously, 
      // ensure that they are processed sequentially. 
      synchronized (TAG) { 
       // [START register_for_gcm] 
       InstanceID instance = InstanceID.getInstance(this); 
       String token = instance.getToken(getString(R.string.gcm_defaultSenderId), 
         GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 
       System.out.println("GCM Token is :" + token); 
       // [END get_token] 
       //Log.i(TAG, "GCM Regist Token: " + token); 

       // TODO: Implement this method to send any registration to your app's servers. 
       sendRegistrationToServer(token); 

       // Subscribe to topic channels 
       subscribeTopics(token); 

       // You should store a boolean that indicates whether the generated token has been 
       // sent to your server. If the boolean is false, send the token to your server, 
       // otherwise your server should have already received the token. 
       sharedPreferences.edit().putBoolean(AppConstants.SENT_TOKEN_TO_SERVER, true).apply(); 
       // [END register_for_gcm] 
      } 
     } catch (Exception e) { 
      // Log.d(TAG, "Failed to complete token refresh", e); 
      // If an exception happens while fetching the new token or updating our registration data 
      // on a third-party server, this ensures that we'll attempt the update at a later time. 
      sharedPreferences.edit().putBoolean(AppConstants.SENT_TOKEN_TO_SERVER, false).apply(); 
     } 
     // Notify UI that registration has completed, so the progress indicator can be hidden. 
     Intent registrationComplete = new Intent(AppConstants.REGISTRATION_COMPLETE); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); 
    } 

And my manifest file: 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.xyz" 
    android:versionCode="1" 
    android:versionName="1.0" > 


    <!-- Permissions needed for read contacts --> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <!-- Permissions needed for read calendars --> 
    <uses-permission android:name="android.permission.READ_CALENDAR" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.READ_LOGS" /> 
    <uses-permission android:name="android.permission.GET_TASKS"/> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 


    <application 
     android:allowBackup="true" 
     android:name=".application.Application" 
     android:icon="@drawable/ic_launcher" 
     android:theme="@style/Theme" 
     android:label="lez"> 


     </receiver> 
     <receiver 
      android:name="com.google.android.gms.gcm.GcmReceiver" 
      android:exported="true" 
      android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 

      </intent-filter> 
     </receiver> 
     <service 
      android:name=".gcm.MyGcmListenerService" 
      android:exported="false" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      </intent-filter> 
     </service> 
     <service 
      android:name=".gcm.MyInstanceIDListenerService" 
      android:exported="false"> 
      <intent-filter> 
       <action android:name="com.google.android.gms.iid.InstanceID"/> 
      </intent-filter> 
     </service> 
     <service 
      android:name=".gcm.RegistrationIntentService" 
      android:exported="false"> 
     </service> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <uses-library android:name="android.test.runner" /> 
    </application>`enter code here` 

</manifest> 

Пожалуйста, кто-нибудь может мне помочь? Я могу запустить образец, указанный в ссылке выше, единственная разница между образцом и моим кодом: у меня нет градиента уровня приложения ... Имею только один файл градиента. В градиенте я только что добавил google-app-gcm dependy ... Проект, импортированный из затмения в андроид-студию

ответ

9

Похоже, это довольно старый, но я решил, что добавлю ответ, так как я просто столкнулся с этой проблемой. Вам просто нужно добавить объявление службы в файл AndroidManifest.xml. Добавьте следующий узел в узел «приложение».

<service 
     android:name=".RegistrationIntentService" 
     android:exported="false"/> 

Это аналогично ответил и обсуждены here.

Смежные вопросы