Send push notifications to Android devices, In another word FCM (Firebase Cloud Messaging), is the centerpiece of the remote notifications facility. highly effective service for Android application developers to propagate information to push notification to Android devices (and, indirectly, watchOS).
FCM Push notification is a message that looks like mobile alerts, that pops up on a mobile device who have installed an app. Sending push notifications to an iOS or Android Application can enhance the user experience exponentially.
The steps to configure FCM
Step 1. Make sure PHP extension CURL is enabled.
Step 2. Create the project in FIREBASE(https://firebase.google.com/) and get server key (Authorization Key).
Step 3. Write below code in PHP file using your project android device registration id and Authorization Key(server key).
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 |
$registrationIds = array(); //Replace with your registrationId $registrationIds[0]="eavs0VY9eQQ:APA91bE00CRj6f7-H9Kkew8m5_Z"; //Send notificatioin to multipal android device like //$registrationIds[1]="cudhioah90acacihuidshcsdbyugusydgc0suchschuishdhcdhcusd" // Replace with your project authorization_key (server key) $authorization_key="AIzaSyCQQLcqDm_4DhZd57aoTHfXKW-6MFyKXc4"; $msg = array(); $msg['message'] = "Testing message"; // You can pass multipal key in notification $fields = array ( 'registration_ids' => $registrationIds, 'data' => $msg ); $headers = array ( 'Authorization: key=' . $authorization_key, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://android.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); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); $result = curl_exec($ch); curl_close($ch); echo $result; |
More Stories
CPU & Memory usage in PHP
Install PHP mcrypt extension on Ubuntu
Text to speech