GCM 不给令牌
GCM not giving Token
你能帮帮我吗?我的代码像 3 天前一样工作。现在,当我 运行 时,我无法为我的设备生成 GCM 令牌。这是我的代码:
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);
}
要获得注册令牌,Google 提供 Instance ID API
来处理注册令牌的创建和更新。
您必须在清单文件中包含 InstanceIDListenerService
才能使用它。
<service android:name="[.MyInstanceIDService]" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
然后要获取令牌,请调用 instanceID.getToken
,提供应用服务器的发件人 ID 并将范围设置为 GoogleCloudMessaging.INSTANCE_ID_SCOPE
。
NOTE: Do not call this method in the main thread; instead, use a
service that extends
IntentService
这是示例代码。
Public class RegistrationIntentService extends IntentService {
// ...
@Override
public void onHandleIntent(Intent intent) {
// ...
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// ...
}
// ...
}
有关更多信息,请查看此 documentation and this complete sample code。
你能帮帮我吗?我的代码像 3 天前一样工作。现在,当我 运行 时,我无法为我的设备生成 GCM 令牌。这是我的代码:
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);
}
要获得注册令牌,Google 提供 Instance ID API
来处理注册令牌的创建和更新。
您必须在清单文件中包含 InstanceIDListenerService
才能使用它。
<service android:name="[.MyInstanceIDService]" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
然后要获取令牌,请调用 instanceID.getToken
,提供应用服务器的发件人 ID 并将范围设置为 GoogleCloudMessaging.INSTANCE_ID_SCOPE
。
NOTE: Do not call this method in the main thread; instead, use a service that extends IntentService
这是示例代码。
Public class RegistrationIntentService extends IntentService {
// ...
@Override
public void onHandleIntent(Intent intent) {
// ...
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
// ...
}
// ...
}
有关更多信息,请查看此 documentation and this complete sample code。