Send notification to android as soon as update occurs in MySQL database

In my Android application, the new user is first registered in the application, and then if the user is approved by the administrator from the database, then he will be able to log in. Now, I want, as soon as the administrator approves the user should appear on the Android mobile phone, informing the user that now he can log in successfully, since he was approved. I saved my data in a MySQL database on a web server. Now I have no idea how to implement this. Any help really be appreciated.

+4
source share
2 answers

You need to save the token of the user device during user registration in the system. And when the administrator activates the user from the backend, you need to send a notification to the user using the FCM service on the device token.

If you have no idea about FCM, visit: https://firebase.google.com/docs/cloud-messaging/concept-options

Confirm sending notification code with PHP below.

function sendFCMPushnotification($arr) {
    $device_token = $arr['device_token'];
    $message = $arr['message'];

    $url = 'https://fcm.googleapis.com/fcm/send';

    $fields = array (
            'registration_ids' => array (
                    $device_token
            ),
            'data' => array (
                "message" => $message,
                "sound"    =>  "default"
            ),
            'notification' => array(
                'body'  => $message,
                'title' =>  'ProjectName',
            )
    );

    $fields = json_encode ( $fields );    
    $headers = array (
            'Authorization: key=' . "PUT_YOUR_FCM_Key",
            '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_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    var_dump($result);
    curl_close ( $ch );
}
$arr = [
    'device_token'  =>  "PLACE_YOUR_DEVICE_TOKEN",
    'message'  =>  'PLACE_YOUR_MESSAGE',
];
sendFCMPushnotification($arr);
0
source

First of all, enable the push notification service in the application. Then, from the server side, you need to send a push notification when you have approved the credentials so that the user receives a push notification in the application.

0
source

Source: https://habr.com/ru/post/1658521/


All Articles