php - GCM push notification coming but with empty message -


when submit data through php file giving notification registered phone there empty message no message content.mostly tried tutorial available online not getting it.please help.

server side code:php file

<?php $con = mysql_connect("localhost", "umane", "pass"); if(!$con){ die('mysql connection failed'); }  $db = mysql_select_db("dbname"); if(!$db){ die('database selection failed'); }  $registatoin_ids = array(); $sql = "select * tblname"; $result = mysql_query($sql, $con); while($row = mysql_fetch_assoc($result)){ array_push($registatoin_ids, $row['registration_id']);  }  // set post variables $url = 'https://android.googleapis.com/gcm/send';  $message = array("notice" => $_post['message']); $fields = array( 'registration_ids' => $registatoin_ids, 'data' => $message, );  $headers = array( 'authorization: key= api key', 'content-type: application/json' ); // open connection $ch = curl_init();  // set 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; ?> 

android code:

public class gcmsender {      public static final string api_key = "my api key";      public static void main(string[] args) {         if (args.length < 1 || args.length > 2 || args[0] == null) {             system.err.println("usage: ./gradlew run -pmsg=\"message\" [-pto=\"device_token\"]");             system.err.println("");             system.err.println("specify test message broadcast via gcm. if device's gcm registration token is\n" +                     "specified, message sent device. otherwise, message \n" +                     "will sent devices subscribed \"global\" topic.");             system.err.println("");             system.err.println("example (broadcast):\n" +                     "on windows:   .\\gradlew.bat run -pmsg=\"<your_message>\"\n" +                     "on linux/mac: ./gradlew run -pmsg=\"<your_message>\"");             system.err.println("");             system.err.println("example (unicast):\n" +                     "on windows:   .\\gradlew.bat run -pmsg=\"<your_message>\" -pto=\"<your_token>\"\n" +                     "on linux/mac: ./gradlew run -pmsg=\"<your_message>\" -pto=\"<your_token>\"");             system.exit(1);         }         try {             // prepare json containing gcm message content. send , send.             jsonobject jgcmdata = new jsonobject();             jsonobject jdata = new jsonobject();             jdata.put("data", args[0].trim());             // send gcm message.             if (args.length > 1 && args[1] != null) {                 jgcmdata.put("registration_ids", args[1].trim());             } else {                 jgcmdata.put("registration_ids", "/topics/global");             }             // send in gcm message.             jgcmdata.put("data", jdata);              // create connection send gcm message request.             url url = new url("https://android.googleapis.com/gcm/send");             httpurlconnection conn = (httpurlconnection) url.openconnection();             conn.setrequestproperty("authorization", "key=" + api_key);             conn.setrequestproperty("content-type", "application/json");             conn.setrequestmethod("post");             conn.setdooutput(true);              // send gcm message content.             outputstream outputstream = conn.getoutputstream();             outputstream.write(jgcmdata.tostring().getbytes());              // read gcm response.             inputstream inputstream = conn.getinputstream();             string resp = ioutils.tostring(inputstream);             system.out.println(resp);             system.out.println("check device/emulator notification or logcat " +                     "confirmation of receipt of gcm message.");         } catch (ioexception e) {             system.out.println("unable send gcm message.");             system.out.println("please ensure api_key has been replaced server " +                     "api key, , device's registration token correct (if specified).");             e.printstacktrace();         }     }  } 

receiver code

/**  * copyright 2015 google inc. rights reserved.  *  * licensed under apache license, version 2.0 (the "license");  * may not use file except in compliance license.  * may obtain copy of license @  *  * http://www.apache.org/licenses/license-2.0  *  * unless required applicable law or agreed in writing, software  * distributed under license distributed on "as is" basis,  * without warranties or conditions of kind, either express or implied.  * see license specific language governing permissions ,  * limitations under license.  */  package gcm.play.android.samples.com.gcmquickstart;  import android.app.notificationmanager; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.media.ringtonemanager; import android.net.uri; import android.os.bundle; import android.support.v4.app.notificationcompat; import android.util.log;  import com.google.android.gms.gcm.gcmlistenerservice;  public class mygcmlistenerservice extends gcmlistenerservice {      private static final string tag = "mygcmlistenerservice";      /**      * called when message received.      *      * @param senderid of sender.      * @param data data bundle containing message data key/value pairs.      *             set of keys use data.keyset().      */     // [start receive_message]     @override     public void onmessagereceived(string from, bundle data) {         string message = data.getstring("data");         log.d(tag, "from: " + from);         log.d(tag, "message: " + message);          if (from.startswith("/topics/")) {             // message received topic.         } else {             // normal downstream message.         }          // [start_exclude]         /**          * production applications process message here.          * eg: - syncing server.          *     - store message in local database.          *     - update ui.          */          /**          * in cases may useful show notification indicating user          * message received.          */         sendnotification(message);         // [end_exclude]     }     // [end receive_message]      /**      * create , show simple notification containing received gcm message.      *      * @param message gcm message received.      */     private void sendnotification(string message) {         intent intent = new intent(this, mainactivity.class);         intent.addflags(intent.flag_activity_clear_top);         pendingintent pendingintent = pendingintent.getactivity(this, 0 /* request code */, intent,                 pendingintent.flag_one_shot);          uri defaultsounduri = ringtonemanager.getdefaulturi(ringtonemanager.type_notification);         notificationcompat.builder notificationbuilder = new notificationcompat.builder(this)                 .setsmallicon(r.drawable.ic_stat_ic_notification)                 .setcontenttitle("e protocol")                 .setcontenttext(message)                 .setautocancel(true)                 .setsound(defaultsounduri)                 .setcontentintent(pendingintent);          notificationmanager notificationmanager =                 (notificationmanager) getsystemservice(context.notification_service);          notificationmanager.notify(0 /* id of notification */, notificationbuilder.build());     } } 

you it's retrieving post variable in php code? try replace this:

$message = array("notice" => $_post['message']); 

with:

$message = array("notice" => "testing"); 

also in receiver saying

string message = data.getstring("data"); 

but defined data "notice" in php code, retrieve data must say:

string message = data.getstring("notice"); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -