gpt4 book ai didi

php - Android通知推送php错误

转载 作者:行者123 更新时间:2023-11-29 21:43:39 25 4
gpt4 key购买 nike

我正在尝试向用户发送通知推送,但无法完成,

这是我在 php 中使用的代码

    $message = new gcm(); 
$message->sendMessageToPhone(2, $message,$valor);

class gcm
{
function sendMessageToPhone($collapseKey, $messageText, $gcmcode)
{
$apiKey = 'there is my apikey';
$headers = array('Authorization:key=' . $apiKey);
$data = array( 'registration_id' => $gcmcode, 'collapse_key' => $collapseKey, 'data.message' => $messageText);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
if ($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) { return 'fail'; }
if ($httpCode != 200) { return 'status code 200'; }
curl_close($ch);
return $response;
}

当我执行 php 时出现错误

可捕获的 fatal error :第 25 行/path/gcm.php 中类 gcm 的对象无法转换为字符串

第 25 行 = curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

最佳答案

嗨, friend ,我必须为 Symfony 2.x 上的一些包开发一个推送通知部分仅使用类并更改 namespace ,添加您的 key ,希望对您有所帮助。我很抱歉我的英语不好。

请记住,设备的 Id 必须在云端注册。

来自 Controller :

    /*
* Google API Key
*/
define("GOOGLE_API_KEY", "xxxxxxxxxxxxxx"); // Place your Google API Key
$gcm = new \MessageBundle\Classes\GCM();

$message = "Hello";
$registatoin_ids = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$gcm->send_notification($registatoin_ids, $message);

类(class):

    <?php


/**
* GCM
*
* @author Esteban lopez
*/
class GCM {


// constructor
function __construct() {

}

/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {

// 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;
}

}

关于php - Android通知推送php错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16444359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com