Shrikant Ballal
Shrikant Ballal

Reputation: 7087

GCM: onMessage() from GCMIntentService is called very few times

I am implementing GCM in my application. I have followed all the steps given in GCM tutorial from developer.android.com

My application's build target is pointing to Goolge API 8 (Android 2.2 version).

I am able to get the register ID from GCM successfully, and I am passing this ID to my application server. So the registration step is performed successfully.

Now when my application server sends a PUSH message to my device, the server gets the message as SUCCESS=1 FAILURE=0, etc., i.e. Server is sending message successfully, but my device never receives the message.

After searching alot about this, I came to know that GCM pushes messages on port number 5228, 5229 or 5230.

Initially, my device and laptop was restricted for some websites, but then I was granted all the permissions to access all websites, so I guess these port numbers are open for my device.

So my question is: Very few times, I am receiving PUSH message from GCM. Why Google is so uncertain in delivering messages? What could be the reason?

Please see my following code and guide me accordingly:

I have declared following in my manifest:

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

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

    <!-- App receives GCM messages. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="package.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            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="packageName" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".ReceiveBroadcast"
            android:exported="false" >
            <intent-filter>
                <action android:name="GCM_RECEIVED_ACTION" />
            </intent-filter>
        </receiver>

        <service
            android:name=".GCMIntentService"
            />


/**
 * @author Shrikant.
 * 
 */
public class GCMIntentService extends GCMBaseIntentService {

    /**
     * The Sender ID used for GCM.
     */
    public static final String SENDER_ID = "myProjectID";

    /**
     * This field is used to call Web-Service for GCM.
     */
    SendUserCredentialsGCM sendUserCredentialsGCM = null;

    public GCMIntentService() {
        super(SENDER_ID);
        sendUserCredentialsGCM = new SendUserCredentialsGCM();
    }

    @Override
    protected void onRegistered(Context arg0, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        sendUserCredentialsGCM.sendRegistrationID(registrationId);
    }

    @Override
    protected void onUnregistered(Context context, String arg1) {
        Log.i(TAG, "unregistered = " + arg1);
        sendUserCredentialsGCM
                .unregisterFromGCM(LoginActivity.API_OR_BROWSER_KEY);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.e("GCM MESSAGE", "Message Recieved!!!");
        String message = intent.getStringExtra("message");
        if (message == null) {
            Log.e("NULL MESSAGE", "Message Not Recieved!!!");
        } else {
            Log.i(TAG, "new message= " + message);
            sendGCMIntent(context, message);
        }
    }

    private void sendGCMIntent(Context context, String message) {
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("GCM_RECEIVED_ACTION");
        broadcastIntent.putExtra("gcm", message);
        context.sendBroadcast(broadcastIntent);
    }

    @Override
    protected void onError(Context context, String errorId) {
        Log.e(TAG, "Received error: " + errorId);
        Toast.makeText(context, "PUSH Notification failed.", Toast.LENGTH_LONG)
                .show();
    }

    @Override
    protected boolean onRecoverableError(Context context, String errorId) {
        return super.onRecoverableError(context, errorId);
    }
}

Thanks.

Upvotes: 1

Views: 1860

Answers (3)

user1527969
user1527969

Reputation: 76

Check whether the debug certificate is expired or not, If it is then create new certificate and generate new key.

Upvotes: 2

DeepakAndroid
DeepakAndroid

Reputation: 137

Make sure your app name and the name you registered on the google console for getting api key are same name.Same name in the sense that exactly the same

Upvotes: 0

Mustafa Gen&#231;
Mustafa Gen&#231;

Reputation: 2579

Try to change your class names as full names with package names. Like:

<service android:name="com.xx.xx.xx" />

And dont forget to put your package name here:

<category android:name="packageName" />

Upvotes: 0

Related Questions