Can someone help me with a working example for receiving messages from gcm using Google Cloud Messaging for Android. I tried both paths (helper library and GoogleCloudMessaging class) and nothing works. I am using a PHP script that shows the following:
Multicast ID: 5.2108110103215E + 18 Number of successfully processed messages: 1 Number of messages with processing errors: 0 Canonical identifiers: 0
So it seems that everything is in order. I could register the device in both directions using the helper library (gcm.jar) and using the GoogleCloudMessaging class. The problem is that I am not sending a message sent via PHP, or at least I do not know how to handle it correctly. Here are the permissions and the recipient from my manifest:
<permission android:name="com.example.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.USE_CREDENTIALS" /> <uses-permission android:name="android.permission.READ_OWNER_DATA" /> <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="com.example.gcm" /> </intent-filter> </receiver> <service android:name=".GCMIntentService" />
Finally, this is a class of service
public class GCMIntentService extends GCMBaseIntentService { private static final String PROJECT_ID = "49XXXXXXXX6"; private static final String TAG = "GCM Intent Service"; public GCMIntentService() { super(PROJECT_ID); Log.d(TAG, "GCMIntentService init"); } @Override protected void onError(Context ctx, String sError) { Log.d(TAG, "Error: " + sError); } @Override protected void onMessage(Context ctx, Intent intent) { Log.d(TAG, "Message Received"); String message = intent.getStringExtra("message"); sendGCMIntent(ctx, message); } private void sendGCMIntent(Context ctx, String message) { Intent broadcastIntent = new Intent(); broadcastIntent.setAction("GCM_RECEIVED_ACTION"); broadcastIntent.putExtra("gcm", message); ctx.sendBroadcast(broadcastIntent); } @Override protected void onRegistered(Context ctx, String regId) { Log.d(TAG, regId);
Here is the code when using the GoogleCloudMessaging class (I changed the manifest to use a custom receiver):
public class GCMBroadcastReceiver extends BroadcastReceiver { private static final String TAG = "GCM Receiver"; public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; private Context ctx; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "Message received"); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); ctx = context; String messageType = gcm.getMessageType(intent); if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { sendNotification("Send error: " + intent.getExtras().toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED .equals(messageType)) { sendNotification("Deleted messages on server: " + intent.getExtras().toString()); } else { sendNotification("Received: " + intent.getExtras().toString()); } setResultCode(Activity.RESULT_OK); }
}
The fact is that everything looks fine, but the message never arrives. Any ideas?? Thanks in advance.
Alejandro Casanova
source share