3

Al intentar recibir las notificaciones en Android se genera el siguiente error:

Failed to resolve target intent service, skipping classname enforcement
Error while delivering the message: ServiceIntent not found.

El envío lo he comprobado en la web http://techzog.com/development/gcm-notification-test-tool-android/ y se realiza correctamente pero no se recibe en el dispositivo.

google-services.json lo he situado en /app

Android Manifest:

<manifest package="com.packageName"
      xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />  <!-- support previous 4.4 KitKat devices-->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.packageName.permission.C2D_MESSAGE" />
<application
    android:name="AppName"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.Black.NoTitleBar">
    <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="com.packageName" />
        </intent-filter>
        <intent-filter> <!-- support previous 4.4 KitKat devices-->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.packageName" />
        </intent-filter>
    </receiver>
    <service
        android:name="com.packageName.activities.RegistrationIntentService"
        android:exported="false" >
    </service>
    <service
        android:name="com.packageName.activities.GcmIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID" />
        </intent-filter>
    </service>
    <service
        android:name="com.packageName.activities.MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
</application>

MyGcmListenerService:

public class MyGcmListenerService extends GcmListenerService {

  @Override
  public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.d("From", "From: " + from);
    Log.d("Msg", "Message: " + message);

  }

}

RegistrationIntentService:

 public class RegistrationIntentService extends IntentService {
   private static String SENDER_ID = "MySenderID";

   public RegistrationIntentService(){
       super(SENDER_ID);
   }

   @Override
   public void onHandleIntent(Intent intent){

      SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

      try {
          InstanceID instanceID = InstanceID.getInstance(this);
          String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
              GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
          Log.i("TOKEN", "GCM Registration Token: " + token);
      } catch (Exception e) {
          Log.d("Fail token", "Failed to complete token refresh", e);
      }

  }


}

GcmIDListenerService:

public class GcmIDListenerService extends InstanceIDListenerService {
 @Override
 public void onTokenRefresh() {
    Intent intent = new Intent(this, RegistrationIntentService.class);
    startService(intent);
 }
}
0

1 respuesta 1

1

Revisando el código y el Manifest.xml encontré que te falta el permiso:

<permission
    android:name="com.packageName.permission.C2D_MESSAGE"
    android:protectionLevel="signature" /> 

agregalo abajo de:

<uses-permission android:name="com.packageName.permission.C2D_MESSAGE" />

Revisa el ejemplo de la documentación, "ejemplo de manifest que soporta GCM":

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

<permission android:name="<your-package-name>.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="<your-package-name>.permission.C2D_MESSAGE" />

<application ...>
    <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="com.example.gcm" />
        </intent-filter>
    </receiver>
    <service
        android:name="com.example.MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <service
        android:name="com.example.MyInstanceIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID" />
        </intent-filter>
    </service>
</application>

Te puedo comentar que cuando implemente un ejemplo por primera ocasión eliminé ese permiso ya que consideré que estaba repetido =P, esto es una causa para que no recibas notificaciones.

1

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.