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

  • Replies:0
  • OpenNot stickiedUnanswered
  • 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: SharedPreference.java, FirebaseInstanceIDService.java 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.

MainActivity.java

FirebaseMessaging.getInstance().subscribeToTopic("test");
FirebaseInstanceId.getInstance().getToken();

SharedPreference.java

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);
            editor.apply();
            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);
        }
}

FirebaseInstanceIDService.java

import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;


public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "FirebaseIIDService";

    @Override
    public void onTokenRefresh() {

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

        registerToken(token);
    }

    private void registerToken(String token) {

        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("Token",token)
                .build();

        Request request = new Request.Builder()
                .url("localhost/login.php”)
                .post(body)
                .build();

        try {
            client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void storeToken(String token) {
        //saving the token on shared preferences
        SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token);
    }
}

login.php

session_start();

require_once(dirname(__FILE__) . "/users/su.inc.php");

    $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’;
        http_response_code(401);
    }
        else
        {

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

            $_uv_Token=$_REQUEST["Token"];

            $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");
        exit;
        }

} // Validation end

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

Be the first to answer