Invalid key on firebase

error saat mau mengirim notifikasi dari server laravel ke android.

//storing token in database
    public function registerDevice(Request $request){

        // validate fields
        $validate = Validator::make($request->all(), [
            'email'     => 'required',
            'token'     => 'required'
        ]);

        if ($validate->fails()) {
            return response()->json([
                'success'   => false,
                'message'   => "sorry, you must fill the field!",
                'errors'    => $validate->errors()
            ]);
        }

        if(!Device::where('email', '=', $request->email)->exists()) {
            // save to db
            $save = Device::create([
                'email'     => $request->email,
                'token'     => $request->token
            ]);

            if($save) {
                return response()->json([
                    'success' => true,
                    'message' => "token successfully has been saved!"
                ]); //return 0 means success
            } else {
                return response()->json([
                    'success' => false,
                    'message' => "token was failed to save!"
                ]);
            } //return 1 means failure
        }else{
            // update db by email registered
            $save = Device::where('email', '=', $request->email)->update(['token' => $request->token]);

            if(!$save) {
                return response()->json([
                    'success' => false,
                    'message' => "token was failed to save!"
                ]);
            }

            $notif = new Push("test", "testting", null);

            $deviceToken = Device::select('token')->where('email', $request->email)->first();

            $getNotif = $notif->getPush();

            $firebase = new Firebase();

            return $firebase->send($deviceToken, $getNotif);

            return response()->json([
                'success' => true,
                'message' => "The token has ben refreshed!"
            ]);
        }

    }

^^^ Untuk fungsi yang saya panggil

dan dibawah adalah fungsi yang berhubungan

<?php

namespace App\Http\Controllers\Utils;

class Push {
    //notification title
    private $title;

    //notification message
    private $message;

    //notification image url
    private $image;

    //initializing values in this constructor
    function __construct($title, $message, $image) {
         $this->title = $title;
         $this->message = $message;
         $this->image = $image;
    }

    //getting the push notification
    public function getPush() {
        $res = array();
        $res['data']['title'] = $this->title;
        $res['data']['message'] = $this->message;
        $res['data']['image'] = $this->image;
        return $res;
    }
}

<?php

namespace App\Http\Controllers\Utils;

use Symfony\Component\HttpKernel\Client;

class Firebase {

    // id server notifikasi
    private $idFCM = "726302345063";

    public function send($registration_ids, $message) {
        $fields = array(
            'registration_ids' => $registration_ids,
            'data' => $message,
        );
        return $this->sendPushNotification($fields);
    }

    /*
    * This function will make the actuall curl request to firebase server
    * and then the message is sent
    */
    private function sendPushNotification($fields) {

        // Set POST variables
        $url = 'https://fcm.googleapis.com/fcm/send';

        $client = new \GuzzleHttp\Client();

        $result = $client->post( $url, [
            'json' =>
                $fields
            ,
            'headers' => [
                'Authorization' => 'key='.env('FCM_LEGACY_KEY'),
                'Content-Type'  => 'application/json',
            ],
        ] );

        return json_decode( $result->getBody(), true );
    }
}

avatar HabibulUmam
@HabibulUmam

59 Kontribusi 10 Poin

Dipost 4 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban