Android does not get push notifications

3

When trying to receive notifications in Android the following error is generated:

  

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

I have checked the shipment on the web link and it is done correctly but I do not know get on the device.

google-services.json I placed it in /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);
 }
}
    
asked by Miguel Angel Martinez Sanchez 23.06.2016 в 19:46
source

1 answer

1

Reviewing the code and Manifest.xml I found that you lack the permission:

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

add it below:

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

Review the example of the documentation , "example of a manifest that GCM supports":

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

I can tell you that when I implemented an example for the first time I deleted that permission since I considered it to be repeated = P, this is a cause for not receiving notifications.

    
answered by 23.06.2016 в 20:50