gpt4 book ai didi

php - 使用 GCM 向多个 android 设备发送推送通知

转载 作者:IT老高 更新时间:2023-10-28 23:11:54 24 4
gpt4 key购买 nike

我关注 http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/ ?通过 GCM 发送推送通知。一切正常,但我只能向一台设备发送推送通知。注册另一个设备会替换先前设备的注册 ID。我尝试了 Shardool 在 http://javapapers.com/android/android-multicast-notification-using-google-cloud-messaging-gcm/ 中提供的解决方案?但它不工作。

任何建议都会有很大帮助。

这是我的 gcm.php 代码,用于注册设备并发送推送通知,但仅限于最近注册的单个设备。

gcm.php

<?php
//generic php function to send GCM push notification
function sendPushNotificationToGCM($registatoin_ids, $message) {
//Google cloud messaging GCM-API url
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
// Google Cloud Messaging GCM API Key
define("GOOGLE_API_KEY", "MY_KEY");
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
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_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
?>
<?php
//this block is to post message to GCM on-click
$pushStatus = "";
if ( ! empty($_GET["push"])) {
$gcmRegID = file_get_contents("GCMRegId.txt");
$pushMessage = $_POST["message"];
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("m" => $pushMessage);
$pushStatus = sendPushNotificationToGCM($gcmRegIds, $message);
}
}
//this block is to receive the GCM regId from external (mobile apps)
if ( ! empty($_GET["shareRegId"])) {
$gcmRegID = $_POST["regId"];
file_put_contents("GCMRegId.txt",$gcmRegID);
echo "Ok!";
exit;
}
?>
<html>
<head>
<title>Google Cloud Messaging (GCM) Server in PHP</title>
</head>
<body>
<h1>Google Cloud Messaging (GCM) Server in PHP</h1>
<form method="post" action="gcm.php/?push=1">
<div>
<textarea rows="2" name="message" cols="23" placeholder="Message to transmit via GCM"></textarea>
</div>
<div><input type="submit" value="Send Push Notification via GCM" /></div>
</form>
<p><h3><?php echo $pushStatus; ?></h3></p>
</body>
</html>

请告诉我如何在 GCMRegid.txt 中存储多个设备注册 ID 并向每个注册设备发送通知

最佳答案

在这里,我使用 mysql 重写了 php,而不是从文件中检索 key 。在这种情况下,我从表中检索所有 regIds 并将它们放入一个数组中并将其传递给 sendPushNotification 函数以将消息推送给所有人。这里有 2 个文件,一个用于连接数据库,一个用于 GCM:

connect.php:

<?php

$db_host = "localhost";
$db_user = "root"; //change to your database username, it is root by default
$db_pass = ''; //change to your database password, for XAMPP it is nothing for WAMPP it is root
$db_db = 'gcmFirst'; //change to your database name

if(!@mysql_connect($db_host, $db_user, $db_pass) || !@mysql_select_db($db_db)) {
die('couldnt connect to database ' .mysql_error());
}

?>

gcm.php

<?php
require 'connect.php';

function sendPushNotification($registration_ids, $message) {

$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);

define('GOOGLE_API_KEY', 'AIzaSyCjctNK2valabAWL7rWUTcoRA-UAXI_3ro');

$headers = array(
'Authorization:key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
echo json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());

curl_close($ch);
return $result;

}

$pushStatus = '';

if(!empty($_GET['push'])) {

$query = "SELECT regId FROM users";
if($query_run = mysql_query($query)) {

$gcmRegIds = array();
while($query_row = mysql_fetch_assoc($query_run)) {

array_push($gcmRegIds, $query_row['regId']);

}

}
$pushMessage = $_POST['message'];
if(isset($gcmRegIds) && isset($pushMessage)) {

$message = array('message' => $pushMessage);
$pushStatus = sendPushNotification($gcmRegIds, $message);

}
}

if(!empty($_GET['shareRegId'])) {

$gcmRegId = $_POST['regId'];
$query = "INSERT INTO users VALUES ('', '$gcmRegId')";
if($query_run = mysql_query($query)) {
echo 'OK';
exit;
}
}
?>

<html>
<head>
<title>Google Cloud Messaging (GCM) Server in PHP</title>
</head>
<body>
<h1>Google Cloud Messaging (GCM) Server in PHP</h1>
<form method = 'POST' action = 'gcm.php/?push=1'>
<div>
<textarea rows = 2 name = "message" cols = 23 placeholder = 'Messages to Transmit via GCM'></textarea>
</div>
<div>
<input type = 'submit' value = 'Send Push Notification via GCM'>
</div>
<p><h3><?php echo $pushStatus ?></h3></p>
</form>
</body>
</html>

您所要做的就是创建一个数据库,其中包含一个用户表,其中 id、regId 和 name 作为列。

希望这就是你要找的东西

关于php - 使用 GCM 向多个 android 设备发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25859898/

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