从php发送时,FCM消息未传送到设备

时间:2018-01-20 05:45:41

标签: php

我正在尝试在我的应用中实施FCM。现在,当我从firebase应用程序控制台发送消息时,我能够收到消息。但是,当我尝试从服务器上的PHP代码发送消息时,消息不会传递到手机。但是,每当完成对FCM的调用并且没有任何错误迹象时,我都会使用数字获取message_id。任何帮助将不胜感激。

PHP代码:

public function send_fcm() {
    $API_ACCESS_KEY = '*****************************';
    $msg = array ('message'   => 'here is a message. message',
                    'title'     => 'This is a title. title',
                    'subtitle'  => 'This is a subtitle. subtitle',
                    'tickerText'    => 'Ticker text here...',
                    'vibrate'   => 1,
                    'sound'     => 1
                    );

    $fields = array('to' => '/topics/mytopic', 
                    'priority' => 'high', 
                    '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));
    $pushResult = curl_exec($ch);
    curl_close($ch);

}

2 个答案:

答案 0 :(得分:1)

您应该向notification提供fields数组,我使用此方法使用PHP发送通知

public static function toDevice($token , $message)
{
    $url = 'https://fcm.googleapis.com/fcm/send';

    $fields = array (
        'to' => $token,
        'notification' => [
            "body" => $message ,
            ...
        ] ,
        "data" => [
            ...
        ]
    );
    $fields = json_encode ( $fields );

    $headers = array (
        'Authorization: key=' . "XXXXXXXXXXXXXXXXXXX",
        'Content-Type: application/json'
    );

    $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_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    curl_close ( $ch );

    return $result;

}

答案 1 :(得分:1)

您的更正代码此.. ..是经过测试的代码。
或点击此链接:https://inducesmile.com/android/android-firebase-cloud-messaging-push-notification-with-server-admin-in-php-and-mysql-database/

public function send_fcm($token) {

    $url = "https://fcm.googleapis.com/fcm/send";
    $msg = array('message' => 'here is a message. message',
        'title' => 'This is a title. title',
        'subtitle' => 'This is a subtitle. subtitle',
        'tickerText' => 'Ticker text here...',
        'vibrate' => 1,
        'sound' => 1
    );

    $fields = array('to' => $token,
        'priority' => 'high',
        'data' => array('message' => $msg)
         );

    $headers = array(
        'Authorization:key=*****************************',
        'Content-Type:application/json'
    );

    $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));
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('CURL FAILED ' . curl_error($ch));
    }

    $info = curl_getinfo($ch);

    curl_close($ch);
    return array('result' => $result, 'status' => $info['http_code']);
}