FCM notifications not received

Viewed 155

I have a web (PHP) application which enables users to select a collection of recipients and send them a message, to be read in our mobile app. To inform recipients of the 'receipt' of this message, I need to send a notification to their phones via FCM.

The data packets sent to FCM:

$packets[$device_token][$message_id] = [
    'notification' => [
        'title' => $msg['title'],
        'body' => $msg['content'],
        'sound' => 'default',
        'badge' => '1'
    ],
    'priority' => 'high',
    'data' => [
        // some data here
    ]
];

The sending of the data to FCM:

    $server_key = 'big_long_key';
    $fcm_url = 'https://fcm.googleapis.com/fcm/send';

    $headers = [];
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key='. $server_key;

    foreach($packets as $device_token => $messages)
    {
        foreach($messages as $msg_id => $messages_to_send)
        {
            $messages_to_send['to'] = $device_token;
            $msg_json = json_encode($messages_to_send);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $fcm_url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $msg_json);
            curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $curl_response = curl_exec($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

            $curl_error = null;
            if( ($httpCode != 200) || (curl_errno($ch)) )
                $curl_error = ($httpCode != 200) ? 'Firebase response not received (200)' : 'Firebase cURL error: ' . curl_strerror(curl_errno($ch));

            curl_close($ch);
            $curl_response = json_decode($curl_response, true);

            // response handling 
        }
    }
}

A substantial (say, 30%-ish) number of our recipients are reporting that they are not receiving any alerts on their phones (iOS and Android). This is despite us recieving a message ID from FCM for those users (which I presume indicates a successful sending of the notification).

What checks should I perform to ensure that a recipient phone should be displaying/playing an alert, and what changes must I make to ensure that the alert arrives and is handled properly?

1 Answers

I would check, if the device_token is still valid or not. If the token has changed, the desired recipient will not get the notification.

Related