PHP Firebase notification shows in tray but not popping up - php

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.

Related

Why my FCM notification are delivering silently and do not pop up?

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

how to send a notification for a theme from my server to firebase

I am sending a message from my server to firebase for a specific topic, I get back an id, but it does not get a notification to the app when I do it from the console to that topic the notification arrives.
How should I do it for a specific topic but that is a notification?
PHP:
<i>
<?php
function send_notification ($message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => "/topics/news",
'data' => $message
);
$headers = array(
'Authorization: key=AAAApDF81wE:APA91bG7g.....',
'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;
}
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($message);
echo $message_status;
?>
</i>
I just changed this
$fields = array(
'to' => "/topics/news",
'notification' => $message
);
i Was not doing proper handling in the app, i should handle the logic and code implementation on how to handle incoming messages within the onMessageReceived method.

Sending notifications to specific device using PHP?

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.

Sending different push notification to each device

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.

send other things alongside the data with GCM using php

I am sending android push notifications using php (curl).
Below is my class that sends the notification.
public function send_notification($registatoin_ids, $message)
{
include_once 'config.php';
$url = 'https://android.googleapis.com/gcm/send';
//issue here
$fields = array('registration_ids' => $registatoin_ids, 'data' => $message);
$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_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE)
{
die('Curl failed: ' . curl_error($ch));
}
Like this I am able to send the message to the desired recipient and I am succeeding.
However, I can only send messages like this.
Suppose I want to send additional information, such as a link of a site, or other data that won't be attached to the message.
Where would I add that ?
Are 'registration_ids' and 'data' unique values ?
androids documentation does not include php example.
The variable data can be an array. So your send the message like this.
$data = array("url" => "wwww.google.com", "message" => "Hello, Google");

Categories