Android Question notification resolved but

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
finally i can send notification from android to any device install my app
but i tried that with php function i found in discussion i put it in my server
the function exected successfully but i didn't get any notify
the script i found is:

<?php
$databasehost = "xxxx";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
$devicepassword="xxxx";
$serverpassword="xxxxxx";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());

function sendGCM($UserID, $message) {
$query = "SELECT id From c2dm Where name='testapi'";
$res=mysql_query($query);
$row = mysql_fetch_object($res);

// Replace with real BROWSER API key from Google APIs
$apiKey = "AIzaSyBfLv6oqlvlY6e2Na4nnDs_W3rDm2s71zU"; // Browser Key

// Replace with real client registration IDs
$registrationIDs = array( $row->id );

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "data" => $message ),
);

$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_SSL_VERIFYPEER, false);
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 "success"
}
sendGCM("testapi","okkkk");
?>

?>
and attached image tells some thing

any ideas?
 

Attachments

  • final.jpg
    final.jpg
    106.8 KB · Views: 249
Last edited:

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
i think it works it gives
{"multicast_id":7686131109786502266,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1493675611051120%22199a2af9fd7ecd"}]}success
but i did get any notification to my app
how to tell php sending page to send notification to certain app
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
hi Mr Erel
i have host already and i put the previous script on it as php page on the hosted server ,
but when i execute and print the result on the page it gives
{"multicast_id":7686131109786502266,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1493675611051120%22199a2af9fd7ecd"}]}success
which mean it works but i did not get any notification for the app, that is my problem.

NOTE:
when i use B4J it works and got notifications the following is the B4J code
B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private const API_KEY As String = "AIzaxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
End Sub

Sub AppStart (Args() As String)
    SendMessage("general", "note", "okkk")
    StartMessageLoop
End Sub

Private Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    m.Put("data": data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
    Job.GetRequest.SetContentType("application/json")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub
Sub JobDone(job As HttpJob)
    Log(job)
    If job.Success Then
        Log(job.GetString)
    End If
    job.Release
    StopMessageLoop '<-- non ui app only
End Sub

but i need that executed with php .
 
Last edited:
Upvote 0
Top