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);
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) {
android null push-notification google-cloud-messaging
Mert karatas
source share