如何使用Firebase从Laravel发送消息到用户的手机号码?

huangapple go评论101阅读模式
英文:

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.

  1. public function sendOTP($phone)
  2. {
  3. $code = $this->generateOTPCode();
  4. // save to auth otp table
  5. $auth_otp = AuthOTP::create([
  6. 'phone' => $phone,
  7. 'code' => $code,
  8. 'status' => BaseConstants::STATUS_INACTIVE
  9. ]);
  10. if ($auth_otp) {
  11. // Send otp to phone number via sms service
  12. $messaging = app('firebase.messaging');
  13. $registrationToken = // how can I get the device token of the user?
  14. $message = CloudMessage::withTarget('token', $registrationToken)
  15. ->withNotification(Notification::create('Mã xác thực OTP', "Mã xác thực của bạn là: $code"))
  16. ->withData(['otp_code' => $code]);
  17. $messaging->sendMulticast([$message], $registrationToken);
  18. //session(['otp' => $otp]);
  19. $messaging->send($message);
  20. }
  21. return $auth_otp;
  22. }

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.

  1. public function sendOTP($phone)
  2. {
  3. $code = $this->generateOTPCode();
  4. // save to auth otp table
  5. $auth_otp = AuthOTP::create([
  6. 'phone' => $phone,
  7. 'code' => $code,
  8. 'status' => BaseConstants::STATUS_INACTIVE
  9. ]);
  10. if ($auth_otp) {
  11. // Send otp to phone number via sms service
  12. $messaging = app('firebase.messaging');
  13. $registrationToken = // how can I get the device token of user?
  14. $message = CloudMessage::withTarget('token', $registrationToken)
  15. ->withNotification(Notification::create('Mã xác thc OTP', "Mã xác thc ca bn là: $code"))
  16. ->withData(['otp_code' => $code]);
  17. $messaging->sendMulticast([$message], $registrationToken);
  18. //session(['otp' => $otp]);
  19. $messaging->send($message);
  20. }
  21. return $auth_otp;
  22. }

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.

  1. FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
  2. if (!task.isSuccessful) {
  3. Log.w(TAG, "Fetching FCM registration token failed", task.exception)
  4. return@OnCompleteListener
  5. }
  6. // Get new FCM registration token
  7. val token = task.result
  8. // Log and toast
  9. val msg = getString(R.string.msg_token_fmt, token)
  10. Log.d(TAG, msg)
  11. Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
  12. })

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.

  1. FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
  2. if (!task.isSuccessful) {
  3. Log.w(TAG, "Fetching FCM registration token failed", task.exception)
  4. return@OnCompleteListener
  5. }
  6. // Get new FCM registration token
  7. val token = task.result
  8. // Log and toast
  9. val msg = getString(R.string.msg_token_fmt, token)
  10. Log.d(TAG, msg)
  11. Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
  12. })

You can check here for a detailed explanation.
Normally after generation, you send the token to your backend for later use.

huangapple
  • 本文由 发表于 2023年5月11日 17:17:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定