skip to Main Content

I’m using Laravel version 7.0.1.
Accidently My push notification service with Laravel is not working without any changes in my code and Firebase settings.

Here I provided my function of sending push notification and also mentioned the error.

It will be great if anyone can help me out.

This is my Laravel version 7.0.1 ‘s Code

function sendNotification($data)
    {
        $url = 'https://fcm.googleapis.com/fcm/send';

        $serverKey = getenv('FIREBASE_SERVERKEY');

        $encodedData = json_encode($data);
        $headers = [
            'Authorization:key=' . $serverKey,
            '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_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        // Close connection
        curl_close($ch);

        // Decode the result
    $response = json_decode($result, true);

    // Check for errors in the FCM response
    if (isset($response['error'])) {
        die('FCM error: ' . $response['error']);
    }

    // Optionally, print the entire response for debugging
    dd($response);
        return true;
    }

**And I’m getting this error.
**

Getting this Error While Sending The notification

array:5 [ // app/Http/Controllers/Api/BaseController.php:254
  "multicast_id" => 830795794971026898
  "success" => 0
  "failure" => 1
  "canonical_ids" => 0
  "results" => array:1 [
    0 => array:1 [
      "error" => "DeprecatedApi"
    ]
  ]
]```

2

Answers


  1. Apps using the deprecated FCM legacy APIs for HTTP and XMPP should migrate to the HTTP v1 API at the earliest opportunity. Sending messages (including upstream messages) with those APIs was deprecated on June 20, 2023, and shutdown begins on July 22, 2024.

    For More Detail.
    https://firebase.google.com/docs/cloud-messaging/migrate-v1

    Login or Signup to reply.
  2. FCM API Change: Sending Notifications Using OAuth Tokens

    Google Firebase has recently changed the way requests are made to send push notifications using Firebase Cloud Messaging (FCM). Below is a guide on how to use the new API format and generate OAuth tokens for authorization.

    Old API Request:

    URL https://fcm.googleapis.com/fcm/send

    headers
    
    {
      "Authorization": "key=AAAAtXILhBA:APA91bH9..."
    }
    
    body
    
    {
      "data": {
        "message": "Notification msg !!!",
        "title": "New Notification"
      },
      "priority": "high",
      "to": "fcm_token"
    }
    

    New API Request (As of Firebase update):

    URL: https://fcm.googleapis.com/v1/projects/salezrobot/messages:send
    
    Headers
    
    {
      "Content-Type": "application/json",
      "Authorization": "Bearer ya29.a0AcM612x7sfqwzPZN8..."
    }
    
    Body
    
    {
      "message": {
        "token": "device_fcm_token",
        "data": {
          "message": "Notification msg",
          "title": "Notification title"
        }
      }
    }
    

    The Authorization header now requires an OAuth token (not an API key).
    This token expires every hour and needs to be regenerated
    programmatically.

    How to Generate OAuth Token for Testing (Using Postman):

    1. Open the OAuth 2.0 Playground.

    2. In the left panel, select:

    * Firebase Cloud Messaging API v1.
    
    * Then, check https://www.googleapis.com/auth/cloud-platform.
    

    enter image description here

    3. Click Authorize APIs, and login or select your Google account.

    enter image description here

    4. After authorization, click Exchange authorization for tokens

    5. Copy the generated OAuth token. You can use this token for testing in Postman or other tools.

    Important Note:

    The OAuth token will expire in 1 hour, so it needs to be regenerated. For production, you should generate this token programmatically using the steps described here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search