0

How to get customer's device token in order to send the notifications on customer device in Magento 2.

1 Answer 1

0

Try the below code:

Add this code in your Helper class: app/code/[Vendor]/[Module]/Helper/Data.php

<?php
namespace [Vendor]\[Module]\Helper;

use Magento\Integration\Model\CredentialsValidator;
use Magento\Integration\Model\Oauth\TokenFactory as TokenModelFactory;
use Magento\Integration\Model\Oauth\Token\RequestThrottler;

class Data extends AbstractHelper 
{
    ...
    /**
     * @var \Magento\Integration\Model\CredentialsValidator
     */
    private $validatorHelper;

    /**
     * Token Model
     *
     * @var TokenModelFactory
     */
    private $tokenModelFactory;

    /**
     * @var RequestThrottler
     */
    private $requestThrottler;

    public function __construct(
        ...
        CredentialsValidator $validatorHelper,
        TokenModelFactory $tokenModelFactory
    ) {
        parent::__construct($context);
        ...
        $this->validatorHelper = $validatorHelper;
        $this->tokenModelFactory = $tokenModelFactory;
    }

    ...
    public function createCustomerTokenKey($email, $password, $customerId)
    {
        $this->validatorHelper->validate($email, $password);
        $this->getRequestThrottler()->throttle($email, RequestThrottler::USER_TYPE_CUSTOMER);
        $this->getRequestThrottler()->resetAuthenticationFailuresCount($email, RequestThrottler::USER_TYPE_CUSTOMER);
        return $this->tokenModelFactory->create()->createCustomerToken($customerId)->getToken();
    }

    /**
     * Get request throttler instance
     *
     * @return RequestThrottler
     * @deprecated 100.0.4
     */
    private function getRequestThrottler()
    {
        if (!$this->requestThrottler instanceof RequestThrottler) {
            return \Magento\Framework\App\ObjectManager::getInstance()->get(RequestThrottler::class);
        }
        return $this->requestThrottler;
    }
}

Then add this in your Model class: app/code/[Vendor]/[Module]/Model/CustomerManagement.php

<?php
namespace [Vendor]\[Module]\Model;

...
use [Vendor]\[Module]\Helper\Data;

class CustomerManagement extends AbstractHelper 
{
    ...
    /**
     * @var [Vendor]\[Module]\Helper\Data
     */
    protected $dataHelper;

    public function __construct(
        ...
        Data $dataHelper
    ) {
        parent::__construct($context);
        ...
        $this->dataHelper =$dataHelper;
    }

    ...
    public function customerLogin()
    {
        $token = $this->dataHelper->createCustomerTokenKey($email, $password, $customerId);

        # Now you can use this $token variable while setting data of customer account as below:
        $customerData['token'] = $token;
    }
}

Hope it will help you. :)

1
  • Hello Ubed, i am not getting the solution, the exact problem is i want to get the device token from the android app while customer log in to the account, Then the Magento customer log in page will have three field 1.) username 2.) device_token 3.) password but customer can see onely two fields that is username and password so when the customer will try to log in then device token will generate and i will take the value from the device_token field. Commented Nov 25, 2021 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.