android – GCM getToken()发送java.io.IOException:TIMEOUT
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android – GCM getToken()发送java.io.IOException:TIMEOUT,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2827字,纯文字阅读大概需要5分钟。
内容图文
我正在实现推送通知,但是在调用getToken时我收到了TIMEOUT异常.
我为GCM here设置了应用程序,SENDER_ID正好是提供的应用程序.此外,Server API密钥保存在后端部分.
是否有有限数量的getToken请求?在测试推送通知时,我最初几次尝试都没有问题.
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
try {
InstanceID instance = InstanceID.getInstance(mContext);
String registrationId = instance.getToken(Constants.GCM_SENDER_ID,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
SharedPreferences sp = mContext.getSharedPreferences(Constants.TOKEN_DATA, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(Constants.REGISTRATION_ID, registrationId);
editor.commit();
NotificationsRegister.getInstance(mContext).register(registrationId);
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
}.execute();
Android清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myexample" >
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET"/>
<permission android:name="com.myexample.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.myexample.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.myexample" />
</intent-filter>
</receiver>
<service
android:name=".helper.TutoriaGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".helper.TutoriaInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
...
依赖关系添加到Module的build.gradle:
> apply plugin:’com.google.gms.google-services’
>编译’com.google.android.gms:play-services-gcm:7.5. “
依赖项添加到项目的build.gradle:
> classpath’com.google.gms:google-services:1.3.0-beta1′
解决方法:
我今天遇到了这个问题,这就是我要解决的问题.
首先,我在尝试获取令牌时也使用了自己的发件人ID.因此,我尝试使用Google提供的示例中的发件人ID getString(R.id.gcm_defaultSenderId)
一旦我这样做,我开始得到一个不同的错误消息SERVICE_NOT_AVAILABLE
有几个SO问题可以解决这个问题,但帮助我的问题是this one.
我关闭了WiFi并在手机上使用了4G连接,它同时使用了gcm_defaultSenderId和我自己的发件人ID.
内容总结
以上是互联网集市为您收集整理的android – GCM getToken()发送java.io.IOException:TIMEOUT全部内容,希望文章能够帮你解决android – GCM getToken()发送java.io.IOException:TIMEOUT所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。