【问题标题】:Android GCM InstanceId.getToken() -> java.io.IOException: TIMEOUTAndroid GCM InstanceId.getToken() -> java.io.IOException: TIMEOUT
【发布时间】:2015-10-28 08:54:08
【问题描述】:

我正在尝试在我的 android 应用中实现推送通知,但我目前卡在接收令牌的时候。

我正在使用“InstanceID”的最新方法并遵循了一些示例。 我已将权限和服务添加到我的清单中(并为它们添加了代码),但无论我尝试什么,我总是得到“java.io.IOException:TIMEOUT”错误。我尝试了不同的手机、wifi、Lte 和 3G,但似乎没有任何改变。

我从来没有遇到过“SERVICE_NOT_AVAILABLE”的错误。只停留在这个。

清单

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver
  android:name="com.google.android.gms.gcm.GcmReceiver"
  android:exported="true"
  android:permission="com.google.android.c2dm.permission.SEND" >
  <intent-filter>
      <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="de.company.appname" />
  </intent-filter>
</receiver>
<service
  android:name="de.company.gcm.ModuleGCMListenerService"
  android:exported="false" >
  <intent-filter>
     <action android:name="com.google.android.c2dm.intent.RECEIVE" />
  </intent-filter>
</service>
<service
  android:name="de.company.gcm.ModuleInstanceIDListenerService"
  android:exported="false">
  <intent-filter>
    <action android:name="com.google.android.gms.iid.InstanceID"/>
  </intent-filter>
</service>

InstanceID 调用:

try 
{
  token = instanceID.getToken(
            senderId,
            GoogleCloudMessaging.INSTANCE_ID_SCOPE, 
            null);
} 
catch (IOException e) 
{
  e.printStackTrace();
}

instanceID.getId() 正在工作。

W/InstanceID/Rpc: No response android.os.ConditionVariable@129d5713
W/System.err: java.io.IOException: TIMEOUT
W/System.err:   at com.google.android.gms.iid.zzc.zzb(Unknown Source)
W/System.err:   at com.google.android.gms.iid.zzc.zza(Unknown Source)
W/System.err:   at com.google.android.gms.iid.InstanceID.zzc(Unknown Source)
W/System.err:   at com.google.android.gms.iid.InstanceID.getToken(Unknown Source)

【问题讨论】:

  • 看看这个repo here,与InstanceID.getToken()相关的来源是here
  • 谢谢,虽然我已经浏览了所有的谷歌示例和其他几个页面。他们都没有给我任何线索我可能做错了什么。
  • 您是否只是克隆了存储库并在没有代码的情况下在您的 IDE 上尝试了自己?你可以试试看
  • 我目前也遇到了同样的问题......之前它工作得很好
  • 如果您使用的是 WIFI,请尝试将其关闭并使用您的移动网络,它对我有用。

标签: java android google-cloud-messaging


【解决方案1】:

在代码中的相同位置搜索类似错误时,我终于弄清楚了我的问题。

New GCM API Register Unknown Source Error

在这种情况下,错误“MAIN_THREAD”确实更明显一些。

这是因为 InstanceId.getToken() 不能在主线程中调用。

虽然它不适用于 Google Github 示例中建议的 new AsyncTask&lt;Void, Void, Void&gt;() 方法。

我不得不使用

new Thread(new Runnable() {
    public void run() {
        //code
    }
}).start();

【讨论】:

  • 没关系 操作需要在主线程之外,可以在异步或新线程中
猜你喜欢
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 2019-09-20
  • 2019-06-09
  • 1970-01-01
相关资源
最近更新 更多