如何使用代码Android使用FCM从一台设备向另一台设备发送通知

时间:2017-09-03 18:19:02

标签: android android-volley firebase-cloud-messaging

我想将通知从一台设备发送到另一台设备,例如: 在按钮上单击发送通知到令牌, 我已经有了包含发送通知的php:

enter code here
<?php 
      require "conn.php";

    function send_notification ($tokens)
 { 
 $msg   =$_POST['msg'];
 $title =$_POST['title'];
 $url = 'https://fcm.googleapis.com/fcm/send';
 $priority="high";
 $notification= array('title'=>$title,'body' => $msg );

 $fields = array(
     'registration_ids' => $tokens,
     'notification' => $notification

    );


$headers = array(
    'Authorization:key=XXXXXXXXXX',
    '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_SSL_VERIFYHOST, 0);  
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
  // echo json_encode($fields);
 $result = curl_exec($ch);           
 echo curl_error($ch);
 if ($result === FALSE) {
   die('Curl failed: ' . curl_error($ch));
}
 curl_close($ch);
 return $result;
}
$sql = "SELECT Token from TEST";
$result= mysqli_query($conn,$sql);
$tokens= array();
if(mysqli_num_rows($result)>0){
  while($row = mysqli_fetch_assoc($result)){
  $tokens [] = $row["Token"];
    }
  }

  $message_status = send_notification($tokens);
  echo $message_status;
 ?>

我真正想做的是使用代码发送使用Php的Notification 和想法怎么做?

1 个答案:

答案 0 :(得分:0)

在android上使用以下命令发送通知:https://gist.github.com/jimfleming/c1adfdb0f526465c99409cc143dea97b

FirebasePush firebasePush = new FirebasePush("LEGACY_SERVER_KEY");
firebasePush.setNotification(new Notification("title","body"));
firebasePush.sendToTopic("news");

我希望我有所帮助:D