PHP:如何发送GCM通知到多个设备一个职位 [英] PHP: How to send GCM notification to multiple devices at one post

查看:142
本文介绍了PHP:如何发送GCM通知到多个设备一个职位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Android开发人员,目前正在GCM服务器端使用PHP。从我的客户端,我存储在一个数据库中的GCM响应令牌,现在我要发送消息到注册用户,一气呵成。我无法弄清楚如何做到这一点,我完全糊涂了。我简单的HTML文件是:

I'm an android developer and currently working on GCM server side using PHP. From my client side I'm storing the gcm response token in one database, and now I want to send message to the registered users, at one go. I'm not able to figure out how to do that, and I'm totally confused. My simple HTML file is:

<html>
<head>
<title>
GCM
</title>
</head>
<body topmargin="50" leftmargin="50" CLASS = "bg">
<form action="send_message.php" name="myform" method="post">
<h4>Admin for sending notificaion to the registered user</h4>
Notification Text: <input name="appText" size="15" type="text"/><br>
URL: <input name="url" size="15" type="text" /><br>
<button type="submit">Submit</button>
</form>
</body>
</html>

和,我send_message.php:

And, my send_message.php:

<?php
error_reporting (0);

$link = mysql_connect("localhost","db","db"); 
if (!$link) 
 { 
 die(mysql_error()) ; 
 } 
mysql_select_db("gcm_user")or die(mysql_error());
........................................
.....................................
.....................................
?>

现在,在PHP文件我该怎么办,我已经试过这个链接<一个href="http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/"相对=nofollow> Android的蜂巢例如

Now, in the php file what should I do, I have tried this link Android Hive example

只是,现在可以通过这个链接来了。请建议我如何使用此解决方案,我的问题。 新的链接,我的问题。

Just, now came through this link. Please suggest me how to use this solution for my problem. New link for my problem.

但我想只有一个我的目的的PHP。请在克服这一PRM帮助。

But I want only one php for my purpose. Please help in overcoming this prm.

推荐答案

回答我的问题。这是我做了什么,以满足我的要求。

Answering my own question. Here is what I have done to meet my requirements.

$gcm_text = $_POST['gcmText'];
$gcm_url = $_POST['gcmURL'];
$gcm_subText = $_POST['gcm_secondText'];

$sql = "select gcm_response_token as regId from GCM";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    $gcm_array[]=$row['regId'];
    $counter++;
}

mysql_free_result($result);


    $body['allsearch'] = array(
            'gcmText' => $gcm_text,
            'gcm_secondText' => $gcm_subText,
            'gcmURL' => $gcm_url
            );

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

  $fields = array(
 'registration_ids' => $gcm_array,
 'data' => $body,
 );

//echo GOOGLE_API_KEY;
 $headers = array(
 'Authorization: key=$apiKey',
  'Content-Type: application/json'
 );


print_r($headers);
// 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;

?>

这样,我就能够JSON数据发送到客户端,并得到了通知。

By this, I was able to send json data to the client side and get the notification.

这篇关于PHP:如何发送GCM通知到多个设备一个职位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆