Push notification not sending from PHP api - php

I have written an API in PHP which will receive Firebase token and will send push notification to Android device.
I have tested Android side code and it receiving sample notification from Firebase console using token. But when I am trying to send from PHP api then it's not sending notification.
Here is my php API.
// send firebase push notifcaiton
$url = 'https://fcm.googleapis.com/fcm/send';
$api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
$fields = array (
'registration_ids' => array (
$pnsToken
),
'data' => array (
"message" => $message,
"guestId" => $guestId,
"guestEntryId" => $guestEntryId,
"guestName" => $guestName,
"guestAddress" => $guestAddress,
"reason" => $reason
)
);
//header includes Content type and api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$api_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 don't know why it's not sending push notification from PHP api.
Edit
Here is fcm error which I am receiving :
"fcmResult": "{\"multicast_id\":7084003691931660410,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"NotRegistered\"}]}",

Related

Separate Notifications do not appear on a same device subscribed to a topic

when i push multiple notifications in a loop via api subscribed to topic using FCM, the previous notification replaced by new notification on my android device. Is there a way to push multiple notifications separately. I am using PHP Curl request.
foreach ($result as $item)
{
$message = array
(
'body' => "Details:".$item['tagid'],
'title' => "Tag:".$item['event'],
'key1' => "NotificationActivity"
);
$fields = array(
'to' => '/topics/all',
'data' => array
(
'message' => $message
),
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
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_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
}
There was problem on my Firebase service. Not identifying notification on id basis.
Problem was, I was passing 0 in id field.
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
After setting unique id of each notification, it's working:
notificationManager.notify(id /* ID of notification */, notificationBuilder.build());

Receive Push Notification Twice firebase PHP Script

I'm new to Firebase Send Push Notification. I'm working on Send Push notification To multiple Users Using PHP Script.
PHP SCRIPT
function sendPushNotification($fb_key_array, $title) {
$finalPostArray = array('registration_ids' => $fb_key_array,
'notification' => array(
'title' => $title,
'sound' => 'default',
'badge' => '1'
),
'priority' => 'high',
);
$server_key = env("SERVER_KEY");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($finalPostArray)); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key=' . $server_key));
$server_output = curl_exec($ch);
print_r($server_output);
return $server_output;
curl_close($ch);
}
I'm send notification using Queue Jobs on Particular Time.
I have a problem when I send Three notification at same time than Notification receive Twice.
Response From This Curl
App\Jobs\PushNotification
<pre>{"multicast_id":4668247729883149905,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1616386021446151%e609af1cf9fd7ecd"}]}[2021-03-22 04:07:01][261] Processed: App\Jobs\PushNotification
[2021-03-22 04:07:01][262] Processing: App\Jobs\PushNotification
<pre>{"multicast_id":4539206321688711164,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1616386022124068%e609af1cf9fd7ecd"},{"message_id":"0:1616386022123922%e609af1cf9fd7ecd"}]}[2021-03-22 04:07:01][262] Processed: App\Jobs\PushNotification
[2021-03-22 04:07:02][263] Processing: App\Jobs\PushNotification
<pre>{"multicast_id":1591082090876608595,"success":3,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1616386023195991%e609af1cf9fd7ecd"},{"message_id":"0:1616386023196003%e609af1cf9fd7ecd"},{"message_id":"0:1616386023195724%e609af1cf9fd7ecd"}]}[2021-03-22 04:07:02][263] Processed: App\Jobs\PushNotification
And Receive 6 Notification .but I send Only Three Notification. Where is my Fault ?
Help Thank You
I am not so sure about that there is nothing wrong with your func but you can try this it's the same but little different if not work so that mean you have a loop somewhere in your code ..
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
'registration_ids' => $target,
// fix new json format
'notification' => array(
'body' => 'this is firebase test mesging',
'title' => 'hello from firebase',
'badge' => 1,
'sound' => 'default',
'priority' => 'high',
)
);
$fields = json_encode($fields);
$server_key = 'ur_server_kay';
// inherited 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, $fields);
$result = curl_exec($ch);
var_dump($result);
if ($result === FALSE) {
die('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);

Firebase Cloud Messaging - PHP Rest API not working for iOS

I am using the PHP code given below to send notification with custom payload through Firebase Cloud Messaging. It is working for Android but not for the iOS.
However, I am able to receive notification sent from firebase cloud messaging console. Kindly advise.
public function send_notification($registatoin_ids, $message, $title, $sound,
$purpose)
{
// Set POST variables
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => array(
"for" => $purpose,
"id" => strtotime("now"),
"message" => $message,
"title" => $title,
"sound" => $sound,
"vibrate" => 1,
"date" => date("D d, M Y h:i A"),
"priority"=>'high',
"content_available"=>false
),
'time_to_live' => 600,
);
$headers = array(
'Authorization: key=' . FCM_API_KEY,
'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);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
return $result;
}
Android and iOS handle Push Notifications differently. While Android will only wake from background when only the data tag is provided, iOS devices require the notification tag to deal with notifications received while the app is in the background.

Firebase Api Key is not working for iOS only

I want to send push notification using firebase in php. My code is working fine for android but not working for ios device.
And I am getting success:1 for both cases.
Here is my code
<?php
$path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';
$fields = array('registration_ids' => $token,
'data' => array('title'=>'Title',
'body'=>$message['message'],
'image'=>$imagepath,
'priority'=>'high'
)
);
$headers = array(
'Authorization:key=AIzaSyBiOCXbU7roG59_**********vWa4Xc' ,
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm);
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);
curl_close($ch);
return $result;
?>
And response is
stdClass Object
(
[multicast_id] => 8.5168949472892E+18
[success] => 1
[failure] => 0
[canonical_ids] => 0
[results] => Array
(
[0] => stdClass Object
(
[message_id] => 0:1493801065120413%2d69fd2bf9fd7ecd
)
)
)
Delete app from iphone and install again, After that you will get new firebase token try to send push again.
Debug this first from php side they are sending push on newer device token.

Withdraw FCM notification PHP

After sending a FCM notification using PHP a multicast id has been received.
is there any way withdrawing the sent notification using this multicast id?
For instance:
When whatsapp web is open on my pc, the whatsapp notification will be gone from my home screen on my phone after reading it on my pc.
$url = 'https://fcm.googleapis.com/fcm/send';
$notification= array(
'body' => $message,
'priority' => 10,
'sound' => "default"
);
$fields = array(
'to' => $targets,
'notification' => $notification,
'sound' => "default",
'priority' => 10
);
$headers = array (
'Authorization: key='.$gcmKey,
'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));
curl_exec($ch);
curl_close($ch);
This is how i 'send' the notification.
My app is being build using phonegap

Categories