我正在实现推送通知,但在调用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:
<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中的依赖项:
添加到项目的build.gradle中的依赖项:
答案 0 :(得分:3)
我今天遇到了这个问题,这就是我要解决的问题。
首先,我在尝试获取令牌时也使用了自己的发件人ID。因此,我尝试使用Google提供的示例getString(R.id.gcm_defaultSenderId)
完成此操作后,我开始收到不同的错误消息SERVICE_NOT_AVAILABLE
有几个问题可以解决这个问题,但帮助我的问题是this one。
我关闭了WiFi并在手机上使用了4G连接,它同时使用gcm_defaultSenderId
和我自己的发件人ID。
答案 1 :(得分:2)
在注册之前,您必须检查google api是否可以使用此代码片段或类似代码:
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(a);
if (resultCode == ConnectionResult.SUCCESS) { //Do the getTokencall } else { //check the code }
可能您没有收到SUCCESS resultCode。例如,如果有谷歌api的待定更新。
答案 2 :(得分:0)
我遇到了这个问题并在再次尝试后得到修复。
答案 3 :(得分:0)
在注册之前播放服务可能无法使用,此课程可能对您有所帮助:
public class PlayServicesValidator {
private static final String TAG = PlayServicesValidator.class.getSimpleName();
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
/**
* Check the device to make sure it has the Google Play Services APK. If
* it doesn't, display a dialog that allows users to download the APK from
* the Google Play Store or enable it in the device's system settings.
*/
public static boolean areAvailable(Context context) {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(context);
if (resultCode != ConnectionResult.SUCCESS) {
return false;
}
return true;
}
/**
* Only call if areAvailable was previously called and returned false
* @param activity
* @return
*/
public static boolean areRecoverable(Activity activity) {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
return true;
} else {
return false;
}
}
return true;
}
/**
* Shows to the user a dialog to update play services
* @param activity
*/
public static void recover(Activity activity) {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
}
}
}
}