I'm building one basic webpage with some forms that would send Firebase push notifications to an Android app. Everything works fine, except one thing. Every time I load the page, the notification is sent right at the page initiation. That is, before even filling in the form.
I have then added an if
condition so that when the form is empty (e.g. $_POST['message'] == NULL
), curl_exec
would not be triggered. It works fine like this, but I have a lot of Notice: messages when loading the page. I can easily ignore that, but I can see now that when I refresh the page, the data in the forms stays the same. Can anyone help me to fix this issue?
The code is given below:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Firebase Push Notification</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-6">
<hr />
<form action="" method="post">
<div class="form-group">
<label for="title">Title:</label>
<input type="text" required="" class="form-control" id="title" placeholder="Title" name="title">
</div>
<div class="form-group">
<label for="message">Content:</label>
<textarea required="" class="form-control" rows="2" id="message" placeholder="Content" name="message"></textarea>
</div>
<div class="form-group">
<label for="destinatia">Where to open?:</label>
<select class="form-control" name="destinatia" required>
<option value="first_page">First page</option>
<option value="events">Events</option>
</select>
</div>
<button type="submit" class="btn btn-info">Send</button>
</form>
</div>
<?php
define('API_ACCESS_KEY', 'AAAA0xxxxxxxxxxxxxxxxxxxx');
$msg = array
(
'body' => $_POST['message'],
'title' => $_POST['title'],
'priority' => 'high',
'vibrate' => 100,
'sound' => 'default',
'click_action' => $_POST['destinatia'],
//'time_to_live' => 3600
);
$fields = array('to' => '/topics/CHECK', 'notification' => $msg);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
if($_POST['message'] == NULL){
echo 'Empty';}
else {
$result = curl_exec($ch);
echo 'Not empty';
curl_close($ch);
echo '<h2>Checking:</h2><hr/><h3>Sent:</h3><p><pre>';
echo json_encode($fields,JSON_PRETTY_PRINT);
echo '</pre></p><h3>ID</h3><p><pre>';
echo $result;
echo '</pre></p>';
}
?>