PHP – send a notification to Android immediately after an update occurs in the MySQL database

In my Android application, new users initially use the application to register themselves. If the user is approved by the back-end administrator, only he can log in. Now, what I want is that once the administrator approves the user, the notification should appear in the Android phone and tell the user that he can log in successfully now, Because he has been approved. I have stored my data in the MySQL database of the network server. Now I don't know how to achieve this. Any help is really appreciated

resolvent:

When a user registers in the system, you must store the user's device token. When the administrator activates the user from the back end, you must send a notification to the user through the FCM service on the device token

If you know nothing about FCM, please visit: https://firebase.google.com/docs/cloud-messaging/concept-options

Please find the push notification of PHP sending code 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);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>