Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't receive messages using Google Cloud Messaging for Android (not the helper library)
    text
    copied!<p>Does anybody can help me with a working example for receiving messages from gcm using Google Cloud Messaging for Android. I have tried both ways (helper library and GoogleCloudMessaging class) and nothing seems to work. I'm using a PHP script that shows the following:</p> <p>Multicast ID: 5.2108110103215E+18 Number of messages processed successfully: 1 Number of messages with processing errors: 0 Canonical IDs: 0</p> <p>So apparently everithing is OK. I could register the device in both ways, using the helper library (gcm.jar) and using GoogleCloudMessaging class. The problem is that there is no way the message I send via PHP arrives, or at least I don't know how to handle it correctly. Here are the permissions and the receiver from my manifest:</p> <pre><code>&lt;permission android:name="com.example.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" /&gt; &lt;uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" /&gt; &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt; &lt;uses-permission android:name="android.permission.USE_CREDENTIALS" /&gt; &lt;uses-permission android:name="android.permission.READ_OWNER_DATA" /&gt; &lt;receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" &gt; &lt;intent-filter&gt; &lt;!-- Receives the actual messages. --&gt; &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt; &lt;!-- Receives the registration id. --&gt; &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt; &lt;category android:name="com.example.gcm" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;service android:name=".GCMIntentService" /&gt; </code></pre> <p>Finally here is the service class</p> <pre><code>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); // Notify main UI to update registration status Intent registrationIntent = new Intent(); registrationIntent.setAction("registered"); registrationIntent.putExtra("regId", regId); sendBroadcast(registrationIntent); } @Override protected void onUnregistered(Context ctx, String regId) { //... } } </code></pre> <p>Here is the code when using the GoogleCloudMessaging class (I changed the manifest to use the custom receiver):</p> <pre><code>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); } // Put the GCM message into a notification and post it. private void sendNotification(String msg) { mNotificationManager = (NotificationManager) ctx .getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, MainActivity.class), 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( ctx).setSmallIcon(R.drawable.ic_launcher_temp) .setContentTitle("GCM Notification") .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setContentText(msg); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); } </code></pre> <p>} </p> <p>The thing is that everything seems to be ok, but the message never arrives. Any ideas?? Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload