0

The AndroidManifest.xml:

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!-- Receiver for GCM Messages-->
    <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.REGISTRATION" />
                <action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION"/>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="de.comp.module.client" />
            </intent-filter>
    </receiver>

    <service
        android:name="de.comp.module.client.gcm.GCMBroadcastIntentService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

The GCMBroadcastIntentService class:

public class GCMBroadcastIntentService extends GcmListenerService {

    @Override
    public void onMessageReceived(String from, Bundle extras) {
         if (extras.containsKey("message")) {}
    }
}

The onMessageReceived() method is never called. The server works fine, the messages are send to Google GCM without any errors, but they never arrive at the device. I also use google-services.json which is stored in the folder /app. In my app the user can loggin after a new installation. In this case a new GCM token is requested and send to the server. So it should be correct and up to date.

What is missing? Thanks for your help.

EDIT:

public class GCMRegistrationIntentService extends IntentService {
    @Override
         protected void onHandleIntent(Intent intent) {

        try {

           // register server key to GCM
           GoogleCloudMessaging gcm =   GoogleCloudMessaging.getInstance(this);
           String regid = gcm.register(getString(R.string.gcm_defaultSenderId));

        // Request new token for this server key
        InstanceID instanceID = InstanceID.getInstance(this);
        String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

        // Subscribe to topic channels
        subscribeTopics(token);
    ...
}
Otis Ottington
  • 425
  • 5
  • 17

4 Answers4

0

you must register the sender id in the Gcm:

 GoogleCloudMessaging gcm=null;

 try {

     if (gcm == null) {

         gcm = GoogleCloudMessaging.getInstance(getContext());

     }

     String regid = gcm.register(SENDER_ID);


 } catch (IOException ex) {

   msg = "Error :" + ex.getMessage();

 }

note that SENDER_ID is the id of the server that sends the notification

Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
  • Thanks for your answer. I will check. Do I have to register it once or every time the app is started? – Otis Ottington Aug 09 '16 at 12:07
  • you have to register it one, but if you register it every time you start the app, it will also work well. – Khalid Taha Aug 09 '16 at 12:09
  • It does not work. Is it a problem that the registation is made inside a IntentService? see my updated question above. – Otis Ottington Aug 09 '16 at 12:28
  • yes, make the registration in the first activity that runs after the application runs – Khalid Taha Aug 09 '16 at 12:33
  • Thank you. I will check this evening. One question. To receive a message at all, I have to send the regId from gcm.register(SENDER_ID) to the server, right? To get messages at all I do not need the token/InstanceId from InstanceID.getInstance(this) anyway, right? – Otis Ottington Aug 09 '16 at 13:23
  • `gcm.register(SENDER_ID)` will register the server that will send the notification for the mobile client. it will return the regId that will be used by the server to send. – Khalid Taha Aug 09 '16 at 14:17
0

Use FCM instead of GCM.Check this official links. FCM is a new Version of GCM

Sagar Gangawane
  • 1,985
  • 1
  • 12
  • 22
0

Try to start from the beginning.

  1. Make sure SENDER_ID matches the project number in console.
  2. Make sure you implement registration IntentService, device registration is successful and you receive push token (GCM registration id).
  3. Make sure you pass it to server.
  4. Not forget to implement InstanceIDListenerService.
  5. Add missing services to manifest.
  6. Start everything from the beginning and pay attention at each step
    https://developers.google.com/cloud-messaging/android/client
Leo DroidCoder
  • 14,527
  • 4
  • 62
  • 54
0

Set up the GCM client according to https://developers.google.com/cloud-messaging/android/client

If you still not get a call to onMessageReceived(), you can check whether the problem is somwhat similar to GcmListenerService.onMessageReceived() not called

Also GCM requires a Google account for Pre-4.0.4 devices if you are using the deprecated library (GCMRegistrar.register), . If yes you need to make an entry in AndroidManifest as

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

Refer GET_ACCOUNTS permission while using GCM - Why is this needed?

Community
  • 1
  • 1
Neh
  • 442
  • 4
  • 7