I use push notifications, but when I getToken, I get a TIMEOUT exception.
I installed the application for GCM here , and SENDER_ID is exactly the one that was provided. In addition, the API server key has been stored internally.
Is there a limited number of getToken requests? At first, I had no problems with 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 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> ...
Dependencies added to the build.gradle module:
- apply plugin: 'com.google.gms.google-services'
- compile 'com.google.android.gms: play-services-gcm: 7.5. + '
Dependencies added to the build.gradle project:
- classpath 'com.google.gms: google-services: 1.3.0-beta1'
blavi source share