2015-09-04 60 views
4

雖沒在谷歌開發者頁面GCM所有指令(清單設置,配置文件,註冊在控制檯..)GCM集成爲Android

public class MyInstanceIDListenerService extends IntentService { 
private static final String TAG = "RegIntentService"; 
private static final String[] TOPICS = {"global"}; 

public MyInstanceIDListenerService() { 
    super(TAG); 
} 

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

    try { 
     // [START register_for_gcm] 
     // Initially this call goes out to the network to retrieve the token, subsequent calls 
     // are local. 
     // [START get_token] 
     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] 

}

的gcm_defaultSenderId和QuickStartPreferences不能解決。爲什麼?我如何克服它?

回答

1

gcm_defaultSenderId是您必須創建的字符串。這是您的應用發件人ID。

QuickStartPreferences作爲示例。如果您想記住該令牌已發送至您的服務器,那麼您可以創建一個類來保存優選鍵(請參閱該行上方的註釋)。

+0

ok QuickStart首選項我只是想確定,但配置文件的目標之一是創建gcm_defaultSenderId,對不對? – Bad0

+0

啊,也許,我不使用它,我使用Java常量來存儲我的發件人ID。你在你的gradle文件中啓用了插件嗎? – clemp6r

+0

是的,我啓用它,我在幾個項目中做了幾次所有的配置。我知道我必須錯過一些東西,但我找不到它。 – Bad0