web-dev-qa-db-ja.com

Firebaseトピック通知の送信方法

以下のスクリプトを使用して、特定のユーザーに通知を送信しています。

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'My_API_KEY' );
$registrationIds = array( TOKENS );
// prep the bundle
$msg = array
(
    'body'  => "abc",
    'title'     => "Hello from Api",
    'vibrate'   => 1,
    'sound'     => 1,
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'          => $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 ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>

スクリプトは正常に動作していますが、アプリをインストールしたすべてのユーザーに通知を送信するにはどうすればよいですか。アプリ(アラート)でトピックを作成し、firebaseコンソール経由ですべてのユーザーに通知を送信できます。トピックの上記のスクリプトを更新するために誰でも私を導くことができます。

18
DIGITAL JEDI

交換して修正しました

$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'          => $msg
);

$fields = array
(
    'to'  => '/topics/alerts',
    'notification'          => $msg
);
23
DIGITAL JEDI

カールなしで通知を送信できます(私のサーバーでは使用できませんでした)。指定したトピックに通知を送信できる関数を準備しました。

sendNotification("New post!", "How to send a simple FCM notification in php", ["new_post_id" => "605"], "new_post", "YOUR_SERVER_KEY");

function sendNotification($title = "", $body = "", $customData = [], $topic = "", $serverKey = ""){
    if($serverKey != ""){
        ini_set("allow_url_fopen", "On");
        $data = 
        [
            "to" => '/topics/'.$topic,
            "notification" => [
                "body" => $body,
                "title" => $title,
            ],
            "data" => $customData
        ];

        $options = array(
            'http' => array(
                'method'  => 'POST',
                'content' => json_encode( $data ),
                'header'=>  "Content-Type: application/json\r\n" .
                            "Accept: application/json\r\n" . 
                            "Authorization:key=".$serverKey
            )
        );

        $context  = stream_context_create( $options );
        $result = file_get_contents( "https://fcm.googleapis.com/fcm/send", false, $context );
        return json_decode( $result );
    }
    return false;
}
3
Ambrus Tóth

私は何度もグーグルファイヤーベースで仕事をしており、トピックに関する通知を送信するためのシンプルなコードを提案しています。

public function test()
{
    // Method - 1
    // $fcmUrl = 'https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1';
    // $notification = [
    //     "message" => [
    //         "topic" => "foo-bar",
    //         "notification" => [
    //             "body" : "This is a Firebase Cloud Messaging Topic Message!",
    //             "title" : "FCM Message",
    //         ]
    //     ]
    // ]; 

    // Method - 2 

    $fcmUrl = 'https://fcm.googleapis.com/fcm/send';



    $notification = [
        "to" => '/topics/cbtf',
            "data" => [
                "message" : "Messaging Topic Message!",
            ]
        ]
    ];


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


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fcmUrl);
    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($notification));
    $result = curl_exec($ch);
    curl_close($ch);

    return true;
}
0
Harsukh Makwana