Firebase Cloud Messaging - PHP REST API not working on iOS - php

I'm using below PHP code to send push notification to Android and iOS devices using Firebase REST API. Push notifications are coming fine in Android device. But it is not coming in iOS device.
Meanwhile, when I send the notification from the Firebase console, both devices are receiving it. Did I miss anything in my API implementation?
$data = array("to" => "/topics/news",
'priority' => '10',
'notification' => array('body' => $msg));
$headers = array
(
'Authorization: key='.$apiKey,
'Content-Type: application/json'
);
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ouput = curl_exec($ch);
if ($ouput === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
$response = curl_getinfo($ch);
} catch(Exception $e) {}

You should set your priority to high instead of 10.
When using GCM/FCM, the only valid values for priority are normal and high which is equal to 5 and 10 for iOS APNs, as was mentioned here:
Sets the priority of the message. Valid values are "normal" and "high." On iOS, these correspond to APNs priorities 5 and 10.

Related

Firebase notification curl with timeout

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?

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

Sending Push Notifications to IOS Devices with php

I'm working on and IOS App that will send push notifications with FCM, i've configured everything right on the FCM Console and tested sending a message from the console to the device and it worked. Now i'm using the below script and the message is not able to push to the phone.
When i send i get this message
{"multicast_id":7893466200486146313,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1506525095628728%b54fead7f9fd7ecd"}]}
Below is my script
// Replace with the real server API key from Google APIs
$apiKey = "-------";
// Replace with the real client registration IDs
$registrationIDs = array('00000');
// Message to be sent
$message = "New Message from GWCL";
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( 'message' => $message,'title' => "New Message",'notId'=>''.time(), 'complain_id'=>'90','push'=>'reply','priority'=>'high'),
);
$headers = array(
'Authorization: key=' . $apiKey,
'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);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
I followed a tutorial which told me to add 'priority'=>'high', i did but still didn't work
Recently i worked on Push notification on both Firebase and APN so i can say as per your FCM response Push notification sent successfully from PHP end
so I guess you are handling some think wrong in your app side
As i am PHP developer i am not much aware of ios development.so can share some links it might help you to debug
firebase iOS not receive push notifications
Firebase when receive Push Notification did not receive the popup
iOS not receiving Firebase Push Notification sent through the API
Not receiving Push Notifications Using firebase and Objective C
Not receiving Firebase push notifications when app is open?

how to send push notification from onesignal the device id's are stored in database - cordova app

I'm using push notification from onesignal platform in my cordova app. I have implemented it to send push notification from oneSignal dashboard but I want to send notification from my own search, I have stored device ids when user install the app but don't know how to send notifications to devices that's ids are stored in my database
Any help regarding this will be appreciated . Thanks if anyone has issue to understand my question can comment i will explain with my best capacity.
To handle this case, I also use one signal, but at the launch of my app, I send my id to my backend to be sure it will be registered to my db when I will need it. Just with a classic http post request.
There is no magic trick to achieve that, you don't have lot of choices here except save it to your db by a way or another.
You can use this function which is provided by OneSignal
function sendMessage(){
$content = array(
"en" => 'YOUR_CUSTOM_MESSAGE'
);
$fields = array(
'app_id' => "YOUR_APP_ID",
'include_player_ids' => array("YOUR_TARGET_ID"),
'contents' => $content
);
$fields = json_encode($fields);
print("\nJSON sent:\n");
print($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$response = sendMessage();
$return["allresponses"] = $response;
$return = json_encode( $return);
print("\n\nJSON received:\n");
print($return);
print("\n");

Calling Firebase Cloud Messaging from server does not work

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?

Categories