1

I am developing a test app that uses notifications via FCM. I am using Volley to send data from the device to the server(WAMP) but the token is always an empty string/a default value that I've written. I searched the internet but I still don't know why the token is not stored/generated(I'm new at coding.) One more thing: everytime I send the token, the toast shows me the "good one" text I've written there but it also gives me a lot of html code. Here are my files: MainActivity.kt

import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.android.volley.AuthFailureError
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest

class MainActivity : AppCompatActivity() {
    var url: String = "......"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var button: Button = findViewById(R.id.save_token)
        button.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                val sharedPreferences =
                    applicationContext.getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE)
                var token: String = sharedPreferences.getString(getString(R.string.FCM_TOKEN),"noToken")
//                var token: String = "sss";
                val request = object : StringRequest(Request.Method.POST, url,
                    Response.Listener { response ->
                        Toast.makeText(this@MainActivity, "Good one $response", Toast.LENGTH_SHORT).show()
                        Log.i("My success", "" + response)
                    }, Response.ErrorListener { error ->
                        Toast.makeText(this@MainActivity, "my error :$error", Toast.LENGTH_LONG).show()
                        Log.i("My error", "" + error)
//                        println("Error $error")
                    }) {
                    @Throws(AuthFailureError::class)
                    override fun getParams(): Map<String, String> {

                        val params = HashMap<String, String>()
                        params.put("fcm_token", token)

                        return params
                    }
                }
                MySingleton.getmInstance(this@MainActivity).addToRequestQueue(request)
            }
        })

    }
}

FcmInstanceIdService.java

public class FcmInstanceIdService extends FirebaseMessagingService{
    @Override
    public void onNewToken(String s) {

        String  recent_token = s;
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF),Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(getString(R.string.FCM_TOKEN), recent_token);
        editor.commit();
    }
}

this is the log I get from the success toast:

2019-06-11 08:48:45.135 5684-5684/com.example.rares.firebaselast D/My success: connection success<br />
    <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: mysqli_close in C:\wamp64\www\firebase\fcm_insert.php on line <i>7</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0023</td><td bgcolor='#eeeeec' align='right'>402152</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\firebase\fcm_insert.php' bgcolor='#eeeeec'>...\fcm_insert.php<b>:</b>0</td></tr>
    </table></font>
    <br />
    <font size='1'><table class='xdebug-error xe-uncaught-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Uncaught Error: Function name must be a string in C:\wamp64\www\firebase\fcm_insert.php on line <i>7</i></th></tr>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Error: Function name must be a string in C:\wamp64\www\firebase\fcm_insert.php on line <i>7</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0023</td><td bgcolor='#eeeeec' align='right'>402152</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\firebase\fcm_insert.php' bgcolor='#eeeeec'>...\fcm_insert.php<b>:</b>0</td></tr>
    </table></font>
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

3 Answers3

3
//Get Firebase FCM token
        FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(activity, new OnSuccessListener<InstanceIdResult>() {
            @Override
            public void onSuccess(InstanceIdResult instanceIdResult) {
                String token = instanceIdResult.getToken();

            }
        });

Put this code in onCreate method of Your first activity to get FCM token. You can save it in Global Variable

Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
  • this is not correct answer (august 2020). There is no getInstanceId() anymore, and addOnSuccessListener must be called after subscribeToTopic() and also it has now different parameters. And they didnt even update their docs lol... – qkx Aug 20 '20 at 10:39
1

Try with FirebaseInstanceId

Firebase Instance ID provides a unique identifier for each app instance and a mechanism to authenticate and authorize actions

KOTLIN

        FirebaseInstanceId.getInstance().instanceId
                .addOnSuccessListener(object : OnSuccessListener<InstanceIdResult> {
                    override fun onSuccess(instanceIdResult: InstanceIdResult) {
                        val token = instanceIdResult.token //Token

                }
              })
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Create application class for your app :

class MyApplication : Application() {


override fun onCreate() {
    super.onCreate()
    initFCM()
}


private fun initFCM(){
    FirebaseInstanceId.getInstance().instanceId
            .addOnCompleteListener(OnCompleteListener { task ->
                if (!task.isSuccessful) {
                    return@OnCompleteListener
                }
                // Get new Instance ID token
                val token = task.result?.token
            })
}}

In Androidmanifest.xml file ,in android:name tag add your custom application class name like this .

<application
    android:name=".MyApplication "
    android:allowBackup="true" 
Ashish singh
  • 329
  • 1
  • 12