1
votes

I have created a GCM Intent service and the device is getting registered successfully and server successfully send the message. but device don't get any notifications.

Below the log i see,

10-29 18:26:09.599: V/GCMBroadcastReceiver(30775): onReceive: com.google.android.c2dm.intent.RECEIVE
10-29 18:26:09.599: V/GCMBroadcastReceiver(30775): GCM IntentService class: com.eye.hor.GCMIntentService
10-29 18:26:09.599: V/GCMBaseIntentService(30775): Acquiring wakelock

Below is the code for GCMBroadcastReceiver,

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Log.i(context.getPackageName(), intent.getExtras().toString());
    Log.d("brdR", "askd");
    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

}

Below is the code for GCMIntentService,

public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

public GcmIntentService() {
    super("GcmIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) { // has effect of unparcelling Bundle
        /*
         * Filter messages based on message type. Since it is likely that
         * GCM will be extended in the future with new message types, just
         * ignore any message types you're not interested in, or that you
         * don't recognize.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            // sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            // sendNotification("Deleted messages on server: " +
            // extras.toString());
            // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // This loop represents the service doing some work.
            // for (int i = 0; i < 5; i++) {
            // Log.d("", "Working... " + (i + 1) + "/5 @ " +
            // SystemClock.elapsedRealtime());
            // try {
            // Thread.sleep(5000);
            // } catch (InterruptedException e) {
            // }
            // }
            Log.d("true", "Completed work @ " + SystemClock.elapsedRealtime());
            // Post notification of received message.
            sendNotification(extras.getString("msg"), extras.getString("title"), extras.getString("id"));

            Log.d("true", "Received: " + extras.toString());
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg, String title, String id) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    int notification_id = Integer.parseInt(id);

    // PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new
    // Intent(this, CommonUtils.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.icon).setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    // mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(notification_id, mBuilder.build());
}

}

This is manifest,

 <!-- 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" />

<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
    android:name="com.eye.hor.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

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

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />

Receiver Code in Manifest,

<receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.eye.hor.service" />
        </intent-filter>
    </receiver>

    <service android:name="com.eye.hor.service.GCMIntentService" />

My main package name is com.eye.hor but i placed GCMIntentService and GCMBroadcastReceiver java files in the com.eye.hor.service package. i already put log in these two classes but they not reach. please help

3
If any one knows where am i wrong, please help me to solve this.user3800832
The problem has to be server side. I have a similar implementation and it works just fine. What server are you using?user3629714
i have saver which runs on php and MySql. Is my above implementation ok ??? Now i changed code as @Eran said.user3800832
Which php lib are you using? The server is the problem, not this client.user3629714

3 Answers

1
votes

Looks like you already know you have a problem with the package names :

The following looks for your service class in the wrong package :

ComponentName comp = 
    new ComponentName(context.getPackageName(), // this is the main package of
                                                // your app - com.eye.hor
                      GcmIntentService.class.getName());

Change it to :

ComponentName comp = 
    new ComponentName(GcmIntentService.class.getPackage().getName(), 
                      GcmIntentService.class.getName());

You should also change the category in your manifest to the main package :

<category android:name="com.eye.hor.service" />

to

<category android:name="com.eye.hor" />
1
votes
<category android:name="com.eye.hor.service" />

This is not supposed to be the package your broadcastreceiver is in, this is the package defined in the google developers console.

I'm willing to bet in your google developers console you defined the package has com.eye.hor, in which case you would have to change this line to:

<category android:name="com.eye.hor" />
0
votes
  1. What is device on which you developed your app? On different devices there are exotic settings influence on background working, check such setting in your device ("autostart", "push notification" and other, they can called different on some devices like Xiamo, Huawei and other).

  2. Are you sure that your push server uses correct "sender id" and "api key" parameters, which you can check in developer console?

  3. Check that Google Play services installed correct.