Unable to retrieve messages using Google Cloud Messaging for Android (and not a secondary library)

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

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); } // 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()); } 

}

The fact is that everything looks fine, but the message never arrives. Any ideas?? Thanks in advance.

+7
source share
5 answers

Add the following to your manufactory:

  <permission android:name="PACKAGE_NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="PACKAGE_NAME.permission.C2D_MESSAGE" /> <!-- App receives GCM messages. --> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
+7
source

Your manifest is missing some permissions:

 <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="com.example.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" /> 

EDIT:

In the manifest, you are using com.google.android.gcm.GCMBroadcastReceiver . This is a class from the old helper library ( gcm.jar ) that starts the intent service. If you want to use the helper library, you must define the intent service in your manifest.

If you do not want to use the helper library, you should modify the GCMBroadcastReceiver package in the manifest as the package of the GCMBroadcastReceiver class that you included in your question. Otherwise, this class will not be used.

+2
source

After fixing the above problems, there are still no push messages. The problem was error 403. It seems that this service is not available in Cuba, so the solution is to use a tool such as “your freedom” or “rights”, etc.

+1
source

Remember to change "com.example.gcm" to your package name, for example. 'Com.yourdomain.yourapp'

0
source

You are using a GCMBaseIntentService which is deprecated by Google. (But it should still work.) I followed the last tutorial http://developer.android.com/google/gcm/client.html and received messages. You can also try.

Another possible reason is that you received a message, but it was not shown in the notification successfully. Add this line to the method: sendNotification (String msg)

  Log.i(TAG, msg); 
0
source

All Articles