1

firebase クラウド メッセージング システムを使用して、サーバーから Android デバイスにプッシュ通知を送信しようとしています。デバイスを正常に登録でき、デバイスのトークンも生成されました。以下のスクリプトを使用してデバイスに通知を送信する際に問題が発生しています

<?php

function send_notification($token1,$message)
{
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array('registration_ids'=>$token1,'data'=>$message);
    $header = array('Authorization:key = <my key here>','Content-Type: application/json');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    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('Curl failed'.curl_error($ch));
    }
    curl_close($ch);

    return $result;

}

require "init.php"; //my db details here

$sql = "select token from fbuser";

$result = mysqli_query($con,$sql);
$tokens = array();

if(mysqli_num_rows($result)>0)
{
    while($row = mysqli_fetch_assoc($result))
    {
        $tokens[] = $row["token"];   // i was able to successfully echo out token as well
    }
}

mysqli_close($con);

$message = array("message"=> "This is a test message"); 
$message_status = send_notification($tokens,$message);
echo $message_status; ***//Trouble here. it just prints out "To" whenever i hit the php script***

?>

スクリプトを実行するたびに、応答が返されます

To

他には何もありません..通知も配信されません。問題を把握できません。ご指導をお願いしております。

ありがとう

4

2 に答える 2

1

「registration_ids」の代わりに「to」を使用してみてください

郵便配達員の履歴に保存された本文データは次のとおりです[機能するかどうかは覚えていません]:

{ "data": {
"score": "5x1",
"time": "15:10" }, "to" : "token_id"}

このスクリプトを確認してください: トピックのメッセージングを行う場合

$url = "https://fcm.googleapis.com/fcm/send";
                $topic = "topic_name";
                $fields = array(
                    "to"                => "/topics/".$topic,
                    "data"              => $message,
                    "time_to_live"      => 30,
                    "delay_while_idle"  => true
                );

                $auth_key = "auth_key";

                $headers = array(
                    "Authorization: key=".$auth_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_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                $result = curl_exec($ch);

その助けを願って

于 2016-07-23T10:49:27.743 に答える
0

解決策をありがとう@Shubham。

「トピック」に通知を送信するには、@Shubham のコードを使用してください。

単一受信者のメッセージに対応するために、彼のコードを少し修正しました。ここに行きます

<?php

$url = "https://fcm.googleapis.com/fcm/send";
                $val = "<your recipient id>";
                $message = array("message" => "This is a test message from server");
                $fields = array(
                    "to"                => $val,
                    "data"              => $message,
                    "time_to_live"      => 30,
                    "delay_while_idle"  => true
                );

                $auth_key = "<Your auth key>";

                $headers = array(
                    "Authorization: key=".$auth_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_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                $result = curl_exec($ch);

?>

to - 単一の受信者
registration_ids - マルチキャスト メッセージ (多数の受信者 - 配列にする必要があります)

これは私の側からの理想的な解決策ではないかもしれませんが、間違いなく機能します:)

于 2016-07-23T12:50:42.067 に答える