How can I send a Firebase Cloud Messaging notification without use the Firebase Console?

后端 未结 16 1286
南旧
南旧 2020-11-22 10:17

I\'m starting with the new Google service for the notifications, Firebase Cloud Messaging.

Thanks to this code https://github.com/firebase/quickstart-a

相关标签:
16条回答
  • 2020-11-22 10:47

    First you need to get a token from android and then you can call this php code and you can even send data for further actions in your app.

     <?php
    
    // Call .php?Action=M&t=title&m=message&r=token
    $action=$_GET["Action"];
    
    
    switch ($action) {
        Case "M":
             $r=$_GET["r"];
            $t=$_GET["t"];
            $m=$_GET["m"];
    
            $j=json_decode(notify($r, $t, $m));
    
            $succ=0;
            $fail=0;
    
            $succ=$j->{'success'};
            $fail=$j->{'failure'};
    
            print "Success: " . $succ . "<br>";
            print "Fail   : " . $fail . "<br>";
    
            break;
    
    
    default:
            print json_encode ("Error: Function not defined ->" . $action);
    }
    
    function notify ($r, $t, $m)
        {
        // API access key from Google API's Console
            if (!defined('API_ACCESS_KEY')) define( 'API_ACCESS_KEY', 'Insert here' );
            $tokenarray = array($r);
            // prep the bundle
            $msg = array
            (
                'title'     => $t,
                'message'     => $m,
               'MyKey1'       => 'MyData1',
                'MyKey2'       => 'MyData2', 
    
            );
            $fields = array
            (
                'registration_ids'     => $tokenarray,
                'data'            => $msg
            );
    
            $headers = array
            (
                'Authorization: key=' . API_ACCESS_KEY,
                'Content-Type: application/json'
            );
    
            $ch = curl_init();
            curl_setopt( $ch,CURLOPT_URL, '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 );
            return $result;
        }
    
    
    ?>
    
    0 讨论(0)
  • 2020-11-22 10:51

    Works in 2020

    $response = Http::withHeaders([
                'Content-Type' => 'application/json',
                'Authorization'=> 'key='. $token,
            ])->post($url, [
                'notification' => [
                    'body' => $request->summary,
                    'title' => $request->title,
                    'image' => 'http://'.request()->getHttpHost().$path,
                    
                ],
                'priority'=> 'high',
                'data' => [
                    'click_action'=> 'FLUTTER_NOTIFICATION_CLICK',
                    
                    'status'=> 'done',
                    
                ],
                'to' => '/topics/all'
            ]);
    
    0 讨论(0)
  • 2020-11-22 10:59
    Go to cloud Messaging select:  Server key
    
    
    
    function sendGCM($message, $deviceToken) {
    
        $url = 'https://fcm.googleapis.com/fcm/send';
        $fields = array (
                'registration_ids' => array (
                    $id
                ),
                'data' => array (
                    "title" =>  "Notification title",
                    "body" =>  $message,
                )
        );
        $fields = json_encode ( $fields );
        $headers = array (
            'Authorization: key=' . "YOUR_SERVER_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_POSTFIELDS, $fields );
        $result = curl_exec ( $ch );
        echo $result;
    
        curl_close ($ch);
    }
    
    0 讨论(0)
  • 2020-11-22 10:59

    Using Firebase Console you can send message to all users based on application package.But with CURL or PHP API its not possible.

    Through API You can send notification to specific device ID or subscribed users to selected topic or subscribed topic users.

    Get a view on following link. It will help you.
    https://firebase.google.com/docs/cloud-messaging/send-message
    
    0 讨论(0)
  • 2020-11-22 10:59

    Or you can use Firebase cloud functions, which is for me the easier way to implement your push notifications. firebase/functions-samples

    0 讨论(0)
  • 2020-11-22 11:00

    this solution from this link helped me a lot. you can check it out.

    The curl.php file with those line of instruction can work.

    <?php 
    // Server key from Firebase Console define( 'API_ACCESS_KEY', 'AAAA----FE6F' );
    $data = array("to" => "cNf2---6Vs9", "notification" => array( "title" => "Shareurcodes.com", "body" => "A Code Sharing Blog!","icon" => "icon.png", "click_action" => "http://shareurcodes.com"));
    $data_string = json_encode($data);
    echo "The Json Data : ".$data_string;
    $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_POSTFIELDS, $data_string);
    $result = curl_exec($ch);
    curl_close ($ch);
    echo "<p>&nbsp;</p>";
    echo "The Result : ".$result;
    

    Remember you need to execute curl.php file using another browser ie not from the browser that is used to get the user token. You can see notification only if you are browsing another website.

    0 讨论(0)
提交回复
热议问题