GCM为gettoken()发送java.io.IOException异常:超时 [英] GCM getToken() sends java.io.IOException: TIMEOUT

查看:2613
本文介绍了GCM为gettoken()发送java.io.IOException异常:超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现推送通知,但我收到TIMEOUT异常调用时为gettoken。

I am implementing push notifications, but I receive TIMEOUT exception when calling getToken.

我这里设置GCM 应用和SENDER_ID恰好提供的之一。此外,服务器API密钥保存在后端的部分。

I've set the app for GCM here and SENDER_ID is exactly the one provided. Also, Server API key was saved on the backend part.

有为gettoken请求有限数量?测试推送通知,当我在第一次尝试几次没有任何问题。

Is there a limited number of getToken requests? I had no problems at first several attempts when testing push notifications.

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清单:

Android Manifest:

<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> 
...

依赖添加到模块的的build.gradle:

Dependencies added to Module's build.gradle:


  • 应用插件:'com.google.gms.google服务

  • 编译com.google.android.gms:发挥服务-GCM:7.5 +。

依赖关系添加到项目的build.gradle:

Dependencies added to project's build.gradle:


  • 类路径'com.google.gms:谷歌服务:1.3.0-β1

推荐答案

我就遇到了这个问题,今天,这里是我做过什么来解决。

I ran into this problem today and here's what I did to troubleshoot.

首先,试图获得令牌的时候我还用我自己的发件人ID。所以不是,我尝试使用发件人ID从谷歌提供了的getString(R.id.gcm_defaultSenderId)

First, I also was using my own sender ID when attempting to get the token. So instead, I tried using the sender ID from the examples Google provides getString(R.id.gcm_defaultSenderId)

在我这样做,我开始得到一个不同的错误信息 SERVICE_NOT_AVAILABLE

Once I did this, I started to get a different error message SERVICE_NOT_AVAILABLE

有,解决这几款SO问题,但帮我出了一个是这个

There are several SO questions that address this, but the one that helped me out was this one.

我关掉WiFi和使用我的手机上4G的连接,并将其与两个 gcm_defaultSenderId 的工作和我自己的发件人ID。

I turned off WiFi and used a 4G connection on my phone and it worked with both the gcm_defaultSenderId and my own sender ID.

这篇关于GCM为gettoken()发送java.io.IOException异常:超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆