GCM returns a null message type

I created an application that uses GoogleCloudMessaging. An application can register with gcm and save its registration identifier in a database on my server. I use php to send push notifications, but when Google sends it to my device, the service-service finds its message type to be zero. I tried the same code in another application and it worked well. but this time it’s not. An application can receive a message from google and process it by showing a notification with empty text. I have provided maintenance services and php codes below. Thank you for your responses.

send_message.php

<?php if (isset($_GET["regId"]) && isset($_GET["message"])) { $regId = $_GET["regId"]; $message = $_GET["message"]; include_once './GCM.php'; $gcm = new GCM(); $registatoin_ids = array($regId); $message = array("price" => $message); $result = $gcm->send_notification($registatoin_ids, $message); echo $result; } ?> 

Gcm.php

 <?php class GCM { //put your code here // constructor function __construct() { } /** * Sending Push Notification */ public function send_notification($registatoin_ids, $message) { // include config include_once './config.php'; // Set POST variables $url = 'https://android.googleapis.com/gcm/send'; $fields = array( 'registration_ids' => $registatoin_ids, 'data' => $message, ); $headers = array( 'Authorization: key=' . GOOGLE_API_KEY, 'Content-Type: application/json' ); // Open connection $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Disabling SSL Certificate support temporarly curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); // Execute post $result = curl_exec($ch); if ($result === FALSE) { die('Curl failed: ' . curl_error($ch)); } // Close connection curl_close($ch); echo $result; } } ?> 

MyIntentService.java

 public class MyIntentService extends IntentService { public static final int NOTIFICATION_ID = 1; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; public MyIntentService() { super("MyIntentService"); } @Override protected void onHandleIntent(Intent intent) { Log.v(MainActivity.TAG, "Handling 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); generateNotification(getApplicationContext(), extras.getString("price")); Log.v(MainActivity.TAG, "IntentService messagetype= " + messageType); 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 * 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 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.i(TAG, "Working... " + (i + 1) // + "/5 @ " + SystemClock.elapsedRealtime()); // try { // Thread.sleep(5000); // } catch (InterruptedException e) { // } // } Log.i(MainActivity.TAG, "Completed work @ " +SystemClock.elapsedRealtime()); // Post notification of received message. sendNotification("Received: " + extras.toString()); generateNotification(getApplicationContext(), "Received:" + extras.getString("price")); Log.i(MainActivity.TAG, "Received: " + extras.toString()); } } // Release the wake lock provided by the WakefulBroadcastReceiver. WakefulBroadcastReceiver.completeWakefulIntent(intent); } 

In short, "Log.v (MainActivity.TAG," IntentService messagetype = "+ messageType); displays" IntentService messagetype = null. "How can I solve this problem?

 public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 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); } } 
+8
android null push-notification google-cloud-messaging
source share
3 answers

Check out AndroidManifest.xml! And fix the settings for GCM as shown below. In my case, I solved this "zero" problem. Good luck

 <permission android:name="[MY PACKAGE NAME].permission.C2D_MESSAGE" android:protectionLevel="signature" /> ... <uses-permission android:name="[MY PACKAGE NAME].permission.C2D_MESSAGE" /> ... <service android:name="[MY PACKAGE NAME].GCMIntentService" /> ... <receiver android:name="[MY PACKAGE NAME].GcmBroadcastReceiver" android:exported="true" 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="[MY PACKAGE NAME]" /> </intent-filter> </receiver> 
+4
source share

In my case, I fixed it by deleting:

 <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

According to the official guide, it is no longer required: https://developer.android.com/google/gcm/client.html

Note also note that exported = "true" in Receiver is also not required.

+1
source share

Instead of checking for messageType, as suggested in the documentation for Android, I just check the Intent action. This is a job in my eyes, but it works:

 /** * Action for GCM registration intents. */ private static final String ACTION_GCM_REGISTRATION = "com.google.android.c2dm.intent.REGISTRATION"; /** * Action for new app updated installed intent. */ private static final String ACTION_PACKAGE_REPLACED = "android.intent.action.PACKAGE_REPLACED"; @Override protected void onHandleIntent(final Intent intent) { final Bundle extras = intent.getExtras(); final String action = intent.getAction(); final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); // messageType will be null for broadcasts with action registration // or package replaced String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { if (GoogleCloudMessaging. MESSAGE_TYPE_MESSAGE.equals(messageType)) { onNotification("Received: " + extras.toString()); } else if (action.equals(ACTION_GCM_REGISTRATION)) { onRegistration(extras.getString("registration_id"), intent); } else if (action.equals(ACTION_PACKAGE_REPLACED)) { onNewAppVersion(); } } GcmBroadcastReceiver.completeWakefulIntent(intent); } 
-one
source share

All Articles