Ad

Push Notifications Are Not Working After Migrating To Fcm From Gcm

I have migrated my code from apns to fcm according to these guidlines https://developers.google.com/cloud-messaging/ios/ios-migrate-fcm. After updating the code, push notifications are not working.

I have created certificates .p12 and .pem files and test both files with my previous apns related code. Certificates are just fine and push notifications are being send to client devices when using old code. but when i test these certificates with my new fcm related code, no push notification appears. I have also changed my server endpoints. I tried sending notifications from firebase console, it is also working fine.

Do I need to change something on my server?

Ad

Answer

Finally solved the issue. I need to change my server side code. With FCM, .pem certificate is not required anymore on server side (php script). What i did is,

$url = "https://fcm.googleapis.com/fcm/send";
$token = "";
$serverKey = '';
$title = "Title";
$body = "Body of the message";
$notification = array('title' =>$title , 'text' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST,

"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);

source : https://www.cumulations.com/blogs/87/how-to-send-push-notifications-in-php-to-ios-devices-using-fcm

Ad
source: stackoverflow.com
Ad