I just developed a firebase web app server. I am sending notifications and everything seems okay but in my android app no notifications are received. If I send a notification from firebase console it comes to my android app. However I want to send it from my web app.
Here is my php code for web app:
public function sendMessage($data,$target=null){
//FCM api URL
$url = 'https://fcm.googleapis.com/fcm/send';
//api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'S6GzaCUhIbS6GzaCAIzaSyJtkQLzl3FCUhIbUhIbS6Gza8A';
//$fields = array();
//$fields['data'] = $data;
if(is_array($target)){
$fields['registration_ids'] = $target;
}
$fields = array(
'notification' => array('title' => 'Working Good', 'body' => 'That is all we want'),
'data' => array('message' => "melaapa")
);
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
var_dump($result);
return $result;
}
The output of
var_dump($result)
is
string(3) "to "
What should the reponse be for a working firebase server or how can I understand that my firebase server is working fine?
Related
I'm trying to use a script i found on SO, it's a php script that pushes notifications using Firebase, in my app i'm trying to push notification to my Android device but i don't know how to identify my device's ID/Key, the script is the following:
$server_key=""; // get this from Firebase project settings->Cloud Messaging
$user_token=""; // Token generated from Android device after setting up firebase
$title="New Message";
$n_msg="The is a message";
$ndata = array('title'=>$title,'body'=>$n_msg);
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array();
$fields['data'] = $ndata;
$fields['to'] = $user_token;
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
I'm trying to identify the $user_token so i can use this script to serve my purpose.
I am trying to send push notification to my unity game on android using PHP and FCM.
notifications are getting delivered to device but silently and not in pop up.
i need to know why?
am I missing something?
I tried replacing Notification key with Data but they nothings works.
function send_notification ($tokens, $message,$title){
$url = 'https://fcm.googleapis.com/fcm/send';
$notification= array('title' => $title,'body' => $message);
$fields = array(
'registration_ids' => $tokens,
'notification' => $notification,
'priority' => 1
);
$headers = array(
'Authorization:key =xxxxxxxxxxxx',
'Content-Type: application/json'
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
I Wanted my notification to pop up.
currently it just gets in notification tray
I tried your code and it works as expected. I see popup only when screen is locked.
This is full list of android-specific features for FCM push https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidNotification
Take a look at this answer FCM for android: popup system notification when app is in background
Maybe you should include this functionality directly in your Unity app: https://firebase.google.com/docs/reference/unity/namespace/firebase/messaging
I am using php to send push notification using firebase to my Android app build using unity.
App is receiving notification and showing it in a tray, but not showing it as pop up notification like whats app or other apps do.
My php code is:
function send_notification ($tokens, $message){
$url = 'https://fcm.googleapis.com/fcm/send';
$notification= array('title' => 'Winner!!','body' => $message);
$data= array('custom_notification' => $custom_notification );
$fields = array(
'registration_ids' => $tokens,
'notification' => $notification,
);
$headers = array(
'Authorization:key = AAAA5C-Y_Ew:APA91bFD1g98n19DZD8FkaEYiME4tDsniePTKU1sGww1EAD7dtSKP6FEqlRGcTpB_MCD_7UM7ToWis1VnZO-dtDXMLFSZtaSIDAQ0fACbJ_SOFjWd93klckboahy3eZr93Z9M02LfP_s',
'Content-Type: application/json'
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
it also showing gray scaled icon instead of colored icon.
I want to show notification as pop up.
is there anyway to do so using FCM?
Instead of 'notification' => $notification use only data key while sending notification for android only.
iOS require only 'notification' => $notification however.
I'm trying send notifications to specific device using PHP. I created the project in google console and start GCM service. I can send notifications to all devices but I want send to specific device for example: 26093d6bbddgggg, 26093d6bbddzzzz
How can i do it ?
I'm trying this PHP script
<?php
define("GOOGLE_API_KEY", "AIzaSyCJiVkatisdQ44rEM353PFGbia29mBVscA");
define("GOOGLE_GCM_URL", "https://android.googleapis.com/gcm/send");
function send_gcm_notify($reg_id, $message) {
$fields = array(
'registration_ids' => array( $reg_id ),
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_URL);
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));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Problem occurred: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
}
$reg_id = "APA91bHuSGES.....nn5pWrrSz0dV63pg";
$msg = "Google Cloud Messaging working well";
send_gcm_notify($reg_id, $msg);
I dont know about PHP. but when you are getting device Ids at that search device Id for specific device and then send.
I have set up php script which will send push notification. Everything works fine and it sends same message to each device in array. But what bugs me is, what if I want to send different message do each device(depending on device preferences for instance)?
What would be the right approach here? My first thought is to call pushNotification function for every device separately instead of calling it once and sending array of devices to GCM?
This is the code I'm using, so please advise...
function sendPushNotificationToGCM($registatoin_ids, $message) {
//Google cloud messaging GCM-API url
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
// Google Cloud Messaging GCM API Key
define("GOOGLE_API_KEY", "my_api_key");
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return($result);
}
$devices = array();
while($results = $result->fetch_array()) {
$devices[] = $results['dev_reg_id'];
}
//return $devices;
$pushMessage = $message;
$message = array("m" => $pushMessage);
$pushStatus = sendPushNotificationToGCM($devices, $message);
You can't send different messages to different devices in the same HTTP request to GCM server. If each device requires a unique message, it would also require a unique call to your sendPushNotificationToGCM function. If some devices share the same message, you can send that message to them in the same function call.