I have a while loop that checks my database for notifications with a status of 1. It then sends a notification to OneSignal.com's API so the user will get the notification.
The problem is, I'm trying to run the script so that it processes all records with a status of 1 as it makes the while loop, but once I include the function for OneSignal the while loop stops after processing only one record.
I cant figure it out, I tried moving things around, and when I put the send message function outside the while loop, the php variables for the user ID don't get passed to it?
What should I do?
<?php
require('connection.inc.php');
$notification = mysqli_query($con, "SELECT *, t1.id AS NotifID FROM notification AS t1 LEFT JOIN user AS t2 ON t1.action_user_id = t2.id WHERE status = '1' ORDER BY time_updated DESC") or die(mysql_error());
//GET NOTIFCATION RECORD
while ($row = mysqli_fetch_assoc($notification)) {
$notificationID = $row['NotifID'];
$type = $row['notification_type'];
$action_user_id = $row['action_user_id'];
$action_user_name = $row['name'];
$action_user_profile = empty($row['profile_image']) ? '/instachurch/images/profile/profile.png' : '/instachurch/' . $row['profile_image'];
$time = $row['time_updated'];
$post_id = $row['post_id'];
$notify_user_id = $row['notify_user_id'];
// GET NOTIFY USER INFO
$GetUserName = mysqli_query($con, "SELECT * FROM user WHERE id = $notify_user_id ") or die(mysql_error());
while ($row = mysqli_fetch_assoc($GetUserName)) {
$username = $row['name'];
$userMob = $row['mob'];
$userHash = $row['hash'];
$OneSignalPushID = $row['id'];
if ($type == "post_liked")
$message = $action_user_name . " liked your post.";
else if ($type == "post_comment")
$message = $action_user_name . " commented on your post.";
else if ($type == "post_comment_reply")
$message = $action_user_name . " replied to your comment.";
else if ($type == "post_answer")
$message = $action_user_name . " answered to your question.";
else if ($type == "follow_user")
$message = $action_user_name . " started following you.";
else if ($type == "discussion_topic_comment")
$message = $action_user_name . " commented on your forum topic.";
else if ($type == "timeline_mention")
$message = $action_user_name . " mentioned you in a comment.";
else if ($type == "school_lesson_mention" || $type == "school_discussion_mention")
$message = $action_user_name . " mentioned you in a comment on discussion.";
if ($type == "post_comment" || $type == "post_liked" || $type == "post_comment_reply" || $type == "post_answer" || $type == "timeline_mention")
$link = "http://www.gypsychristiannetwork.com/instachurch/post.php?post_id=" . $post_id . '&hash=' . $userHash;
else if ($type == "follow_user")
$link = "http://www.gypsychristiannetwork.com/instachurch/users.php?id=" . $action_user_id . '&hash=' . $userHash;
else if ($type == "discussion_topic_comment")
$link = "http://www.gypsychristiannetwork.com/tgcm/school/account.php?q=24&topic=" . $post_id . '&hash=' . $userHash;
else if ($type == "school_lesson_mention")
$link = "http://www.gypsychristiannetwork.com/tgcm/school/account.php?q=11&qid=" . $post_id . '&hash=' . $userHash;
else if ($type == "school_discussion_mention")
$link = "http://www.gypsychristiannetwork.com/tgcm/school/account.php?q=24&topic=" . $post_id . '&hash=' . $userHash;
}
$q3 = mysqli_query($con, "UPDATE `notification` SET `status` = '2' WHERE `id` = '$notificationID'");
echo $notificationID . ':' . $message;
// echo $link;
$userPushID = $OneSignalPushID;
// echo $userPushID.'<BR><P>';
$heading = 'Check your Notifications';
$content = array(
"en" => $message
);
$heading = array(
"en" => $heading
);
$fields = array(
'app_id' => "APPID",
'include_external_user_ids' => array(
"$userPushID"
),
'channel_for_external_user_ids' => 'push',
'data' => array(
"foo" => "bar"
),
'contents' => $content,
'headings' => $heading,
'url' => $link
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic APPSECRET'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode($return);
print("\n\nJSON received:\n");
print($return);
print("\n");
}
?>