Mai*_*r00 25 java android push-notification google-cloud-messaging
我遇到了一个奇怪的问题 - 我在我的应用程序中使用GCM已经有很长一段时间了,一切都运行得很好.然而,发布谷歌Play前,我从改变了我的应用程序包的名字com.android.testapp来com.android.recognition并在此之后GCM停止工作.起初我得到错误GCM sender id not set on constructor并通过覆盖修复它getSenderIds(Context context),但现在我无法获得注册ID.以下是来自logcat的消息:

我怎样才能解决这个问题?当我切换到新包时,我将清单文件中的所有内容更改为新包:
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.android.recognition" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
那背后的问题是什么?可以重命名应用程序包导致这个还是有另一个原因?
Ama*_*tam 42
这个问题得到了回答,在我看来,这个问题要复杂得多.
错误的时钟给我带来了麻烦.:)
Pan*_*mar 32
此SERVICE_NOT_AVAILABLE错误表示GCM Service当前不可用.等一段时间后再试.
这种情况发生很多次(根据我的经验),所以不要担心.
请参阅GCMConstantsGCM Lib类.
/**
* The device can't read the response, or there was a 500/503 from the
* server that can be retried later. The application should use exponential
* back off and retry.
*/
public static final String ERROR_SERVICE_NOT_AVAILABLE =
"SERVICE_NOT_AVAILABLE";
Run Code Online (Sandbox Code Playgroud)
对于更多的调查看handleRegistration()的GCMBaseIntentService
private void handleRegistration(final Context context, Intent intent) {
String registrationId = intent.getStringExtra(EXTRA_REGISTRATION_ID);
String error = intent.getStringExtra(EXTRA_ERROR);
String unregistered = intent.getStringExtra(EXTRA_UNREGISTERED);
Log.d(TAG, "handleRegistration: registrationId = " + registrationId +
", error = " + error + ", unregistered = " + unregistered);
// registration succeeded
if (registrationId != null) {
GCMRegistrar.resetBackoff(context);
GCMRegistrar.setRegistrationId(context, registrationId);
onRegistered(context, registrationId);
return;
}
// unregistration succeeded
if (unregistered != null) {
// Remember we are unregistered
GCMRegistrar.resetBackoff(context);
String oldRegistrationId =
GCMRegistrar.clearRegistrationId(context);
onUnregistered(context, oldRegistrationId);
return;
}
// last operation (registration or unregistration) returned an error;
Log.d(TAG, "Registration error: " + error);
// Registration failed
if (ERROR_SERVICE_NOT_AVAILABLE.equals(error)) {
boolean retry = onRecoverableError(context, error);
if (retry) {
int backoffTimeMs = GCMRegistrar.getBackoff(context);
int nextAttempt = backoffTimeMs / 2 +
sRandom.nextInt(backoffTimeMs);
Log.d(TAG, "Scheduling registration retry, backoff = " +
nextAttempt + " (" + backoffTimeMs + ")");
Intent retryIntent =
new Intent(INTENT_FROM_GCM_LIBRARY_RETRY);
retryIntent.putExtra(EXTRA_TOKEN, TOKEN);
PendingIntent retryPendingIntent = PendingIntent
.getBroadcast(context, 0, retryIntent, 0);
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + nextAttempt,
retryPendingIntent);
// Next retry should wait longer.
if (backoffTimeMs < MAX_BACKOFF_MS) {
GCMRegistrar.setBackoff(context, backoffTimeMs * 2);
}
} else {
Log.d(TAG, "Not retrying failed operation");
}
} else {
// Unrecoverable error, notify app
onError(context, error);
}
}
Run Code Online (Sandbox Code Playgroud)
Era*_*ran 18
确保您更改了清单的权限部分中的包名称:
<permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="YOUR_PACKAGE_NAME.permission.C2D_MESSAGE" />
Run Code Online (Sandbox Code Playgroud)
由于该部分中的包名称不正确,我遇到了类似的错误.
Ela*_*ava 18
SERVICE_NOT_AVAILABLE是Google Cloud Messaging最令人沮丧的问题之一.这是一个例外GoogleCloudMessaging.register(SENDER_ID),函数调用注册设备以进行推送通知并返回注册ID.
阅读更多:http: //eladnava.com/google-cloud-messaging-extremely-unreliable/
仅这些问题就足以让我开始寻找GCM替代品.我每天或每两天都会对我的应用程序进行1星评价,并在评论中包含抛出SERVICE_NOT_AVAILABLE时显示的错误消息.我无能为力帮助这些用户,因为他们中的大多数人都是出于他们无法控制的原因而接收这些用户.
Google Cloud Messaging的替代方案
Pushy(https://pushy.me/)是一个独立的推送通知网关,完全独立于GCM.它保持自己的后台套接字连接,就像GCM一样,用于接收推送通知.底层协议是MQTT,一种极轻量级的pub/sub协议,利用非常少的网络带宽和电池.
Pushy的一个巨大优势是,用于发送推送通知(来自服务器)以及注册设备以进行推送通知的代码实际上可以在GCM和Pushy之间互换.这使得在实施GCM之后切换到Pushy非常容易,并且由于其不稳定而不得不放弃它.
(完全披露:我为自己的项目创立了Pushy,并意识到许多应用程序将从这样的服务中受益)
| 归档时间: |
|
| 查看次数: |
68588 次 |
| 最近记录: |