我的建议是从 javascript 将用户 ID 发送到 Kotlin(应用程序),然后从应用程序发送令牌和用户我用 PHP 将其保存在数据库中,我这样做了并且它有效。
脚本.js
function showAndroidToast() {
Android.showToast("18");// ID de Usuario
}
showAndroidToast();
WebAppInterface.kt
/** Instantiate the interface and set the context */
class WebAppInterface(private val mContext: Context) {
/** Show a toast from the web page */
@JavascriptInterface
fun showToast(id_usuario: String) {
Toast.makeText(mContext, "ID-Usuario>"+id_usuario, Toast.LENGTH_SHORT).show()
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w(ContentValues.TAG, "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
}
// Obtenga un nuevo token de registro de Firebase Cloud Messaging (FCM)
val token = task.result
// MOSTRAR LOG Y TOAST
//val msg = getString(R.string.msg_token_fmt, token)
val msg = "TOKENdesdeJavascript>$token"
Log.d(ContentValues.TAG, msg)
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()
// Todo ENVIA TOKEN Y USUARIO A LA BBDD
val urlServer ="http://example.com/AP_RegistrarToken.php"
val stringRequest: StringRequest = object : StringRequest(Request.Method.POST, urlServer,
Response.Listener<String> {
fun onResponse(response: String?) {
val mensaje = "Registro Satisfactorio TOKEN y USUARIO"
Log.d(ContentValues.TAG, mensaje)
Toast.makeText(mContext, mensaje, Toast.LENGTH_LONG).show()
}
}, Response.ErrorListener {
fun onErrorResponse(error: VolleyError?) {
Toast.makeText(mContext, "Error en la Conexión TOKEN y USUARIO", Toast.LENGTH_LONG).show()
}
}) {
@Throws(AuthFailureError::class)
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params.put("id_usuario_kotlin",id_usuario)
params.put("token_kotlin", token)
return params
}
}
Log.d(ContentValues.TAG, "SE REGISTRO EXITOSAMENTE")
val requestQueue: RequestQueue = Volley.newRequestQueue(mContext)
requestQueue.add(stringRequest)
}) // FirebaseMessaging
Toast.makeText(mContext, "SE REGISTRO EXITOSAMENTE", Toast.LENGTH_SHORT).show()
} // FIN FUNCION
}
通过这种方式,仅当用户已经注册时,您才将令牌发送到数据库。
我希望它能帮助你。我昨天做了这个并且它有效并且像你一样,我正在开发一个带有 webview 的应用程序来发送通知。