Saidqb
Saidqb Hanya sekedar bercerita dengan damai

Firebase Fcm v1

Summary

TextInput

Masking Rupiah

https://medium.com/@vc21496/updating-from-fcm-legacy-api-to-http-v1-api-d0a5a765d363

  1. Create and Download a Service Account Key:
    • Go to your Google Cloud Console.
    • Navigate to IAM & Admin > Service accounts.
    • Create a new service account or select an existing one.
    • Assign the “Firebase Admin SDK Administrator Service Agent” role to the service account.
    • Generate a new JSON key and download it (service account > key > add key). Save this key in your Laravel project’s storage directory (e.g., storage/app/firebase-service-account.json).
  2. Update Your FcmChannel Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

namespace App\Broadcasting;

use Illuminate\Notifications\Notification;
use GuzzleHttp\Client;
use Google_Client;

class FcmChannel
{
    public function send($notifiable, Notification $notification)
    {
        $message = $notification->toFcm($notifiable);

        $client = new Client();
        $response = $client->post('https://fcm.googleapis.com/v1/projects/YOUR_PROJECT_ID/messages:send', [
            'headers' => [
                'Authorization' => 'Bearer ' . $this->getAccessToken(),
                'Content-Type' => 'application/json',
            ],
            'json' => [
                'message' => [
                    'token' => $message['to'],
                    'notification' => $message['notification'],
                    'data' => $message['data'],
                ],
            ],
        ]);

        return $response;
    }

    private function getAccessToken()
    {
        $credentialsPath = storage_path('app/firebase-service-account.json'); // Path to your service account file

        $client = new Google_Client();
        $client->setAuthConfig($credentialsPath);
        $client->addScope('https://www.googleapis.com/auth/firebase.messaging');

        $token = $client->fetchAccessTokenWithAssertion();
        return $token['access_token'];
    }
}
Rating:

comments powered by Disqus