简体   繁体   中英

Android GCM - Cannot receive Messages

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);
    ...
}

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

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM