首页 文章

GOOGLE FCM( Cloud 消息传递)通知未显示在移动设备上

提问于
浏览
1

我用Push Notification创建了一个移动应用程序 . 该应用程序从Google FCM抓取令牌将其存储到数据库中 . 然后我们使用仪表板向所有已注册的设备发送通知 .

我们的通知存在问题 . 虽然它在FCM响应中显示成功,但该消息不会发送到设备 .

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

PHP代码:

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

我已通过Google Cloud 端消息控制台发送了相同的消息,并确实已将通知发送到我的设备 .

以前有人有这个问题吗?

先感谢您 .

1 回答

  • 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";
    
        ?>
    

相关问题