• <tfoot id='KoR7D'></tfoot>
      <bdo id='KoR7D'></bdo><ul id='KoR7D'></ul>

  • <legend id='KoR7D'><style id='KoR7D'><dir id='KoR7D'><q id='KoR7D'></q></dir></style></legend>
      <i id='KoR7D'><tr id='KoR7D'><dt id='KoR7D'><q id='KoR7D'><span id='KoR7D'><b id='KoR7D'><form id='KoR7D'><ins id='KoR7D'></ins><ul id='KoR7D'></ul><sub id='KoR7D'></sub></form><legend id='KoR7D'></legend><bdo id='KoR7D'><pre id='KoR7D'><center id='KoR7D'></center></pre></bdo></b><th id='KoR7D'></th></span></q></dt></tr></i><div id='KoR7D'><tfoot id='KoR7D'></tfoot><dl id='KoR7D'><fieldset id='KoR7D'></fieldset></dl></div>

        <small id='KoR7D'></small><noframes id='KoR7D'>

      1. 使用 GCM 向多个 Android 设备发送推送通知

        sending push notifications to multiple android devices using GCM(使用 GCM 向多个 Android 设备发送推送通知)

      2. <tfoot id='c9Wlu'></tfoot>
          <legend id='c9Wlu'><style id='c9Wlu'><dir id='c9Wlu'><q id='c9Wlu'></q></dir></style></legend>
            <i id='c9Wlu'><tr id='c9Wlu'><dt id='c9Wlu'><q id='c9Wlu'><span id='c9Wlu'><b id='c9Wlu'><form id='c9Wlu'><ins id='c9Wlu'></ins><ul id='c9Wlu'></ul><sub id='c9Wlu'></sub></form><legend id='c9Wlu'></legend><bdo id='c9Wlu'><pre id='c9Wlu'><center id='c9Wlu'></center></pre></bdo></b><th id='c9Wlu'></th></span></q></dt></tr></i><div id='c9Wlu'><tfoot id='c9Wlu'></tfoot><dl id='c9Wlu'><fieldset id='c9Wlu'></fieldset></dl></div>
              <tbody id='c9Wlu'></tbody>
          • <small id='c9Wlu'></small><noframes id='c9Wlu'>

            • <bdo id='c9Wlu'></bdo><ul id='c9Wlu'></ul>

                • 本文介绍了使用 GCM 向多个 Android 设备发送推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

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

                  gcm.php

                  $registatoin_ids,'数据' =>$消息,);//Google Cloud Messaging GCM API 密钥定义(GOOGLE_API_KEY",MY_KEY");$headers = 数组('授权:密钥='.GOOGLE_API_KEY,'内容类型:应用程序/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);如果($result === FALSE){死('卷曲失败:'.卷曲错误($ ch));}curl_close($ch);返回 $result;}?><?php//这个块是在点击时将消息发布到GCM$pushStatus = "";如果(!空($_GET[推"])){$gcmRegID = file_get_contents("GCMRegId.txt");$pushMessage = $_POST["message"];if (isset($gcmRegID) && isset($pushMessage)) {$gcmRegIds = 数组($gcmRegID);$message = array("m" => $pushMessage);$pushStatus = sendPushNotificationToGCM($gcmRegIds, $message);}}//这个块是从外部(移动应用程序)接收GCM regId如果(!空($_GET[shareRegId"])){$gcmRegID = $_POST["regId"];file_put_contents("GCMRegId.txt",$gcmRegID);echo "好的!";出口;}?><头><title>PHP 中的 Google Cloud Messaging (GCM) 服务器</title><身体><h1>PHP 中的 Google Cloud Messaging (GCM) 服务器</h1><form method="post" action="gcm.php/?push=1"><div><textarea rows="2" name="message" cols="23" placeholder="通过 GCM 传输的消息"></textarea>

                  <div><input type="submit" value="Send Push Notification via GCM"/></div></表单><p><h3><?php echo $pushStatus;?></h3></p>

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

                  解决方案

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

                  connect.php:

                  gcm.php

                  $registration_ids,'数据' =>$消息,);定义('GOOGLE_API_KEY','AIzaSyCjctNK2valabAWL7rWUTcoRA-UAXI_3ro');$headers = 数组('授权:密钥='.GOOGLE_API_KEY,'内容类型:应用程序/json');回声 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_error());curl_close($ch);返回 $result;}$pushStatus = '';if(!empty($_GET['push'])) {$query = "从用户中选择 regId";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 = "插入用户值 ('', '$gcmRegId')";if($query_run = mysql_query($query)) {回声'好';出口;}}?><头><title>PHP 中的 Google Cloud Messaging (GCM) 服务器</title><身体><h1>PHP 中的 Google Cloud Messaging (GCM) 服务器</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><input type = 'submit' value = '通过 GCM 发送推送通知'>

                  <p><h3><?php echo $pushStatus ?></h3></p></表单>

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

                  希望这就是你要找的

                  I am following http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/? to send push notifications via GCM. Everything works fine but, i am able to send push notification to just one device. Registering another device replaces registration id of previous device. I tried solution provided by Shardool in http://javapapers.com/android/android-multicast-notification-using-google-cloud-messaging-gcm/? but its not working.

                  Any suggestion would be of great help.

                  Here are my gcm.php codes that registers device and sends push notifications but, only to a single device registered recently.

                  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>
                  

                  Please tell me how do I store multiple device registration ids in GCMRegid.txt and send notifications to each of the registered device

                  解决方案

                  Here I rewrote the php using mysql rather than retrieving the keys from file. In this case, I retrieve all the regIds from the table and put them in an array and pass it to sendPushNotification function to push the message to all. here you have 2 files, one for connect to database and one for 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>
                  

                  all you have to do is create a database that has a users table with id, regId and name as columns.

                  Hope this is what you are looking for

                  这篇关于使用 GCM 向多个 Android 设备发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                • <legend id='c1Rwc'><style id='c1Rwc'><dir id='c1Rwc'><q id='c1Rwc'></q></dir></style></legend>

                • <small id='c1Rwc'></small><noframes id='c1Rwc'>

                    • <bdo id='c1Rwc'></bdo><ul id='c1Rwc'></ul>
                  1. <tfoot id='c1Rwc'></tfoot>

                    <i id='c1Rwc'><tr id='c1Rwc'><dt id='c1Rwc'><q id='c1Rwc'><span id='c1Rwc'><b id='c1Rwc'><form id='c1Rwc'><ins id='c1Rwc'></ins><ul id='c1Rwc'></ul><sub id='c1Rwc'></sub></form><legend id='c1Rwc'></legend><bdo id='c1Rwc'><pre id='c1Rwc'><center id='c1Rwc'></center></pre></bdo></b><th id='c1Rwc'></th></span></q></dt></tr></i><div id='c1Rwc'><tfoot id='c1Rwc'></tfoot><dl id='c1Rwc'><fieldset id='c1Rwc'></fieldset></dl></div>