0
你能幫我一下嗎?我的代碼像3天前一樣工作。現在,當我運行時,我無法獲取爲我的設備生成的GCM令牌。這裏是我的代碼:GCM不給Token
private void registerGCM() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String token = null;
try {
InstanceID instanceID = InstanceID.getInstance(this);
token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.e(TAG, "GCM Registration Token: " + token);
// sending the registration id to our server
sendRegistrationToServer(token);
sharedPreferences.edit().putBoolean(Config.SENT_TOKEN_TO_SERVER, true).apply();
} catch (Exception e) {
Log.e(TAG, "Failed to complete token refresh", e);
sharedPreferences.edit().putBoolean(Config.SENT_TOKEN_TO_SERVER, false).apply();
}
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE);
registrationComplete.putExtra("token", token);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
檢查您的'gefaultSenderId',並確保您沒有禁用'GCM API.' –
檢查您的.json文件在應用程序文件夾中 – param
好吧,我檢查了我的API_KEY是一樣的Android API密鑰在控制檯 –