Untitled

 avatar
unknown
plain_text
2 years ago
4.2 kB
5
Indexable
<?php
    header('Content-Type: text/html; charset=utf-8');
	require 'db.php';

	$message = '';
	$messageClass = '';
	$host = 'localhost';
	$user = 'root';
	$password = 'mysql';
	$dbname = 'waliamlr_post';
	ini_set('default_charset','UTF-8');

if (filter_has_var(INPUT_POST, 'submit')) {
    $title = $_POST['title'];
    $body = $_POST['body'];
    if (!empty($title) && !empty($body)) {

        $mh = curl_multi_init();
        $handles = array();

		$get_query = "SELECT * FROM waliamlr_post.happiness_users ORDER BY id DESC LIMIT 1000";
	    $get_conn = new PDO('mysql:host=' . $host .';dbname=' . $dbname, $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
	    $statement = $get_conn->prepare($get_query);
	    $statement->execute();
	    $num_rows = $statement->rowCount();
	    $total_rows = $num_rows;

	    while ($num_rows > 0) {

	        while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
		            extract($row);
					$value = "\x20 يا " . $first_name . "\x20" . $second_name . "\x20" . $body;
		            $data = array(
							'to' => $registration_id,
							'notification' => array(
								'title' => $title,
								'body' => $value,
								'click_action' => 'com.walid.maktbti.happiness.HappinessContentActivity',
							),
						);

					$headers = array(
                		'Authorization: key=AAAA_AwdPtE:APA91bHCJahptGad2fsafnJ1KlS114PjIImWuhWddPYCE_mjOuOgm7NfsfqlglhAq8LgU_aL4DfsafbniM4UdcVp34n09rm6MH65255aBkzxAHWVMQQy7ZHvJLSNqTfIf_edXjFlNx6wBDRoNlbUqsJox',
                		'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($data));
                	curl_multi_add_handle($mh, $ch);
                	$handles[] = $ch;
		        }

		        $running = null;
		        do {
                    curl_multi_exec($mh, $running);
                } while ($running > 0);

                $statement = null;

                $next_query = "SELECT * FROM waliamlr_post.happiness_users ORDER BY id DESC LIMIT 1000 OFFSET " . $total_rows;
                $statement = $get_conn->prepare($next_query);
	            $statement->execute();
	            $num_rows = null;
	            $num_rows = $statement->rowCount();
	            $total_rows += $num_rows;
	    }

	    curl_multi_close($mh);
        $message = 'Notifications were sent successfully';
        $messageClass = 'alert-success';

    } else {
        $message = 'Fill all fields';
        $messageClass = 'alert-danger';
    }
}

?>

<!DOCTYPE html>
<html>

<head>
    <title>Send Happiness Notification for registred users</title>
    <link rel="stylesheet" href="../css/bootstrap.min.css">
</head>

<body>




    <div class="container">
        <div class="jumbotron">
            <?php if ($message != '') : ?>
                <div class="alert <?php echo $messageClass; ?>">
                    <?php echo $message; ?>
                </div>
            <?php endif; ?>
            <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
                <div class="form-group">
                    <label>Title</label>
                    <input type="text" name="title" class="form-control" value="شوف رسالتك في البرطمان 💙" >
                </div>
                <div class="form-group">
                    <label>Notification Body</label>
                    <input type="text" name="body" class="form-control" value="">
                </div>
           

                <div class="d-grid gap-2 mt-2 mb-2">
  <button class="btn btn-outline-primary" type="submit" name="submit">Send</button>

</div>
            </form>
        </div>
    </div>



</body>

</html>
Editor is loading...