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

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

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.

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:

确定