php脚本用php curl执行两次

时间:2016-06-26 13:28:53

标签: php android firebase firebase-cloud-messaging

我在我的Android应用和PHP脚本中使用Firebase推送通知服务。我想在每次插入mysql DB后发送通知。我遇到问题,当我调用名为sendNotifications()的自定义方法时,它再次执行php脚本并在数据库中执行两次执行,以便通知也发送两次。这是代码.. 从这里我调用insert到DB,之后我调用sendNotifications方法

if($_POST['action']  == $news::$INSERT)
    {
            $isInserted = $news->insert($_POST);
            if($isInserted){            
                deliverResponse(200,'Success..',true,NULL);
                $message = $news->selectLastInserted();
                if(count($message) > 0)
                {
                    require_once('fcm_notify.php');
                    $obj = new NotifiyUsers();
                    $obj->sendNotifications($message);
                }
            }
            else
                deliverResponse(200,'Failed...',false,NULL);

    }

这是我的sendNotification方法

class NotifiyUsers{
function sendNotifications($message)
{
    //getting tokens from users table to send notification
    $users = new FMCUser();
    $tokens = $users->selectTokens();
    if(count($users)<0) // if no user register
        return;
    ////
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array(
                    'registration_ids' => $tokens,
                    '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);            
        return $result;
}
}

0 个答案:

没有答案