Android 谷歌云消息传递 - GCM - SERVICE_NOT_AVAILABLE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11398470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Cloud Messaging - GCM - SERVICE_NOT_AVAILABLE
提问by Thomas Besnehard
I am trying, to implement the new GCM, I am following Google get stated: http://developer.android.com/guide/google/gcm/gs.html
我正在尝试实施新的 GCM,我正在关注谷歌得到声明:http: //developer.android.com/guide/google/gcm/gs.html
I getting stuck at getting my device registration ID !
我被困在获取我的设备注册 ID 上!
My app keep trying to connect with Google server, Here my error logs :
我的应用程序不断尝试与 Google 服务器连接,这是我的错误日志:
onReceive: com.google.android.gcm.intent.RETRY
GCM IntentService class: com.dombox.app.GCMIntentService
Acquiring wakelock
[GCMIntentService] start
Registering app com.dombox.app of senders _my_sender_id_
Releasing wakelock
onReceive: com.google.android.c2dm.intent.REGISTRATION
GCM IntentService class: com.dombox.app.GCMIntentService
Acquiring wakelock
[GCMIntentService] start
handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
Registration error: SERVICE_NOT_AVAILABLE
Scheduling registration retry, backoff = 912617 (768000)
Releasing wakelock
Here is my Activity Code, asking for an ID :
这是我的活动代码,要求提供 ID:
try{
Log.i(TAG, "[checkNotifRegistration] checkDevice");
GCMRegistrar.checkDevice(this);
Log.i(TAG, "[checkNotifRegistration] checkManifest");
GCMRegistrar.checkManifest(this);
if (GCMRegistrar.isRegistered(this)) {
Log.i(TAG, "[checkNotifRegistration] reg id : "+GCMRegistrar.getRegistrationId(this));
}
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
// SENDER_ID is my project id into google account url
GCMRegistrar.register(this, SENDER_ID);
Log.i(TAG, "[checkNotifRegistration] reg id : "+GCMRegistrar.getRegistrationId(this));
} else {
Log.i(TAG, "[checkNotifRegistration] already registered as : " + regId);
}
} catch(Exception e){
Log.e(TAG, "[checkNotifRegistration] Exception : "+e.getMessage());
e.printStackTrace();
}
Here my Service code
这是我的服务代码
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(SENDER_ID);
// SENDER_ID is my project id into google account url
// TODO Auto-generated constructor stub
Log.d(TAG, "[GCMIntentService] start");
}
public GCMIntentService(String senderId) {
super(senderId);
// TODO Auto-generated constructor stub
Log.d(TAG, "[GCMIntentService] start - sender Id : "+senderId);
}
}
And here is my Android Manifest :
这是我的 Android 清单:
<permission android:name="com.dombox.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.dombox.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<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.myapp.app" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" android:enabled="true"/>
<activity
android:name=".activity.Myapp"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I'm following Google's instruction, but I stuck with this SERVICE_NOT_AVAILABLE error,
我正在按照 Google 的说明进行操作,但我坚持使用此 SERVICE_NOT_AVAILABLE 错误,
What am I doing wrong ?
我究竟做错了什么 ?
回答by Thomas Besnehard
The problem was not code related, but link to the test phone. The test phone do not have a SIM, and the clock was not set. So google cannot resgistred it with a wrong time.
问题与代码无关,而是与测试手机的链接。 测试手机没有 SIM 卡,也没有设置时钟。所以谷歌不能在错误的时间重新注册它。
I manage to have a registration Id using a Android Virtual Device, with google api, and by setting the test phone clock.
我设法使用 Android 虚拟设备和 google api 并通过设置测试电话时钟来获得注册 ID 。
回答by Ryan Gray
android:enabled="true"
Remove this ^
删除这个^
If you don't already add this to your manifest
如果您尚未将其添加到清单中
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8"
You don't need the second constructor in GCMIntentService. only this one
您不需要 GCMIntentService 中的第二个构造函数。只有这个
public GCMIntentService() {
super(SENDER_ID); }
Ensure that your SENDER_ID is the number copied out of the URL from your browser while you are browsing the google code website AND browsing the GCM service.
确保您的 SENDER_ID 是在您浏览 google code 网站和浏览 GCM 服务时从浏览器的 URL 中复制出来的号码。
回答by Daniel
Remember that if you're behind a corporate firewall, some ports that GCM uses may not be allowed to communicate over the local wireless. I had this problem when trying to get my emulator to work initially. I had to get a special IP that allowed the ports listed below through.
请记住,如果您位于公司防火墙后面,则可能不允许 GCM 使用的某些端口通过本地无线通信。最初尝试让我的模拟器工作时遇到了这个问题。我必须获得一个允许下面列出的端口通过的特殊 IP。
Note: If your organization has a firewall that restricts the traffic to or from the Internet, you need to configure it to allow connectivity with GCM in order for your Android devices to receive messages. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM doesn't provide specific IPs, so you should allow your firewall to accept outgoing connections to all IP addresses contained in the IP blocks listed in Google's ASN of 15169.
注意:如果您的组织有防火墙来限制进出 Internet 的流量,您需要将其配置为允许与 GCM 连接,以便您的 Android 设备接收消息。要打开的端口是:5228、5229 和 5230。GCM 通常只使用 5228,但有时会使用 5229 和 5230。GCM 不提供特定的 IP,因此您应该允许防火墙接受到包含的所有 IP 地址的传出连接在 Google 的 ASN 15169 中列出的 IP 块中。
回答by Vinícius Barbosa
I've had this problem and it was caused by the WIFI connection. I turned WIFI off and made the test again (on 3g) and it Worked. Then I turned the WIFI back on and had the error again. Back to 3g, to make sure, and it worked again.
我遇到了这个问题,它是由 WIFI 连接引起的。我关闭了 WIFI 并再次进行了测试(在 3g 上)并且成功了。然后我重新打开WIFI并再次出现错误。回到 3g,以确保它再次起作用。
So, in conclusion, the issue was caused by the connection.
因此,总而言之,问题是由连接引起的。
回答by jpCoder411
I had this problem...my solution was to disable internet connection and use the phone service.
我遇到了这个问题...我的解决方案是禁用互联网连接并使用电话服务。