Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Notification not received on device when sent via cURL/PHP

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 Firebase console.

When I try to send push notifications via a cURL request, the response from the server indicates that I was successful but the message isn't received on the device. I have tried this with both multicast and single recipient payloads.

I must be missing something more fundamental, but I can't see it.

Here is my PHP code:

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AI*****4LPGkx8xtDG2tBl*****7KWJfmp1szcA' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title'
);
$fields = array
(
    'to'            => "cUxd-iTVVWo:APA*****kQTuqJ5RREKhhlJjm27NCuVfUn5APg3lBFqh-YWjgx*****iOpAQeLB14CzM2YTwIJo_75jzCmbFLKj0_zpKSHvAEbmJz*****BBezGJIng-N4H-cAD6ahY7mQNYZtEJLAIE",
    'data'          => $msg
);

$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;

Here is the response I get when running this code:

{"multicast_id":5814921248239922706,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1476193002715692%a4ddee3cf9fd7ecd"}]}

like image 359
Robbie Lewis Avatar asked Oct 11 '16 14:10

Robbie Lewis


3 Answers

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.

like image 73
ultrasamad Avatar answered Oct 22 '22 10:10

ultrasamad


There were two issues:

  1. I needed to include a notification section in the payload instead of data
  2. Somehow the payload wasn't being formatted properly by PHP.

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).'\"}}"');
like image 40
Robbie Lewis Avatar answered Oct 22 '22 09:10

Robbie Lewis


Try to add { priority : high }

$fields = array
(
    'to'            => "cUxd-iTVVWo:APA*****kQTuqJ5RREKhhlJjm27NCuVfUn5APg3lBFqh-YWjgx*****iOpAQeLB14CzM2YTwIJo_75jzCmbFLKj0_zpKSHvAEbmJz*****BBezGJIng-N4H-cAD6ahY7mQNYZtEJLAIE",
    'data'          => $msg,
    'priority'      =>'high'
);
like image 22
Leo Valentim Avatar answered Oct 22 '22 09:10

Leo Valentim