phplaravelpush-notificationlaravel-8onesignal

Issue: 403 Forbidden Error When Sending Notification via OneSignal


Description:

I am encountering a 403 Forbidden error when attempting to send a notification using the OneSignal::sendNotificationToUser function in Laravel. The error message suggests that the request is being denied due to a missing or invalid API key in the Authorization header. Below is the error message from the exception:

Client error: `POST https://onesignal.com/api/v1/notifications` resulted in a `403 Forbidden` response:
{"errors": ["Access denied. Please include an 'Authorization: Basic ...' header with a valid API key"]}

Code Example:

Here’s the code I’m using to send the notification:

try {
    OneSignal::sendNotificationToUser(
        $message,
        $player_id,
        $url = null,
        $data = null,
        $buttons = null,
        $schedule = null
    );
} catch (\Exception $e) {
    return response()->json([
        "error" => 'one_signal_error',
        "message" => $e->getMessage(),
        "file" => $e->getFile(),
        "line" => $e->getLine(),
    ], 422);
}

Error Message:

{
    "error": "one_signal_error",
    "message": "Client error: `POST https://onesignal.com/api/v1/notifications` resulted in a `403 Forbidden` response:\n{\"errors\": [\"Access denied.  Please include an 'Authorization: Basic ...' header with a valid API key (https:\/\/documenta (truncated...)\n",
    "file": "/home/arloponc/public_html/developer/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php",
    "line": 113
}

Expected Behavior:

The notification should be sent successfully when the correct OneSignal API key is used.

Actual Behavior:

The request results in a 403 Forbidden response, indicating that the API key is either missing or invalid. I have already verified the API key and added it to the environment variables, but the issue persists.

Steps to Reproduce:

  1. Set up the OneSignal integration as per the package instructions.
  2. Use the sendNotificationToUser function to send a notification.
  3. Observe the 403 Forbidden error in the response.

Environment:

Additional Information:

Any guidance or suggestions would be appreciated.


Solution

  • Please include valid API key with your request header.

    It seems like you're using the GitHub package and may be the configuration portion is not properly done.

    After everything run php artisan optimize:clear to clear the configuration cache. Cross verify your secrets with your actual credentials.