Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64
Your IP : 3.137.175.83
<?php
// FCM Server Key from your Firebase project settings
$serverKey = 'AAAAgJjtDyo:APA91bH7vx_QfSxjkz2ZxoaVORvtyfkjA4W3kU4p96GPpF0hbpByEczObtu0ky4KS4XA-shF993AXXCapFsdzZFBrdPx5KUgaKBlQTuZDgQAHQZyUuKXTkMGewteQZBsxYRDIsmO2J7H';
// Device token of the recipient (obtained from your app)
$deviceToken = 'eChFaZHoZkrGmcQ7_CmpYi:APA91bEd6KRKiOL7B7tHNsp_dwtjQvieqWboasr4GOXyvwoWt8AEWWuJ4pKQBLvww6OwRKdCmq-iSF7Xbr6KeBR0HiKUkwyq3VMURShkmtPb9BQmCGqRfsXVvRb8HCcu30C84-J_85Hw';
// Notification payload
$message = array(
'title' => 'Notification Title',
'body' => 'Notification Body',
);
$data = array(
'to' => $deviceToken,
'notification' => $message,
);
// Set headers
$headers = array(
'Authorization: key=' . $serverKey,
'Content-Type: application/json',
);
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification (for testing)
// Set the payload as JSON
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// Execute cURL session
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
} else {
// Print FCM response
echo $response;
}
// Close cURL session
curl_close($ch);
?>
Make sure to replace 'YOUR_SERVER_KEY' with your Firebase Server Key and 'DEVICE_TOKEN' with the target device's token
obtained from your React Native app.
Step 4: Trigger the PHP Script
To send a push notification from your PHP server, you'll need to trigger the PHP script when a specific event occurs or
when you want to send a notification. This can be done via a web request, a server-side script, or any other mechanism
suitable for your application's architecture.
Step 5: Handle Notifications in React Native
In your React Native app, you should have already set up the handling of push notifications using a library like
react-native-push-notification or react-native-notifications. Make sure your app listens for incoming notifications and
displays them to the user as needed.
By following these steps, you can set up a system to send push notifications from a PHP server to your React Native app
using Firebase Cloud Messaging (FCM).
|