I get this error when I send a push notification from the backend in laravel 10.
{
“error”: {
“code”: 403,
“message”: “SenderId mismatch”,
“status”: “PERMISSION_DENIED”,
“details”: [
{
“@type”: “type.googleapis.com/google.firebase.fcm.v1.FcmError”,
“errorCode”: “SENDER_ID_MISMATCH”
}
]
}
}
class FirebaseController extends Controller
{
public function testPush(Request $request)
{
// Path to the Service Account JSON file
$serviceAccount = ServiceAccount::fromJsonFile(config_path(‘firebase-adminsdk.json’));
// Initialize the Firebase Factory with the service account
$firebase = (new Factory)
->withServiceAccount($serviceAccount)
->create();
// Get the Messaging component
$messaging = $firebase->getMessaging();
$deviceToken = 'e7x0c2ydQ86RhIh5VSJ9J_:TrPFu2h_EqJFLz5TlAXFxLTKrgkfoTYAAgoIqlsgpq_sD_dYwXEeASgS3Jzmfk7wp-bR22Fa0ZDFkZUVeuzkPx6kZFrLbUCaH1I6RgJtPT1x_J6'; // Replace with your actual device token
// Create a message
$message = CloudMessage::withTarget('token', $deviceToken)
->withNotification(Notification::create('Test Notification', 'This is a test notification from Laravel cURL!'));
// Send the message
try {
$messaging->send($message);
return response()->json(['success' => 'Notification sent successfully'], 200);
} catch (KreaitFirebaseExceptionMessagingException $e) {
return response()->json(['error' => $e->getMessage()], 400);
} catch (KreaitFirebaseExceptionFirebaseException $e) {
return response()->json(['error' => $e->getMessage()], 400);
}
}
}