Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在MySQL中插入新行时,如何自动发送android推送通知?_Php_Android_Mysql_Push Notification - Fatal编程技术网

Php 在MySQL中插入新行时,如何自动发送android推送通知?

Php 在MySQL中插入新行时,如何自动发送android推送通知?,php,android,mysql,push-notification,Php,Android,Mysql,Push Notification,目前我有两个android应用程序共享同一个MySQL数据库。第一个应用程序将向数据库发布数据。第二个应用程序将显示数据库数据。如何在第一个应用将数据发布到数据库后立即自动向第二个应用发送推送通知 知道如何用GCM解决这个问题吗?将数据发布到数据库后,执行以下代码 $apiKey = "your api key"; $db_username = 'root'; $db_password = 'root'; $db_name

目前我有两个android应用程序共享同一个MySQL数据库。第一个应用程序将向数据库发布数据。第二个应用程序将显示数据库数据。如何在第一个应用将数据发布到数据库后立即自动向第二个应用发送推送通知


知道如何用GCM解决这个问题吗?

将数据发布到数据库后,执行以下代码

$apiKey = "your api key";

        $db_username    = 'root';
        $db_password    = 'root';
        $db_name        = 'gcm001';
        $db_host        = 'localhost';
        $mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);


        if($mysqli->connect_errno > 0){
            die('Unable to connect to database [' . $mysqli->connect_error . ']');
        }   

        //get registration id's from database, whom you want to send notification  ... assuming you have stored registration ids of 2nd app in database
        $query = "SELECT * FROM RegisteredDevices where userid = $userid";

        $result = mysqli_query($mysqli, $query);
        $i=0;
        $data = array();
        while($row   = mysqli_fetch_assoc($result))
        {
            $data[$i] = $row['reg_id'];
            $i++;
        }

        $registrationIDs = array_values($data);
        //print_r($registrationIDs);

        $url = 'https://android.googleapis.com/gcm/send';



        $fields = array(
                        'registration_ids'  => array_values( $registrationIDs ),
                        'data'              => array( "message" => "Your notification message here" ),
                        );

        $headers = array( 
                            'Authorization: key=' . $apiKey,
                            '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 );
         curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

        // Execute post
        $result = curl_exec($ch);

        // Close connection
        curl_close($ch); 

        echo $result;

希望这有帮助……

你的问题真的能自己回答。这个?让我们看看你到目前为止都做了些什么??