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.
Related
We are creating overpayments via the PHP Xero API.
In some cases, the overpayments are being duplicated.
Here is the code we are using:
<?php
$endpoint = 'https://api.xero.com/api.xro/2.0/BankTransactions';
$headers = array(
"Content-Type: application/json",
"Xero-tenant-id: " . $xero_access['tenant_id'],
"Authorization: Bearer " . $xero_access['token'],
);
$postFields = array(
"Type" => "RECEIVE-OVERPAYMENT",
'Contact' => ['ContactID' => $contactID],
'BankAccount' => ['accountID' => $xero_settings['account_id']],
'LineAmountTypes' => 'NoTax',
'LineItems' => [0 => [
'Description' => 'Customer Credit',
'LineAmount' => $price
]]
);
try {
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_URL, $endpoint);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
#curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
#curl_setopt($ch, CURLOPT_POST, 1);
#curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields, JSON_PRETTY_PRINT));
$response = #curl_exec($ch);
$status_code = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
error_log($status_code);
error_log($response);
} finally {
#curl_close($ch);
}
?>
I am not quite sure why the transactions are being duplicated or even if it is a network issue, or something in the code above.
Is there a way to make these API calls unique and make sure a request is not sent twice?
Thanks
Could you raise a case with Xero Support and include:
Your client id, the name of the connect tenant, the date and time of a call with duplication (and your timezone), the details of the overpayment eg the contact name, date and amount
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());
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\"}]}",
I am sending push notification from web using firebase, but I am not able to send push notification, while I am getting only name. How can I send push notification from web using firebase help me.
This is my php code.
<?php
$DEFAULT_URL = 'https://pushnotificatioexample.firebaseio.com/.json';
$mytoken = $_POST['token'];
$mymessage = $_POST['message'];
echo $mytoken;
echo '<br>'.'</br>';
echo $mymessage;
$registrationIds = array($mytoken);
$message = array
(
'message' => 'My awesome message',
'title' => 'My awesome title',
'subtitle' => 'My awesome subtitle',
'tickerText' => 'My awesome Ticker text',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array(
'registration_ids' => $registrationIds,
'data' => $message
);
$headers = array(
'Authorization:key = AIzaSyC9M4KFwqdy3vjcIBx9TNO8M7IysADbY_s',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $DEFAULT_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);
echo $result;
?>
Its look like you are sending a cloud message from your service side script. According to Firebase (FCM) official documentation, you don't have to use the url from Realtime database and all. So you have to change your url
From
$DEFAULT_URL = 'https://pushnotificatioexample.firebaseio.com/.json';
TO
$DEFAULT_URL = 'https://fcm.googleapis.com/fcm/send';
See the Firebase Cloud Messaging HTTP Protocol and here for more details
My code is here
Please see carefully
IOs gcm not working in background only working in foreground php script
$data = array('message' =>$msg,
'contentTitle' => $msg,
"tickerText" => "some ticker text",
"contentTitle" => "some content title",
"contentText" => "some content description text",
"sound"=> "default",
"vibrate" => "false",
"otherCustomData" => "some value"
);
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array("message" => $data),
'content_available' => true
);
//have to change
$headers = array(
'Authorization: key=IDSSSSSSS',
'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);
// 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);
// print_r($result);
if ($result === FALSE)
{
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
My Ios developer said to me its only working in foreground not working in background.This is properly working in android
Please help fast
Thanks in advance
You need to put notification parameter into your message. This way, GCM will deliver your message over APNS. Or, use content_available.
https://developers.google.com/cloud-messaging/http-server-ref#notification-payload-support