I have an iOS app which is live on the app store. I am able to send push notifications to iOS devices which have the app installed, but only when I send them from the Fireba
this is what i used :
<?php
//Server keys is now the requirement not the legacy key since its not available
define( 'API_ACCESS_KEY', 'AAAA9....sQ:APA..HKb_Qfz.....q_k4NJonXd...9r-Fof....dJ_ylYLbR....NFz-uQRve.....0GoNISAA......SKx8G_4M...n' );
$messsage = array
(
'body' => 'msg',
'title' => 'title',
'key1' => 'val1'
);
$fields = array
(
'to' => "MdS.....Jg:APA91bEhZ........wkZ7rVmt6UmTc4T189M6ie39COvVmqsIA2DEijt_o9lHUGZbSFez......rPqNo4W_Ru............BEGEo6cuWOKu2vasQju",
'notification' => $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 );
echo $result;
?>
you can add keys and values like example and handle it in your program
There were two issues:
notification
section in the payload instead of data
In the end I used the PHP function shell_exec()
to do a cURL request over SSH instead. This isn't ideal but it got the job done.
Example code:
shell_exec('curl -X POST --header "Authorization: key=<key here>" --header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"'.$to.'\",\"priority\":\"high\",\"notification\":{\"body\": \"'.stripslashes($message).'\"}}"');
From the Firebase documentation you have the choice of using either data or notification in the message payload. But when you use data, you have the responsibility of handling the receipt of the notification yourself. In other words it will not be sent straight away to your app client. You handle it in the didReceiveRemoteNotification: for ios and onMessageReceived() in case of Android.
If however you use notification in the payload, firebase will send the message straight away to your client App.
That is why you will not receive the message(in case you used data) even if your curl request tells you it has succeeded in making the request.
Try to add { priority : high }
$fields = array
(
'to' => "cUxd-iTVVWo:APA*****kQTuqJ5RREKhhlJjm27NCuVfUn5APg3lBFqh-YWjgx*****iOpAQeLB14CzM2YTwIJo_75jzCmbFLKj0_zpKSHvAEbmJz*****BBezGJIng-N4H-cAD6ahY7mQNYZtEJLAIE",
'data' => $msg,
'priority' =>'high'
);