How to send a token from the SharedPreferences to MySQL with a session PHP script?

  • Replies:0
  • Forum posts: 1

Jan 6, 2019, 2:42:53 PM via Website

I have web view app and I ask for help. I have the following scripts:, and login.php, where is login.php hosted on the server.

I need to send the recorded token from SharedPreference to the MySQL database using the login.php script that contains the session.

When I delete the session from login.php, the token is record to the MySQL, when I return the session to the login.php the token is not recorded in the database.

Where am I wrong? Please Help.


import android.content.Context;
    import android.content.SharedPreferences;

    public class SharedPreference {
        private static final String SHARED_PREF_NAME = "FCMSharedPref";
        private static final String TAG_TOKEN = "tagtoken";

        private static SharedPreference mInstance;
        private static Context mCtx;

        private SharedPreference(Context context) {
            mCtx = context;
        public static synchronized SharedPreference getInstance(Context context) {
            if (mInstance == null) {
                mInstance = new SharedPreference(context);
            return mInstance;
        //this method will save the device token to shared preferences
        public boolean saveDeviceToken(String token){
            SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(TAG_TOKEN, token);
            return true;
        //this method will fetch the device token from shared preferences
        public String getDeviceToken(){
            SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
            return  sharedPreferences.getString(TAG_TOKEN, null);

import android.util.Log;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "FirebaseIIDService";

    public void onTokenRefresh() {

        String token = FirebaseInstanceId.getInstance().getToken();


    private void registerToken(String token) {

        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()

        Request request = new Request.Builder()

        try {
        } catch (IOException e) {

    private void storeToken(String token) {
        //saving the token on shared preferences



require_once(dirname(__FILE__) . "/users/");

    $UserK = new User();

    // Login from post data
    if( isset($_POST["username"]) )

        $userId = $UserK->loginUser($_POST["username"], $_POST["password"]);
        if(!$userId) {
        $error = ‘Error username or password’;

        if (isset($_REQUEST["Token"])) {
            require_once ("config.php");


            $conn = connect_db();

            $q="INSERT INTO user_push_notification (userId, Token) VALUES ({$userId}, '{$_uv_Token}') "
                ." ON DUPLICATE KEY UPDATE Token = '$_uv_Token', userId={$userId};";

            mysqli_query($conn,$q) or die(mysqli_error($conn));


        header("Location: menu.php");

} // Validation end

— modified on Jan 6, 2019, 2:46:12 PM

Be the first to answer