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
Related
I am following this Firebase Cloud Messaging (FCM) Server Side Implementation. . but got an error of invalidregistrationid.
Did googling but found nothing.
I also found some related problem which already answered on StackOverflow. but got no solutions.
the following link I visited for the solution.
a link.
B link.
class abc{
function send_android_notification($registration_ids="2fa86bbc8bc4aee204255a0d84461c000a26442312d0a9b1df1664d461fd3b7d") {
$message = array('title' => 'CodeCastra', 'body' => "hi huye" ,'sound'=>'Default','image'=>'Notification Image' );
define("GOOGLE_API_KEY", "AIzaSyDDvpPcWfOTVkkvA-yQg4yTYzIuh0MNIRw"); //legacy server key
$fields = array(
'registration_ids' => (array)$registration_ids,
'notification' => $message, //note: body & title fileds must be specified for the message or your only get just the vibration but the notification
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY, // FIREBASE_API_KEY_FOR_ANDROID_NOTIFICATION
'Content-Type: application/json'
);
//Open connection
$ch = curl_init();
//Set the url, number of POST vars, POST data
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
//echo json_encode($fields);
//print_r($headers);
//exit;
//Execute post
$result = curl_exec($ch);
if ($result === false) {
die('Curl failed:' . curl_errno($ch));
}
// Close connection
curl_close($ch);
return $result;
}
}
print_r((new abc)->send_android_notification());
die;
{"multicast_id":4707836736661373968,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
Any help will be appreciated.
thanks
I am implementing your code.
Use firebase registration token it generated in device (ios/android)
instead of the Device token .
'registration_ids' => (array)$token,
Hope it's helpful
I am using firebase notification for communicate with android applications with Laravel / PHP server,
My code for PHP :
$url = "https://fcm.googleapis.com/fcm/send";
$fields = array(
'registration_ids' => $fcm_id, /* array for fcm ids */
'data' => $data
);
$headers = array(
'Authorization: key=/*My 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_POSTFIELDS, json_encode($fields));
curl_exec($ch);
curl_close($ch);
This code is working fine but takes too much time when there is large numbers of registration ids, to overcome this problem i have to use background process for curl, so that i use timeout_ms in curl
like :
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10);
but when i use this line, notification not sent to android.
my question is why execution of fcm stops when i using this line, or is there any other way to do background process for FCM Notification?
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.