英文:
How to send message from laravel to user's phone number using firebase?
问题
I'm making a website using Firebase and I want to send otp
to the user when they register their new account.
This is my function to send otp
to the user by their phone number.
public function sendOTP($phone)
{
$code = $this->generateOTPCode();
// save to auth otp table
$auth_otp = AuthOTP::create([
'phone' => $phone,
'code' => $code,
'status' => BaseConstants::STATUS_INACTIVE
]);
if ($auth_otp) {
// Send otp to phone number via sms service
$messaging = app('firebase.messaging');
$registrationToken = // how can I get the device token of the user?
$message = CloudMessage::withTarget('token', $registrationToken)
->withNotification(Notification::create('Mã xác thực OTP', "Mã xác thực của bạn là: $code"))
->withData(['otp_code' => $code]);
$messaging->sendMulticast([$message], $registrationToken);
//session(['otp' => $otp]);
$messaging->send($message);
}
return $auth_otp;
}
I tried to use Firebase cloud message, but I don't know how to get the device token. So, how can I get the device token from the user?
英文:
I'm making a website using Firebase and I want to send otp
to the user when they register their new account.
This is my function to sent otp
to user by their phone number.
public function sendOTP($phone)
{
$code = $this->generateOTPCode();
// save to auth otp table
$auth_otp = AuthOTP::create([
'phone' => $phone,
'code' => $code,
'status' => BaseConstants::STATUS_INACTIVE
]);
if ($auth_otp) {
// Send otp to phone number via sms service
$messaging = app('firebase.messaging');
$registrationToken = // how can I get the device token of user?
$message = CloudMessage::withTarget('token', $registrationToken)
->withNotification(Notification::create('Mã xác thực OTP', "Mã xác thực của bạn là: $code"))
->withData(['otp_code' => $code]);
$messaging->sendMulticast([$message], $registrationToken);
//session(['otp' => $otp]);
$messaging->send($message);
}
return $auth_otp;
}
I tried to use Firebase cloud message, but I don't know how to get device token. So, how can I get the device token from user?
答案1
得分: 1
The device token is generated on the user's device, for instance, in a mobile app.
The code to generate the token should be similar to the one below.
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w(TAG, "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
}
// Get new FCM registration token
val token = task.result
// Log and toast
val msg = getString(R.string.msg_token_fmt, token)
Log.d(TAG, msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
})
You can check here for a detailed explanation.
Normally after generation, you send the token to your backend for later use.
英文:
The device token is generated on the user's device, for instance, in a mobile app.
The code to generate the token should be similar to the one bellow.
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w(TAG, "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
}
// Get new FCM registration token
val token = task.result
// Log and toast
val msg = getString(R.string.msg_token_fmt, token)
Log.d(TAG, msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
})
You can check here for a detailed explanation.
Normally after generation, you send the token to your backend for later use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论