Hey I looked into Notifications for you.
The script to send push notifications is given in your link.
I would do it like so:
PHP
<?php
// beware this example is not 100% secure
// #1 with sql to track the stuff #2 without just send it back
// include("dbconnect.php"); // #1
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// get the client information
$token=htmlspecialchars($_POST['token'],$con);
$platform=htmlspecialchars($_POST['platform'],$con);
$id=htmlspecialchars($_POST['id'],$con);
$ip=GetClientIP();
$date=date("Y-m-d H:i:s");
// set the messages and call the function
$title="Notification Test";
$message="It Works! Platform :".$platform." ID: ".$id." IP: ".$ip." Date: ".$date;
echo "Title: ".$title." Message: ".$message; // test
SendPushNotificationAndroid($token,$message,$title); // #2
/* #1
// better use mysql_real_escape_string on the variables before give it to the database
$con=DBConnect();
$sql="SELECT ID FROM `pushnotifications` WHERE token='".$token."'";
$result=mysql_query($sql);
if(mysql_num_rows($result)==1)
{
SendPushNotificationAndroid($token,$message,$title);
}
else
{
$sql="INSERT INTO `pushnotifications`(`token`,`platform`,`id`,`ip`,`date`) VALUES ('".$token."','".$platform."','".$id."','".$ip."','".$date."')";
$result=mysql_query($sql) or die('Error: '.mysql_error());
echo $result;
}
mysql_close($con);
*/
}
else
{
echo -1;
}
function GetClientIP()
{
if (!isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
return $_SERVER['REMOTE_ADDR'];
}
else
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
}
function SendPushNotificationAndroid( $deviceToken, $message, $title )
{
// Replace with real SERVER API key from Google APIs
$apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => array( $deviceToken ),
'data' => array( "message" => $message, "title" => $title ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
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, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
}
?>
AGK
// Project: Push Notifications
// Created: 2015-03-30 by Janbo
// set window properties
SetWindowTitle( "Push Notifications" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
UserID = 01234 // some user ID that your server can use to recognise whos key this is
SetPushNotificationKeys( "1234567890", "" )
Result=PushNotificationSetup()
if Result=1
Token$=GetPushNotificationToken()
while token$=""
token$=GetPushNotificationToken()
endwhile
message("Token: "+Token$)
ConnectionID=RequestToken(Token$,GetDeviceBaseName(),UserID)
elseif Result=0
message("This platform does not support push notifications")
endif
do
Result$=ResponseToken(ConnectionID)
if Result$<>""
message("Response Result: "+Result$)
endif
Sync()
loop
function ResponseToken(ConnectionID)
Result=getHTTPResponseReady(ConnectionID)
if Result=-1
//message("Error")
elseif Result=1
//message(GetHTTPResponse(ConnectionID))
Result$=GetHTTPResponse(ConnectionID)
endif
if Result<>0
closeHTTPConnection(ConnectionID)
deleteHTTPConnection(ConnectionID)
endif
endfunction Result$
function RequestToken(Token$,Platform$,UserID)
Parameter$="token="+Token$+"&platform="+Platform$+"&id="+str(UserID)
ConnectionID=createHTTPConnection()
setHTTPHost(ConnectionID,"www.example.com",0)
SendHTTPRequestASync(ConnectionID,"sendtoken.php",Parameter$) // we do not need the server to return anything so we can ignore getting the response
endfunction ConnectionID
I recieve the output of the first echo of the script, but not the notification.
I wonder if we need to call GetNotification or something in the agk code?
What is it good for... can't we just do it all with php scripts?