2015-06-19 7 views
8

ich RegistrationIntentService nennen:Android - nicht GCM bekommen Token

if (checkPlayServices()) { 
     Log.i("udazzT", "check servicies"); 
     // Start IntentService to register this application with GCM. 
     Intent intent2 = new Intent(this, RegistrationIntentService.class); 
     startService(intent2); 
    } 

Aber ich kann nicht der Protokolle in RegistrationIntentService sehen:

public class RegistrationIntentService extends IntentService { 

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

    public RegistrationIntentService() { 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.i("udazzT", "setting token"); 
     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] 
       // Initially this call goes out to the network to retrieve the token, subsequent calls 
       // are local. 
       // [START get_token] 
       Log.i("udazzT", "setting token 2"); 
       InstanceID instanceID = InstanceID.getInstance(this); 
       String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), 
         GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 
       // [END get_token] 
       Log.i(TAG, "GCM Registration 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(QuickstartPreferences.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(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply(); 
     } 
     // Notify UI that registration has completed, so the progress indicator can be hidden. 
     Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); 
    } 

    /** 
    * Persist registration to third-party servers. 
    * 
    * Modify this method to associate the user's GCM registration token with any server-side account 
    * maintained by your application. 
    * 
    * @param token The new token. 
    */ 
    private void sendRegistrationToServer(String token) { 
     // Add custom implementation, as needed. 
    } 

    /** 
    * Subscribe to any GCM topics of interest, as defined by the TOPICS constant. 
    * 
    * @param token GCM token 
    * @throws IOException if unable to reach the GCM PubSub service 
    */ 
    // [START subscribe_topics] 
    private void subscribeTopics(String token) throws IOException { 
     for (String topic : TOPICS) { 
      GcmPubSub pubSub = GcmPubSub.getInstance(this); 
      pubSub.subscribe(token, "/topics/" + topic, null); 
     } 
    } 
    // [END subscribe_topics] 

} 
+0

Haben Sie durch diese Frage zu lesen: http://stackoverflow.com/questions/11713363/i-can-not-get-registration-id-from- android-g cm? –

+0

Die Registermethode in diesem Beispiel ist veraltet –

Antwort

17

Vergewissern Sie sich, dass es in Ihrem Manifest registriert ist.

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

Ich habe nicht funktioniert. Muss ich einen Absichtsfilter setzen? –

+0

@MarianoPelizzari sehen Sie die Protokollausgabe vor der Absicht? 'Log.i (" udazzT "," check services ");' – mattfred

+0

Ja, ich bin das gesehen –

2

Hast du gedacht, hinzufügen alle hier benötigt wird, wie googlesamples

+0

Ich habe, und ich habe mehrmals überprüft –

+0

ok, weil ich einige Methoden vergessen habe, aber jetzt funktioniert es für mich mit googlesamples. –

0

Gerade dieses Add

<intent-filter> 
<action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
<category android:name="<PACKAGE-NAME>" /> 
</intent-filter> 

in manifest.xml von "GcmReceiver" & "MyGcmListenerService" in meinem Code ist es aussieht Wie unten.

In meinem Fall funktioniert es ... Vielleicht ist es hilfreich für Sie. Best Of Luck