Android - 数据库中字段更改时的通知

时间:2017-07-23 12:58:03

标签: php android mysql json notifications

我对编程很陌生。 我正在为android创建一个从数据库获取有关事件数据的应用程序,用户可能会添加新事件,但是他们将“status”设置为0,因此我必须将它们在phpmyadmin中更改为1以使它们在MainActivity中显示为listview。一切都是通过php和json完成的。 我想制作某种通知,当新事件将其状态在数据库中更改为1时,通知将发送给拥有该应用程序的所有人。什么是最好的方法?我清楚地描述了一切吗? 我正在通过互联网查看有关GCM的信息,但我很遗憾,我不知道从哪里开始。你有没有一些教程可以实现我的目标?

1 个答案:

答案 0 :(得分:0)

为了发送通知进行前瞻性词组更改,我创建了 php 文件:

<?php
define("servername","your_servername");
define("username","your_username");
define("password","your_password");
define("dbname","your_dbname");

define( 'API_ACCESS_KEY', your_API_Key' );

$topic = "/topics/todos";
$url = 'https://fcm.googleapis.com/fcm/send';

$exists=0;
$frasenova=$_POST['frase'];
$autornovo=$_POST['autor'];

$conn =new mysqli(servername, username, password, dbname);

if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}else{
    $sqlquery="SELECT * FROM frase_dia WHERE id='1'";

$stmt=$conn->prepare($sqlquery);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $frase, $autor);
while($stmt->fetch()) 
{
    $exists=$exists+1;
}

if($exists==1){
    //if row exists
    $sqlupdate="UPDATE frase_dia SET frase='".$frasenova."', autor='".$autornovo."' WHERE id='1'";

    $stmt=$conn->prepare($sqlupdate) or trigger_error($conn->error, E_USER_ERROR);  
    $stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);

    //notification
    $fields = array (
        'to' => $topic,
        'notification' => array (
                "body" => $frasenova."\n\n".$autornovo,
                "title" => "Frase do dia",
                "sound"=> "default"
        ),
        'data' => array(
            "frase" => $frasenova,
            "autor"=> $autornovo,
            "tiponotificacao"=> 1
        )
);
}else{
    //if row not exists
    $sqlnew="INSERT INTO frase_dia (frase, autor) VALUES ('$frasenova','$autornovo')";
    $stmt=$conn->prepare($sqlnew) or trigger_error($conn->error, E_USER_ERROR); 
    $stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);
    echo 'adicionado (novo)';

    //notification
    $fields = array (
        'to' => $topic,
        'notification' => array (
                "body" => $frasenova."\n\n".$autornovo,
                "title" => "Frase do dia",
                "sound"=> "default",
                "click_action"=> "inicio"
        ),
        'data' => array(
            "frase" => $frasenova,
            "autor"=> $autornovo,
            "tiponotificacao"=> 1
        )
);
}

$fields = json_encode ( $fields );
$headers = array (
        'Authorization: key=' . API_ACCESS_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, $fields );

$result = curl_exec ( $ch );
curl_close ( $ch );

echo'<div>
<svg style="width:48px;height:48px" viewBox="0 0 24 24">
    <path fill="#a62a2a" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" />
</svg>
<br>
<font size="+2">';
    echo 'Conteúdo&nbsp;adicionado';
echo'</font>
<br><br>
</div>';
}

?>

HTML 文件:

<html>
<head>
<meta charset="utf-8">
<title>Frase do dia</title>
<!--Import Google Icon Font-->
      <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
      <!--Import materialize.css-->
      <link type="text/css" rel="stylesheet" href="css/materialize.min.css"  media="screen,projection"/>

      <!--Let browser know website is optimized for mobile-->
      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

      <style>
   /* label focus color */
   .input-field input[type=text]:focus + label {
     color: #a62a2a;
   }
   /* label underline focus color */
   .input-field input[type=text]:focus {
     border-bottom: 1px solid #a62a2a;
     box-shadow: 0 1px 0 0 #a62a2a;
   }
   .input-field textarea[class=materialize-textarea]:focus + label {
color: #a62a2a;
}
   .input-field textarea[class=materialize-textarea]:focus {
border-bottom: 1px solid #a62a2a;
box-shadow: 0 1px 0 0 #a62a2a;
}

      </style>
</head>

<body bgcolor="#F2F2F2">
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
      <script type="text/javascript" src="js/materialize.min.js"></script>
      <div style="margin-left:20%; margin-right:20%; background-color:#FFF; border-radius:5px; margin-bottom:25px;">
      <h1 style="text-align:center; padding-top:10px;">Frase do dia</h1>
      <div class="row">
    <form class="col s12" action="adicionar_frase.php" method="post">
      <div class="row">
        <div class="input-field col s12">
          <input id="frase" type="text" name="frase" required>
          <label for="frase">Frase</label>
        </div>
      </div>
      <div class="row">
        <div class="input-field col s12">
          <input id="autor" type="text" name="autor" required>
          <label for="autor">Autor</label>
        </div>
      </div>
      <button style="display:block; margin:auto" class="btn waves-effect waves-light red darken-3" type="submit" name="Adicionar">Adicionar
  </button>

    </form>
  </div>
  <div style="width:100%; height:5px;"></div>
   </div>   
</body>
</html>

所以我要做的是将 HTML 文件中的表单数据发送到 php 文件,并在php文件中更改mysql数据库表中的短语,如果更改为成功我创建并向用户发送通知。

只要问一下,您就可以将这些代码调整到您的项目中。