2

I have created a mobile application with Push Notification. The App grabs the Token from Google FCM stores it into a database. Then we use a dashboard to send a notification to all registered devices.

There has been an issue with our notifications. Although it shows successful on FCM Response, the message does not go to devices.

FCM Response: {"multicast_id":8418406699445470273,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1531307530435857%b446114df9fd7ecd"}]}

PHP Code:

    $url = 'https://fcm.googleapis.com/fcm/send';
    // Set GCM post variables (device IDs and push payload)     
    $post = array(
                    'registration_ids'  => $ids,
                    'data'              => $data, 
                    'priority'              => 'high',    
                    'notification' => $data,               
                    );

    // Set CURL request headers (authentication and type)       
    $headers = array( 
                        'Authorization: key=' . $apiKey,
                        'Content-Type: application/json'
                    );

    // Initialize curl handle       
    $ch = curl_init();

    // Set URL to GCM endpoint      
    curl_setopt( $ch, CURLOPT_URL, $url );

    // Set request method to POST       
    curl_setopt( $ch, CURLOPT_POST, true );

    // Set our custom headers       
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );

    // Get the response back as string instead of printing it       
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

    // Set JSON post data
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );

    // Actually send the push   
    $result = curl_exec( $ch );

    // Error handling
    if ( curl_errno( $ch ) )
    {
        echo 'GCM error: ' . curl_error( $ch );
    }

    // Close curl handle
    curl_close( $ch );

    // Debug GCM response       
    echo $result;

I have sent the same message through Google Cloud Message Console and it did send the notification to my device.

Has anyone had this problem before?

Thank you in advance.

Aslan Kayardi
  • 423
  • 1
  • 4
  • 12

1 Answers1

0

This Code is perfect working i have notification get, i have already use this code

 <?php
    $t = $_POST["tokan"];
    $msg = $_POST["msg"];
    function sendMessage($data, $t)
    {
        //FCM api URL
        $url = 'https://fcm.googleapis.com/fcm/send';

        if (!defined('KEY_VALUE'))
            define('KEY_VALUE', 'webapikey');

        $fields         = array();
        $fields['data'] = $data;
        $fields['to']   = $t;

        //header with content_type api key
        $headers = array(
            'Content-Type:application/json',
            'Authorization:key=' . KEY_VALUE
        );

        $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);
        return $result;
    }
    $data = array(

        'post_msg' => $msg,
        'post_title' => "help",
    );

    sendMessage($data, $t);
    echo "success";

    ?>
amar shah
  • 71
  • 1
  • 10
  • Result with your code: {"multicast_id":4641890741778347559,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1531310267485098%b446114df9fd7ecd"}]} – Aslan Kayardi Jul 11 '18 at 11:59
  • I do get this result with my code too. However, I do not get it to my device. I enter same token ID to google console and it does send the notification to my device. – Aslan Kayardi Jul 11 '18 at 11:59
  • You have add manifest file permission – amar shah Jul 11 '18 at 12:47
  • Amar: This code is already added to the Android app. The device receives the notification that is sent via Google Firebase Cloud Messaging Console. – Aslan Kayardi Jul 13 '18 at 13:09